Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Read and write file in Java

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read and write file in Java

    Read file is at the first part and write file is at the second part. Try it
    This example is at Learn Your Java From Here: Read and write file
    There are more example or Java learning in Learn Your Java From Here

    		// get which file you want to read and write
    		File file = new File("D://test.txt");
     
    		try {
     
    			// check whether the file is existed or not
    			if (file.exists()) {
     
    				// create a new file if the file is not existed
    				file.createNewFile();
    			}
     
    			// new a writer and point the writer to the file
    			BufferedWriter writer = new BufferedWriter(new FileWriter(file));
     
    			// writer the content to the file
    			writer.write("I write something to a file.");
     
    			// always remember to close the writer
    			writer.close();
    			writer = null;
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    		try {
     
    			// new a reader and point the reader to the file
    			BufferedReader reader = new BufferedReader(new FileReader(file));
     
    			String line = "";
    			System.out.println("Content of the file\n================================");
    			while ((line = reader.readLine()) != null) {
    				System.out.println(line);
    			}
    			reader.close();
    			reader = null;
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    Last edited by waiheng1986; March 11th, 2012 at 08:16 AM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Read and write file in Java

    Thanks for the tip. However the statements that set null reference to the reader and writer are redundant.

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM
  3. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  4. Read (using Odbc)and write using (POI api)
    By amruta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2010, 10:12 AM
  5. Writing in a file using Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: August 13th, 2008, 06:45 AM