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

    Default reading from input file and then write on it

    hello everyone
    I need to read from an input file and then write to the same file??

    can I do this?
    my code is the following
    Java Code:
         Scanner inFile2 = new Scanner(new FileInputStream(file2));
    			BufferedReader reader2 = new BufferedReader(new FileReader(file2));
    			//BufferedWriter writer2 = new BufferedWriter(new FileWriter(file2));
    			line2=null;
    	        line2 = reader2.readLine() ;
    but when I write the above code and then start reading the lines from the file I have got blank file
    can you please help me
    what is the problem of defining the reading and writing file

    thanks alot

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    new FileWriter(filename,true);
    FileWriter (Java 2 Platform SE v1.4.2)
    I die a little on the inside...
    Every time I get shot.

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

    Default

    thanks alot
    but using this method I can read lines and then write ( or adding )some lines on the file
    I want to read and then clear the file and then write new lines how can I do this??

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sara12345 View Post
    thanks alot
    but using this method I can read lines and then write ( or adding )some lines on the file
    I want to read and then clear the file and then write new lines how can I do this??
    Just open the file for writing (don't use the two argument constructor); the previous content will be lost then.

    kind regards,

    Jos

  5. #5
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    sorry but I didn't get what you saying?? I need to read the contents of file and then clear it and then write new contents on this file
    I have used the following code:
    Java Code:
    Scanner inFile2 = new Scanner(new FileInputStream(file2));
    			BufferedReader reader2 = new BufferedReader(new FileReader(file2));
    			line2=null;
    	        line2 = reader2.readLine() ;
    			
    	      
    	        FileWriter writer2= new FileWriter(file2,true);
    can you tell me what is the wrong in this code ?? because using the above code I can only read contents and then write on this file without clearing the previous content
    thanks alot

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    but if I read and then close the file and then open it for writing it will also add the new lines to the previous content it will not clear the pervious content may be I did some thing wrong with code
    My code is the following:
    Java Code:
     Scanner inFile2 = new Scanner(new FileInputStream(file2));
    			BufferedReader reader2 = new BufferedReader(new FileReader(file2));
    			line2=null;
    	        line2 = reader2.readLine() ;
    			
    // doing reading the file
    
    
    
    inFile2.close();
                 
                  
                  FileWriter writer2= new FileWriter(file2,true);
    	              
    			
    	      	
    	      writer2.write("sum is " );
    	          
            
    	         writer2.close();
    can you please tell me what is the wrong??
    thanks alot

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Read my previous reply again and change your last line to this:

    Java Code:
     FileWriter writer2= new FileWriter(file2);
    Reading the API documentation for that FileReader class might help too.

    kind regards,

    Jos

  9. #9
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    ok thanks the problem was the true
    thanks alot for your reply:)
    my problem is solved

  10. #10
    Varun12345 is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    hi sara

    Read the file first, close it, and only then open it for writing.

Similar Threads

  1. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 04:02 PM
  2. [SOLVED] Reading an input string?!
    By sfe23 in forum New To Java
    Replies: 6
    Last Post: 02-23-2009, 04:38 AM
  3. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  4. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 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
  •