1

I (think) I'm processing a text file line by line until I find a specific token;

(Psuedo Code)

Scanner scanner = new Scanner(new FileReader("myTextFile.txt");
while (scanner.hasNext() {
    boolean found = process(scanner.nextLine();
    if (found) return;
}

Some of the files are huge. Does this code actually scan the file line by line or does either the Scanner or FileReader read the entire file into memory and then work it's way through the memory buffer line by line?

4

3 回答 3

3
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean found = false;
while ((line = br.readLine()) != null) {
     if(line.equalsIgnoreCase("Your string"))
       found = true;
}
于 2013-05-24T13:14:57.663 回答
1

你想要BufferedInputStream

public static void main(String[] args) {

        File file = new File("C:\\testing.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
            fis = new FileInputStream(file);

            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            while (dis.available() != 0) {
                System.out.println(dis.readLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                bis.close();
                dis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

资源

于 2013-05-24T13:15:32.987 回答
0
Path filePath = Paths.get("myTextFile.txt");
boolean found = false;
try (BufferedReader br = Files.newBufferedReader(filePath, CharSet.forName(<char-set-name>)){
   for (String line = br.readLine(); line != null; line = br.readLine()) {
     found = process(line);
     if (found){
        break;
     };
   }
}
于 2013-05-24T13:30:43.537 回答