• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to read and write data from file.

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to read a data from a file and after making modifications in the data i want to write it back in the same file.

I have read the data and stored it in some temporary structure now after making modification now i want to write it back into the same file.

Can someone let me know how can i do that.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problems are you having ? If you know how to read a file, it's not that hard to work out how to write to a file. Show us what you've tried so far and in what way it's not working.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now after making modifications in some attributes i want to write it back in the same out.txt instead of writing in a seperate file of creating a seperate file.


import java.io.*;
import java.util.*;

public class Test
{
public static void main(String[] args)
{
try
{
String fileName="c:\\out.txt";
if ((fileName == null) || (fileName == ""))
throw new IllegalArgumentException();
String line;
ArrayList file = new ArrayList();
FileReader fr = new FileReader(fileName);
BufferedReader in = new BufferedReader(fr);
if (!in.ready())
throw new IOException();
while ((line = in.readLine()) != null) {
file.add(line);
}
in.close();
}
catch (IOException e)
{
System.out.println(e);
}

}
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pintoo saluja wrote:Now after making modifications in some attributes i want to write it back in the same out.txt instead of writing in a seperate file of creating a seperate file.


There's no problem there. You've read the contents into memory and closed the input file, so there's nothing stopping you from writing the modified data back out to the same file.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's more than one way to skin a cat. Using a BufferedWriter, what kind of problems do you have?

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya i was having problem as i was using \n for line seperator.

Thanks a lot...it is working now...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic