how to modify the contents of a file

Example::
If there is a file with

hello

#how

are

you

Then how to delete the # and maintain the same file

Write it to another file, delete the old and rename the new to the old.

In this "special" circumstance you actually could overwrite the five characters
[pre]"#how"[/pre] with [pre]"how "[/pre] (note the extra space) using RandomAccessFile, but that is not, of course, the same as "deleting" the "#".

File are simply sequential bytes on the disk. The only way to "delete" a character from that is to shift all the bytes behind it forward, and the only way to do that is to read and rewrite them. Since you are talking about a single character, and not a block of at least 4k, all of those read and write calls to the disk would be extremely ineffecient. It is simply easier, and much more effecient, and the standard, accepted practice to simply read it and write a new file and replace the old with the new. When the files are guaranteed to be small, you can always read them in completely, modify that in memory, and then write out to the same filename (note, that, technically, this still creates a new file, though, simply overwriting the old filesystem reference directly).

Just so you know, even all of these mainstream file editors do this. They do not, because they can not (effeciently), "modify" the file "in place".

thnx....but could u be a bit more elementary ........im really new to this stuff

If it is a text file, BufferedReader, if it is not explicity a text file BufferedInputStream, like always for reading files. For "removing" the "#", the replace method of String, for writing the file BufferedWriter or BufferedOutputStream using the same consideration, respectfully, as used for Reader/InputStream. Now at least try to write something.

Edited 5 Years Ago by masijade: n/a

Comments
Crystal clear

import java.io.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedReader br =new BufferedReader(new FileReader("textfile.txt"));
String str;
while((str=br.readLine())!=null)
{

if((str=br.readLine()).equals("#exit 0"))
{
str.replace("#"," ");
System.out.println(str);
}

}
}
}

now im jus trying to print it in the command line at first.......but its actually not recognizing the "#exit 0" thing at all

import java.io.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedReader br =new BufferedReader(new FileReader("textfile.txt"));
String str;
while((str=br.readLine())!=null)
{
String p=str.replaceAll("#exit 0","exit 0");
System.out.println(p);

}
}
}

now im not able to write it to the file....

import java.io.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedReader br =new BufferedReader(new FileReader("textfile.txt"));
String str;
BufferedWriter br1= new BufferedWriter(new FileWriter("textfile.txt"));
while((str=br.readLine())!=null)
{
String p=str.replaceAll("#exit 0","exit 0");
br1.write(p);
}
}
}

the output is blank
i mean the file is empty

import java.io.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedReader br =new BufferedReader(new FileReader("textfile.txt"));
String str;
BufferedWriter br1= new BufferedWriter(new FileWriter("textfile1.txt"));
while((str=br.readLine())!=null)
{
String p=str.replaceAll("#exit 0","exit 0");
br1.write(p);
br1.newLine();
br1.flush();
}
br1.close();
File f = new File("textfile.txt");
boolean bol = f.delete();
File f1 = new File("textfile1.txt");
f1.renameTo(new File("textfile.txt"));
}
}

i managed to write it to another file but im not able to do the last process which is renaming the textfile1 to textfile

Edited 3 Years Ago by mike_2000_17: Fixed formatting

import java.io.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedReader br =new BufferedReader(new FileReader("textfile.txt"));
String str;
BufferedWriter br1= new BufferedWriter(new FileWriter("textfile1.txt"));
while((str=br.readLine())!=null)
{
String p=str.replaceAll("#exit 0","exit 0");
br1.write(p);
br1.newLine();
br1.flush();
}
br1.close();
File f = new File("textfile.txt");
boolean bol = f.delete();
File f1 = new File("textfile1.txt");
f1.renameTo(new File("textfile.txt"));
}
}

i managed to write it to another file but im not able to do the last process which is renaming the textfile1 to textfile

Ok......so now thts done.......could u please explain why we cannot modify the file in the file itself.......i mean isnt it a waste of resources if we rename the file and delete the old file.

This question has already been answered. Start a new discussion instead.