Results 1 to 3 of 3
  1. #1
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default problem with writing new data into file

    hello
    again I have problem with writing data into file for recursive method
    the problem is that this method doesn't write the new content in to file, keeps reading the old content of file it doesn't read the new content
    Java Code:
    static void Reordering_solution(FileWriter writer_rordering_out,File reordering){
    	 try { 
    
    ///////////////////////////////////////////////////////////////////////
    // some commands///////////////////////////////////////////////
    // writing into file//////////////////////////////////////////////
    
     for (int g = 0; g < test_after_filling_ordering.size(); g++) {
    				writer_rordering_out.write(test_after_filling_ordering.get(g).toString());
    				writer_rordering_out.write("\r\n");}
    		        writer_rordering_out.write("Empty");
    		        writer_rordering_out.write("\r\n");
    /////////////////////////////////////////////////////////
    // close writing /////////////////////////////////////
    
      writer_rordering_out.close();
    //////////////////////////////////////////////
    reading data written///////////////////////
    //////////////////////////////////////////////
        Scanner inFile_77= new Scanner(new FileInputStream(reordering));
        BufferedReader reader_reordering_sub = new BufferedReader(new FileReader(reordering));
        line_reodering = null;
        line_reodering = reader_reordering_sub.readLine();
        line_reodering=inFile_77.nextLine();
    
    ///////////////////////////////////////////////
    //some command to get new data//////////
    ///////////////////////////////////////////////
    
    // writing new data into file/////////////////
    /////////////////////////////////////////////
    
    		File re= new File("reordering");
    	 FileWriter writer_rodering_2 = new FileWriter("reordering");
    	 
    	while (inFile_77.hasNextLine()){
    		 line_reodering=inFile_77.nextLine();
    		 System.out.println(" the line is "+ line_reodering);
    		 writer_rodering_2.write(line_reodering);
    			   writer_rodering_2.write("\r\n");}
    
    ///////////////////////////////////////////////////////////////
    // calling recursive method////////////////////////////
    ///////////////////////////////////////////////////////////
    Reordering_solution(writer_rodering_2 ,re);
    
    }
    can you tell me what is the problem with writing the new data into file??

    plzzzzzzz I'm waiting for replies
    thanks

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you must close the reader before trying to write:

    Java Code:
    reader_reordering_sub.close ();

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Read/write to the same file dynamically is not safe at all. Best way is read the content at once and do write process later. But if your file size is too large then it's kind of difficult/problematic. So what you can do is, create a file with the same name in a different folder location and write to content as you did above.

Similar Threads

  1. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  2. Problem with writing unicode characters in a file
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 10:47 PM
  3. Writing to file problem
    By PeraPisar in forum Advanced Java
    Replies: 12
    Last Post: 09-16-2009, 11:00 AM
  4. Replies: 0
    Last Post: 03-05-2009, 06:18 AM
  5. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM

Posting Permissions

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