How Can I Reset The File Pointer to the Beginning of the File in Java?

前端 未结 10 1504
孤城傲影
孤城傲影 2021-02-19 20:17

I am writing a program in Java that requires me to compare the data in 2 files. I have to check each line from file 1 against each line of file 2 and if I find a match write the

相关标签:
10条回答
  • 2021-02-19 20:57

    I believe that you could just re-initialize the file 2 file reader and that should reset it.

    0 讨论(0)
  • 2021-02-19 21:02

    Just a quick Question. can't you keep one object pointed at the start of the file and traverse through the file with another object? Then when you get to the end just point it to the object at the beginning of the file(stream). I believe C++ has such mechanisms with file I/O ( or is it stream I/O)

    0 讨论(0)
  • 2021-02-19 21:06

    I think the best thing to do would be to put each line from file 1 into a HashMap; then you could check each line of file 2 for membership in your HashMap rather than reading through the entire file once for each line of file 1.

    But to answer your question of how to go back to the beginning of the file, the easiest thing to do is to open another InputStream/Reader.

    0 讨论(0)
  • 2021-02-19 21:07

    As others have suggested, you should consider other approaches to the problem. For the specific question of returning to a previous point in a file, java.io.FileReader inherits mark() and reset() methods that address this goal.

    0 讨论(0)
提交回复
热议问题