Results 1 to 4 of 4
  1. #1
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Read/Write Files

    Hi there, I have a project on my mind at the moment that would require reading and writing to/from a file for my robot. I have looked on the net and examples seem to be fairly confusing. Any one know a simple way of explaining read to/from file?

    Thanks
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    reading form a file means using
    Scanner or BuffereredReader right?

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    There are several ways of writing/reading to a file and they are lucid. BufferedReader and BufferedWriter is one way:

    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class I_slash_O {
    	public static void main(String[] args) throws IOException {
    		//no need to catch exception because we created the file;
    		//thus, we know it exists.
    		
    		String filename = "example.txt";
    		
    		//------------------------------------------------
    		//Write to a file
    		//------------------------------------------------
    		
    		FileWriter file = new FileWriter(filename);
    		BufferedWriter wrappedFile = new BufferedWriter(file);
    		
    		wrappedFile.write("Hello World!");
    		wrappedFile.newLine();
    		wrappedFile.write("This is Lil_Aziz1");
    		wrappedFile.close();
    		
    		//------------------------------------------------
    		//Read from the same file we wrote too
    		//------------------------------------------------
    		
    		FileReader read = new FileReader(filename);
    		BufferedReader wrappedReader = new BufferedReader(read);
    		
    		String contents;
    		
    		//Read line by line.
    		while(wrappedReader.ready()) {
    			contents = wrappedReader.readLine();
    			System.out.println(contents);
    		}
    
    		
    		wrappedReader.close();
    		
    		
    	}
    }
    I highly recommend you read Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Last edited by Lil_Aziz1; 02-06-2010 at 07:45 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Lol_Aziz1 thanks for your explanation. Seems easy to understand.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

Similar Threads

  1. How to read/write txt in Java
    By megalomitis in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 10:09 AM
  2. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 02:54 PM
  3. How Read and Write XMl files using Java
    By tjs in forum SWT / JFace
    Replies: 0
    Last Post: 02-23-2009, 12:19 PM
  4. [SOLVED] Read/write files using applets
    By coolFrenzi in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2008, 10:45 PM
  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
  •