aspose file tools*
The moose likes Java in General and the fly likes Need to read and write data from file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need to read and write data from file." Watch "Need to read and write data from file." New topic
Author

Need to read and write data from file.

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Nov 18, 2006
Posts: 174
There's more than one way to skin a cat. Using a BufferedWriter, what kind of problems do you have?



Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
Ya i was having problem as i was using \n for line seperator.

Thanks a lot...it is working now...
 
wood burning stoves
 
subject: Need to read and write data from file.