Results 1 to 3 of 3
  1. #1
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default Read and Write.. it was working

    Hi everyone,

    I have this code since last year and now I would like to reuse it again. but right now I run it and there was no error but also there is no result.

    Could anyone help me please ,,


    Java Code:
    import java.io.*;
    
    public class ReadAndWrite{
    
    
    
    public static void main(String args[]) throws IOException {
        if(args.length !=2)
    	return;
        BufferedReader inStream         
    	= new BufferedReader (new FileReader("C:\\Temp\\ReadAndWrite\\File1.txt"));// to open the read stream
       
        FileWriter outStream = (new FileWriter("C:\\Temp\\ReadAndWrite\\File2.txt"));// to open the write stream
    
        String line = inStream.readLine();// put the the sentance in Line
    	int r1 = 1;
    	while (line != null){
    	
     
    	    outStream.write(r1+ "" + line +"\n" );// to write 
    	    line = inStream.readLine(); // to read the next line
    	 r1++;
        }// end while
      
    
    	inStream.close(); // close the input file
    	outStream.close (); // close the output file                         
    
       
    }//end main
    }// end class

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    C:\\Temp\\ReadAndWrite\\File1.txt - does this file still exist?

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I fixed your code for you. Also make sure that these files exist.

    Java Code:
    import java.io.*;
    
    public class ReadAndWrite{
    
    public static void main(String args[]) throws IOException {
        if(args.length !=2)
    	return;
        BufferedReader inStream         
    	= new BufferedReader(new File("C:\\Temp\\ReadAndWrite\\File1.txt"));// to open the read stream
       
        BufferedWriter outStream = new BufferedWriter(new PrintWriter(new File("C:\\Temp\\ReadAndWrite\\File2.txt")));// to open the write stream
    
        String line;
    	int r1 = 1;
    	while ((line=inStream.readLine()) != null){
    	    outStream.write(r1+ "" + line +"\n" );// to write
    	 r1++;
        }// end while
    
    	inStream.close(); // close the input file
    	outStream.close (); // close the output file                         
    }//end main
    }// end class
    Last edited by Solarsonic; 03-28-2011 at 04:19 AM.

Similar Threads

  1. How to get sockt to write after read??
    By zardos in forum Networking
    Replies: 3
    Last Post: 03-04-2011, 12:00 PM
  2. Replies: 8
    Last Post: 08-06-2010, 08:41 AM
  3. XML read/write in Java
    By bogdy.laurentiu in forum XML
    Replies: 5
    Last Post: 05-31-2010, 06:08 PM
  4. How to read/write txt in Java
    By megalomitis in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 10:09 AM
  5. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 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
  •