English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i know i can read it using fileReader and write to it using FileWriter but how can i edit the String i get from using FileReader, or if this is not possible, how can i delete a file, so that i can then create a new file with correct information. any help would be greatfull thanxs :)

2006-08-02 16:18:47 · 1 answers · asked by dragongml 3 in Computers & Internet Programming & Design

1 answers

You can't really edit the file "in-place", though some platforms support editing a file "in-place" but for portability (which is what Java is best for!), you are better off deleting the original file and write another one with the same name. Here's how to do this:

String fileName = "myFileName.ext";
File file1 = new File(fileName);
//where the new data is outputed. it can be any name.
//this will hold a temporary name.
File file2 = new File(fileName+".temp");

//Read a chunk from file1 using FileReader,
//change the data to your choosing, then write the chunk to file2
//using FileWriter.

//You can create a while loop that will copy the data from file1 to file2, at the same time searching for the place in the file to modify, then write the new modified data to file 2. You can have this loop continue to read it until there is no more data from file1.

//Delete old file.
file1.delete();

//rename file2 to file1's name.
file2.renameTo(new File(fileName));

Hope this helps

2006-08-02 17:10:43 · answer #1 · answered by ComputerTechGeek 1 · 0 0

fedest.com, questions and answers