3 Replies Latest reply: Jun 25, 2007 10:18 AM by 800435 RSS

    writing data in text file..

    807600
      1. check the file name in D:\\temp\\test.txt
      2. if it exists, then open file. if there is data, then read data. after the last record, it writes new data.
      3. if it does not exist, create text.txt file and write data into text.txt file.

      so far, I have a code..

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


      public class Main {
      public static void main(String[] args) {


      String filename="test";


      String format = "%1$-9s%2$-3s%3$-30s%4$-30s%5$-30s\n";

      String arraydata[] = { "John", "F.", "Kennedy", " ", "t", "John2", "F2.", "Kennedy2", " ", "t2"};

      String cnt_id ="2";
      int cnt_orderid = Integer.valueOf(cnt_id);

      try
      {
      PrintStream ps = new PrintStream(new FileOutputStream("D:\\temp\\"+filename+".txt"));
      for (int i=1; i<cnt_orderid; i++)
      {
      ps.println(String.format(format, (Object[])arraydata)); // write in test.txt file
      }

      ps.close();
      }
      catch (Exception e)
      {
      System.out.println(e);
      }
      }
      }

      my question is..
      1. how to check the file exists or not
      2. how to write the data in the new line after the last data in the file.