File f1 = new File(file);
FileInputStream fis = new FileInputStream(f1);
byte[] bytIn = new byte[(int) f1.length()];
for (int i = 0; i < f1.length(); i++) {
bytIn[i] = (byte) fis.read();
}这样读取速度太慢了,怎样提升读取速度

解决方案 »

  1.   

    采用缓存            File f1 = new File(file);
                FileInputStream fis = new FileInputStream(f1);
                BufferedInputStream bf = new BufferedInputStream(fis,1024*10);
                byte[] bytIn = new byte[(int) f1.length()];
                for (int i = 0; i < f1.length(); i++) {
                    bytIn[i] = (byte) fis.read();
                }
      

  2.   


    是不是写错了, bytIn[i] = (byte) fis.read(); 应该是 bf.read() 对吧
      

  3.   

    晕了,File的length()方法返回的不是long类型吗?进行for循环的时候还
    for(int i = 0; i<f1.length() ; i++) 
    这样写?
      

  4.   

    上面没仔细看 File f1 = new File(file);
            FileInputStream fis = new FileInputStream(f1);
            InputStreamReader r = new InputStreamReader(fis,"utf-8");//文件采用的编码方式
            BufferedReader bf = new BufferedReader(r,1024*10);
            String s = null;
            while((s =bf.readLine())!=null){
             System.out.println(s);
            }
      

  5.   

    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;public class FileOperation {
    public FileOperation() {

    }
    /** 
     * @param filePath 要读取的文件 
     * @param position  读取开始位置 
     * @param length  读取长度
     * @return  文件内容
     */
    public String GetStrFromFile(String fileName, int position, int length) {
    String strFromFile = "";
    File f = new File(fileName);
    if(f.exists()) {
    try {
    RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
    FileChannel fChan = raf.getChannel();
    MappedByteBuffer buffer = fChan.map(FileChannel.MapMode.PRIVATE, position, length);
    strFromFile = (new sun.misc.BASE64Encoder()).encodeBuffer(buffer);
    fChan.close();
    raf.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    } else {

    }
    return strFromFile;
    }

    }
      

  6.   

    说明下,我的源文件,是经过des加密后用Byte写入的我现在要将这个文件读取出来,并转换未byte如果先用readLine() 转换字符串,在转换byte ,在解密就失败,抛异常 
      

  7.   

    readLine() 转换字符串,在转换byte ,在解密就失败,抛异常 
      

  8.   

    我也有个关于FileInputStream类操作的问题,请各位高手指点一下?
    public void copy(File newFile, File oldfile) {
    try {
    newFile.createNewFile();
    FileInputStream fin = new FileInputStream(oldfile);
    FileOutputStream fout = new FileOutputStream(newFile);
    byte[] by = new byte[512];
    int rs=-1;
    while ( (rs=fin.read(by))>0) {
    fout.write(by, 0, rs);
    System.out.println(fin.read(by));
    }
    System.out.println("读取的信息:"+fin.read(by));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    以上函数中while()语句中相应位置换为以下语句:
    int rs=-1;
    rs=fin.read(by);
     while ( rs>0) {
     fout.write(by, 0, rs);
      System.out.println(fin.read(by));
     }
    这种情况下为什么会出现死循环?
      

  9.   

    rs=fin.read(by);执行后,rs为一常量,若读到的byte数大于0,则
    while ( rs>0) {
       ...
    }while条件恒成立,所以会出现死循环。