Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 them to a third file. After I read to the end of file 2, how do I reset the pointer to the beginning of the file?

public class FiFo {
    public static void main(String[] args) 
    {
        FileReader file1=new FileReader("d:\\testfiles\\FILE1.txt");
        FileReader file2=new FileReader("d:\\testfiles\\FILE2.txt");
        try{
            String s1,s2;
            while((s1=file1.data.readLine())!=null){
                System.out.println("s1: "+s1);
                while((s2=file2.data.readLine())!=null){
                    System.out.println("s2: "+s2);
                }
            }
            file1.closeFile();
            file2.closeFile();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class FileReader {
    BufferedReader data;
    DataInputStream in;

    public FileReader(String fileName)
    {
        try{
            FileInputStream fstream = new FileInputStream(fileName);
            data = new BufferedReader(new InputStreamReader(fstream));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    } 

    public void closeFile()
    {
        try{
            in.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
like image 902
Shaz Avatar asked Feb 09 '10 04:02

Shaz


People also ask

How do I change the file pointer to the beginning of a file?

fseek(fptr, 0, SEEK_SET); to reset the pointer to the start of the file. You cannot do that for stdin . If you need to be able to reset the pointer, pass the file as an argument to the program and use fopen to open the file and read its contents.

Which of the following methods will reset a file pointer to the start of a file?

If you want to reset the pointer to the beginning of the file, you need to call the Seek() method on the file.

How do you go back to the beginning of a file in C?

C library function - rewind() The C library function void rewind(FILE *stream) sets the file position to the beginning of the file of the given stream.

How can we move the file pointer at the end of file?

fseek() is used to move file pointer associated with a given file to a specific position. position defines the point with respect to which the file pointer needs to be moved. It has three values: SEEK_END : It denotes end of the file.


2 Answers

I believe RandomAccessFile is what you need. It contains: RandomAccessFile#seek and RandomAccessFile#getFilePointer.

rewind() is seek(0)

like image 99
Gennady Shumakher Avatar answered Sep 18 '22 10:09

Gennady Shumakher


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.

like image 38
danben Avatar answered Sep 19 '22 10:09

danben