13 Replies - 419 Views - Last Post: 18 January 2011 - 10:48 PM Rate Topic: -----

#1 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Editing a text file and the saving it again

Posted 18 January 2011 - 08:02 PM

I would like to know how I can edit my text file and then save it again.
 public static void writeToFile(Object itemC, Object itemA, JTextArea _resultArea , JFrame frame) {
    boolean donotoverwrite = true;
    File dataFile = new File("cart.txt");
    FileWriter out;
    BufferedWriter writeFile;
    Scanner input = new Scanner(System.in);
    try {
      out = new FileWriter(dataFile, donotoverwrite);
      writeFile = new BufferedWriter(out);
      writeFile.write("Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
      writeFile.newLine();
      writeFile.close();
      out.close();
      System.out.println("Data written to file.");
      readFile( _resultArea, frame );
    } catch (IOException e) {
      System.out.println("Problem writing to file.");
      System.err.println("IOException: " + e.getMessage());
    }
  }
   public static void readFile( JTextArea _resultArea , JFrame frame) {
  File textFile = new File("cart.txt");
  FileReader in;
  BufferedReader readFile;
  String lineOfText;
  String s = "";
    int lineCount = 0;

  try {
   in = new FileReader(textFile);
   readFile = new BufferedReader(in);
      while ((lineOfText = readFile.readLine()) != null ) {

  //     String kishore = lineOfText;
//       System.out.print(kishore);
  //     _resultArea.setText(kishore);
//        frame.setSize(600,200);
s = s + lineOfText;
    

       
      }
       _resultArea.setText(s);
      readFile.close();
      in.close();
 //     }
   } catch (FileNotFoundException e) {
   System.out.println("File does not exist or could not be found.");
   System.err.println("FileNotFoundException: " + e.getMessage());
  } catch (IOException e) {
   System.out.println("Problem reading file.");
      System.err.println("IOException: " + e.getMessage());
     }
 } 


Is This A Good Question/Topic? 0
  • +

Replies To: Editing a text file and the saving it again

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 08:32 PM

Do itemC and itemA have a toString() method ?
What do you pass to your method useless parameter like textArea and JFrame it just confuses everything
Was This Post Helpful? 0
  • +
  • -

#3 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:05 PM

Yes they have a tostring method.
I pass them because I need them to change them later. E.g. JLabel.setText("X");
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:12 PM

You realize that

_resultArea.setText(kishore);

will set your JTextField to

"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");

???

Assuming you uncommented your code
Was This Post Helpful? 0
  • +
  • -

#5 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:15 PM

Oh sorry, I meant to delete that commented stuff. Basically the JTextField will read everything on my .txt file because of the variable s.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:21 PM

So the JTextField will contains

"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");
"Your city destination is: " + itemA +"." + "The number of tickets you have booked to this destionation is: " +itemC+".");

???
Was This Post Helpful? 0
  • +
  • -

#7 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:23 PM

Well thats a Yes and No answer. In theory it would do that if I wrote that line 8 times (itemA would change each time).
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:27 PM

View PostCryanLo, on 18 January 2011 - 11:23 PM, said:

Well thats a Yes and No answer. In theory it would do that if I wrote that line 8 times (itemA would change each time).

Ya, but you will have the "The number of tickets you have booked to this destionation is: " crap many times in your JTextField which does not seem to me to be a desired behaviour
Was This Post Helpful? 0
  • +
  • -

#9 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:38 PM

I don't understand what you mean. If I want it to write 2 times the program writes it two times to the same file. (2 seperate lines tho)
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 09:45 PM

What I mean is that you probably want to write in your file itemC and itemA not all the comments that goes with it
so when you read back from your file you will just have to read these two items an put them back in the appropriate JTextField without all the useless stuff that are kind of comments as far as the program is concerned
Was This Post Helpful? 0
  • +
  • -

#11 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 10:00 PM

Oh I see, that is a really good idea!
But wouldn't there be issues when it came to reading the file? Like it wouldn't be able to distinguish the difference between itemC and itemA
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 10:05 PM

View PostCryanLo, on 19 January 2011 - 12:00 AM, said:

Oh I see, that is a really good idea!
But wouldn't there be issues when it came to reading the file? Like it wouldn't be able to distinguish the difference between itemC and itemA

They will be one after the other
so if odd line number exists (not end of file), it will contains an itemC and will be followed next line by an itemA
Was This Post Helpful? 0
  • +
  • -

#13 CryanLo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-January 11

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 10:20 PM

So when reading these strings what statements would I use to read them?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Editing a text file and the saving it again

Posted 18 January 2011 - 10:48 PM

Look at the documentation for whatever file input tool you are using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1