2014-07-16 53 views
0

這是我的java代碼的一部分,我想要從文件中讀取字節數,我在每個循環中增加4個字節,因爲整數的大小是4個字節,但這段代碼工作不正常,這段代碼的循環超出了文件的大小。請解決問題。正在讀取文件的進度

import java.io.File; 
import java.util.Scanner; 



public class S { 


    public S() throws Exception { 

     Scanner sc = new Scanner(new File("file.txt")); 

     for(int i=0; sc.hasNext(); i+=4) { 
      sc.nextInt(); 
      System.out.println("Read = "+i+" bytes"); 
     } 

     System.out.println("done"); 
    } 


    public static void main(String arg[]) throws Exception { 
     new S(); 
    } 

} 
+3

'nextInt()'將讀取的數,這可能是個字節的可變數量,這取決於數多長時間是在字符的字符串表示。 – rgettman

+0

你可以發佈一個file.txt的例子嗎? – slartidan

+0

要獲取讀取字節數,您可以鉤住/裝飾文件的InputStream。 – qqilihq

回答

1

掃描儀不適用於這種用例。掃描儀將讀取的數據超出其目前使用的 - 如果你跟蹤寫入字節,比你會得到預期的其他結果:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Scanner; 



public class S { 


    private final static class ProgressStream extends FileInputStream { 
     private int bytesRead; 

     private ProgressStream(File file) throws FileNotFoundException { 
      super(file); 
     } 

     @Override 
     public int read() throws IOException { 
      int b = super.read(); 
      if (b != -1) 
       bytesRead++; 
      return b; 
     } 

     @Override 
     public int read(byte[] b) throws IOException { 
      int read = super.read(b); 
      if (read != -1) 
       bytesRead += read; 
      return read; 
     } 

     @Override 
     public int read(byte[] b, int off, int len) throws IOException { 
      int read = super.read(b, off, len); 
      if (read != -1) 
       bytesRead += read; 
      return read; 
     } 
    } 

    public S() throws Exception { 

     ProgressStream progressStream = new ProgressStream(new File("file.txt")); 

     Scanner sc = new Scanner(progressStream); 

     while (sc.hasNext()) { 
      sc.nextInt(); 
      System.out.println("Read = " + progressStream.bytesRead + " bytes"); 
     } 

     System.out.println("done"); 
    } 


    public static void main(String arg[]) throws Exception { 
     new S(); 
    } 

} 

輸入,文件

123 456 789 

它輸出

Read = 13 bytes 
Read = 13 bytes 
Read = 13 bytes 
done 

所以你將不得不通過自己的實現類似掃描儀的功能...

import java.io.File; 
import java.io.FileInputStream; 



public class S { 

    public S() throws Exception { 

     FileInputStream stream = new FileInputStream(new File("file.txt")); 

     int b; 
     StringBuilder lastDigits = new StringBuilder(); 
     int read = 0; 
     while ((b = stream.read()) != -1) { 
      read++; 
      char c = (char) b; 
      if (Character.isDigit(c)) { 
       lastDigits.append(c); 
      } else if (lastDigits.length() > 0) { 
       System.out.println("found int "+Integer.parseInt(lastDigits.toString())); 
       System.out.println("Read = "+read+" bytes"); 
       lastDigits = new StringBuilder(); 
      } 
     } 

     if (lastDigits.length() > 0) { 
      System.out.println("found int "+Integer.parseInt(lastDigits.toString())); 
      System.out.println("Read = "+read+" bytes"); 
     } 

     System.out.println("done"); 
    } 


    public static void main(String arg[]) throws Exception { 
     new S(); 
    } 

} 

將輸出

found int 123 
Read = 4 bytes 
found int 456 
Read = 8 bytes 
found int 789 
Read = 13 bytes 
done 
+0

爲什麼你在while循環的末尾添加if(lastDigits.length()> 0)? – Shafu