Results 1 to 2 of 2
  1. #1
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default write to the same .txt file

    Hello,

    I hope some one can help me to figure this out:

    I have one class called 'commonMethods.java' and I have another class 'testCommonMethods.java'.

    In 'commonMethods.java' I created these class variables:
    Java Code:
    static String currDate= "" + c.get(Calendar.YEAR) +
                 "-" + df2.format(c.getTime())+
                 "-" + c.get(Calendar.DAY_OF_MONTH) +
                 "-" + c.get(Calendar.HOUR_OF_DAY) +
                 "hr-" + c.get(Calendar.MINUTE) ;
    
    	 static File f = new File("OrderNumbers_"+currDate+".txt");

    and then in my 'commonMethods.java' class I created this method:

    Java Code:
        	 
          public  static void sendOrderNumToFile(WebDriver driver,String orderNum)
         {
        	 	try {
    				PrintWriter pw = new PrintWriter(new FileWriter(f, true));
    				
    				pw.println(orderNum);
    				pw.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    In my second class 'testCommonMethods.java' I am calling this method
    Java Code:
    commonMethods.sendOrderNumToFile(driver,orderNumber);
    Every time I use this method I get a new txt file created and String is printed inside of it.
    I want to get only ONE txt file and every time I call this method it should print String into the SAME txt file.

    Questions:
    1. Am I getting multiple txt files because of this line:
    Java Code:
    PrintWriter pw = new PrintWriter(new FileWriter(f, true));
    2. I thought that since I created my file as ' static File f' it means that there is going to be only one 'f' no matter how many times I invoke the method. Why am I wrong?

    Note: Webdriver is an interface that I use Generated Documentation (Untitled)

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,773
    Rep Power
    5

    Default Re: write to the same .txt file

    Well the static variable is not final, so it is very possible to simply assign a new File object to that static variable. If you get multiple text files, which means they must have different filenames, that must be happening somewhere.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2012, 10:00 AM
  2. Text read and write between *.txt file and *.xls file
    By lemontree45 in forum New To Java
    Replies: 6
    Last Post: 08-12-2011, 02:08 AM
  3. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 5
    Last Post: 02-05-2009, 10:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •