Is there a way to edit data on a text file without

2019-09-10 13:31发布

Suppose I've a couple of objects' values stored on a text file. In the beginning, there will be some values which will have the same value, for instance, all students by default will have 0 age. Now if I want to make an edit in the age of one student using the conventional file handling approach, I'll end up making changes to all other students who have 0 age, while writing my data onto the temporary file. Thus, I was hoping that if there is a better way to make changes to a file using file handling in java. Just to give an example of the issue at hand consider the following text file

Edsger

Dijkstra

123

72 years


Ruth

Dijkstra

12345

29 years

The line indicates a space between the age and the name. Now, my task is to construct a program where a user can change any detail, such as the First Name, surname, roll_number or the age. As you can see from the example given above, two people can share some data that is common. In this case, it is the surname. However, the roll number (123,12345) will always be unique. The problem comes when you have to change similar data. Suppose the user wants to edit the surname. Then by I would create a temporary file which would hold this data and later I would read this data with some conditions to it. So the code might look like this: Note: This data is stored at a known location "abc.txt".

            BufferedReader br=new BufferedReader(new FileReader("abc.txt")); 
            BufferedWriter bw=new BufferedWriter(new FileWriter("Temp.txt"));
            String a=br.readLine();
             while(a!=null)
            { bw.write(a);
              bw.newLine();
              a=br.readLine();
            }
            br.close();
            bw.close();
            BufferedReader br1=new BufferedReader(new FileReader("Temp.txt"));
            BufferedWriter bw1=new BufferedWriter(new FileWriter("temp.txt"));
            String b=br1.readLine();
            while(b!=null)
                    { 
            if(b.equals(requested_surname))
                        bw1.write(w);//w is the String that holds the altered surname as desired by the User, for the sake of an example say it is Euler
                        else
                        bw1.write(b);
            bw1.newLine();
            b=br1.readLine();
            }
            bw1.close();
            br1.close();
            f.delete();

As a result the Original text file "abc.txt" will show something like this:-

Edsger

Euler

123

72 years


Ruth

Euler

12345

29 years

Now this will be a bungling problem as I intend to change only Ruth's surname! I know that this is slightly different from what I initially asked, but I think that if I could target the line below "Ruth", I can make the desired changes.

Please Help...

标签: java file
2条回答
成全新的幸福
2楼-- · 2019-09-10 14:00

There are several approaches to do this.

You could store the data in a csv file, 1 line for each object:

123,Edsger,Dijkstra,72
12345,Ruth,Dijkstra,29
4567,Ruth,Euler,27

Then, on program start-up read all the objects in memory (in a structure) for easy access. On program exit or save, write everything back to the file (assuming the number of objects isn't really big - i.e. not millions).

Another way is to store every field of the object as a fixed width value:

123    Edsger      Dijkstra    72
12345  Ruth        Dijkstra    29
4567   Ruth        Euler       27

That way changes to the data can easily be written 'in place' in the file. You only have to make sure the fields don't exceed the maximum size. The number fields could even be in binary format if needed.

With fixed width, or an exact size for each object, it is easy (and faster) to look up a certain object or roll number: since the size of the objects is known, file seek can be used to jump directly to the beginning of each object - no parsing is needed.

Note: the objects don't need to be on a separate line in this case (I've done it for clarity) - but if they are, newlines (could be \r or \r\n or \n) will have to be added to the size of the objects.

Of course, searches for a certain person/object should always be done on a unique ID, in this case roll number, never on the name.

查看更多
你好瞎i
3楼-- · 2019-09-10 14:16

File can be thought as a character array.

  char[] file    = ... // file (on disk)
  char[] newData = ... // data to be written
  int pos = ...        // the position in the file to write to
  for (i=0; i<newData.; i++) { 
     file[pos+i] = newData[i];
  }

You can particularly make use of seek(). Check this out as well: http://docs.oracle.com/javase/tutorial/essential/io/rafs.html

查看更多
登录 后发表回答