• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read n Modify Text file  RSS feed

 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have .txt file which I need to read linebyline and find the specific line n replace as different line and then save as same filename.
Could you please help me out
Thanks
Cheers
Jowsaki
 
paul wheaton
Trailboss
Posts: 22580
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big of a text file are we talking about?
For me, I have some utilities I've made for small text files. Read the whole file into a string array, make the changes, write the file back out.
For bigger files, you'll need an in stream and an out stream. Read the line, make changes if you need to, write the line. Repeat until done. Close files.
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its about 5 lines..
pls find below the format
COD-Type:image/gif
COD-Size:26241
COD-URL:http://xyz/230116.gifOD-Description:welcome ft
COD-Name:welcome ft
eg. image/gif need to be changed as image/jpeg
Pls give me an example.
Tks
Jowsaki
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which part is giving you trouble? Is it reading a file? Is it writing a file? Is it analyzing the contents of a String? Is it making a Java program in general? Is it all of it?
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the problem n Thanks for your reply.
I was having a problem with writing the updated line n rest of the lines, for which I saved as different filename n append the lines.
But my actual requiredment is that to save in the same filename, If you can pls alter my code n advise me.
Here is my code store as a different filename..
Thanks
Cheers
Jowsaki
public void updateCOD(String codfile)
{
System.out.println("Inside UpdateCOD ");
try
{
FileReader codFile = new FileReader(codfile);
BufferedReader br1 = new BufferedReader(codFile);
// Read the first line from the file.
String line1 = br1.readLine();
// As long as the line isn�t null, keep going.
while ( line1 != null )
{
// extract integers from a text file, then do the caculation.
System.out.println("Line1 is "+line1);
if(line1.trim().equals("COD-Type:image/gif"))
{
line1="COD-Type:image/jpeg";
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(codfile+".bak",true));
out.write(line1);
out.write("\n");
out.close();
} catch (IOException e) {
}
line1 = br1.readLine();
}
br1.close();
codFile.close();
}
catch(FileNotFoundException fne)
{
fne.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sham:
try the File.renameTo() method
Jamie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!