Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Need to read and write data from file.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic