第二个问题可以使用RandomAccessFile类来实现

解决方案 »

  1.   

    public static boolean copyFile(String sourceFile ,String objectFile) throws IOException
     {
        File inFile = new File(sourceFile) ;
    if (!inFile.exists())
    {
    System.out.println("文件不存在!") ;
    return false;
    }
    if ( inFile.isDirectory() )
    {
    System.out.println("不能复制文件夹!") ;
    return false ;
    }
    File outfile = new File(objectFile) ;
            String objectFile = outfile.getParent() ;
            if( objectFile != null )
            {
                File outfile = new File(objectFile ) ;
                if( !outfile.exists() )
                     outfile.mkdirs();
            }
    FileInputStream  is = new FileInputStream( sourceFile ) ;      
        FileOutputStream os = new FileOutputStream( objectFile ) ;
            int ch ;
            while ((ch = is.read()) >= 0)  os.write(ch) ;
            is.close();//关闭输入流
            os.close();//关闭输出流
     }
      

  2.   

    不是很理解你的问题:你说每次读取4k byte然后转为BigInteger,你的意思是否是
    while(文件未结束)
    {
         读取4kbyte内容;
         解析读取来得4kbyte内容变为一个一个的数字;
         对这些数字进行计算;
         计算结果output到另一个新文件;
    }
    是这样么???
      

  3.   

    package iotest;import java.math.*;
    import java.io.*;
    import java.util.*;public class Compute
    {
    public static void main(String[] args) throws IOException
        {
         //DataInputStream in =
             //new DataInputStream(
                    //new BufferedInputStream(
                     //new FileInputStream("YourFile.java")));//首先将文件读入成输入流in //因为你在读取以后需要进行大数字运算,所以这里就不能使用DataInputStream里的readDouble()等方法
    //使用readDouble()可能会出现无法读到的情况,因为文件里有些数字超出了java定义的double最大数
    //除非你可以保证没有数超出java规定的double最大数才可以这么做
    //所以我的做法是,必须需要知道数字的分隔符号,否则这个方法没用
    try
    {
       FileReader file;
    StreamTokenizer st;
       file = new FileReader("YourFile");
    st = new StreamTokenizer(new BufferedReader(file));
    st.ordinaryChar('/');//这里我假设数字之间是以/分隔 ArrayList al = new ArrayList();//存放取得的大数字
    int m = 0; while(st.nextToken() != StreamTokenizer.TT_EOF)//尚未结束
    {
             BigInteger bi;
             String s;
             switch(st.ttype)
             {
             //使用TT_WORD而不使用TT_NUMBER是因为TT_NUMBER时,其nval为double
                case StreamTokenizer.TT_WORD:
                 try
                 {
                 bi = new BigInteger(st.sval);//取得数字并转为大数字
                 al.add(m++ , bi);//顺序存放取得的数字
                 }
                 catch(NumberFormatException e2) //不是数字字符串
                 {
                 bi = BigInteger.valueOf(0);
                 }
                 break;
                default: // single character in ttype
                 s = "Nothing to do!";
            }
         }      //此时arraylist里存放了全部解析数字,你可以按需要来获取数字进行计算,如:
         BigInteger a = (BigInteger)al.get(0);
         BigInteger b = (BigInteger)al.get(1);
         BigInteger c = a.add(b);
        }
        catch(Exception e)
        {
         e.printStackTrace();
        }
        }
    }
    //以上代码编译通过,但没有实际测试过
    //缺点是必须知道字段之间特定的分隔符号,同时没有满足你说的一次读取4kbyte,但个人觉得这样也是可以的了,呵呵
      

  4.   

    谢谢各位的回答,我稍微解释一下我要做的是Secret Sharing,把一个数字secret通过多元一次方程的方法分成n分,其中k (k<n)分可以恢复这个数字
    (详细请搜索 adi shamir how to share a secret) 现在我想把这个方法运用到share任意文件。我的思想是读4k byte(或者其他大小)的文件,让后将这4k转为 一个 大数,进行我需要的运算后将结果(也是一个大数)存到另外一个文件中我看到过类似的事先方法采用的是C++中的unsigned long,就是4 bytes 每次,请各位讨论一下哪一个效率高。谢谢(任意文件)
      

  5.   

    import java.io.*;import java.lang.*;import java.math.*;public class FileRead 
    {
    public static void main(String[] args) 
    {
    //Two file paths given by command line
    File inFile = new File(args[0]);
    File outFile = new File(args[1]); byte[] bArray = new byte[64]; //A byte array buffer
    try
    { //Two stream objects for read/write
    RandomAccessFile inStream = new RandomAccessFile(inFile, "r");
    RandomAccessFile outStream = new RandomAccessFile(outFile, "rw"); //read bytes to buffer
    if (!(inStream.read(bArray)==-1))
    System.out.println("Read OK"); BigInteger mBigIn = new BigInteger (bArray);
    System.out.println(mBigIn.toString()); //Similar operations like mBigIn = mBigIn.multiply(mBigIn);
    //Where the transformations will be bArray = mBigIn.toByteArray(); outStream.write(bArray); }
    catch (FileNotFoundException e)
    {
    System.out.println("File Not Found");
    }
    catch(IOException e) 
    {
    System.out.println("IOException");
    }
    }
    }