Results 1 to 4 of 4

Thread: How to read and write files in Java

  1. #1
    Join Date
    Jun 2009
    Posts
    3,620

    How to read and write files in Java

    I am creating a program where the code is able to read a user specified file and also capable of writing few lines in the same file. But how will this be possible using JAVA? How can I code Java for read and write functions? Can anyone explain me the working of this module?

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: How to read and write files in Java

    Code:
    import java.io.*;
    
    public class ReadWriteFile
    {
    	public static void main (String args[])
    	{
    		FileInputStream input;		
    		FileOutputStream output;		
    		try
    		{
    		    input = new FileInputStream ("testfile.txt");
    		    System.out.println( new DataInputStream(input).readLine() );
    		    input.close();		
    		}
    		try
    		{
    		    output = new FileOutputStream ("testfile.txt");
    		    new PrintStream(output).println ("hello world!");
    		    output.close();		
    		}
    		catch (IOException e)
    		{
    			System.err.println ("Unable to read/write file");
    			System.exit(-1);
    		}
    	}	
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to read and write files in Java

    Here is the code for reading the from the file:

    Code:
    public void read(String myFile) throws IOException{
    	FileReader readFile = null;
    	BufferedReader readBuffer;
    	String line;
    	readFile = new FileReader(myFile);
            readBuffer = new BufferedReader(readFile);
            line="";   
            while(line != null){        
            	line = readBuffer.readLine();		
    		System.out.println(line);
            }        
            readBuffer.close();
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to read and write files in Java

    Here is what I found on SUN.com. To open a file for reading, you can use the newInputStream method. This method returns an unbuffered input stream for reading bytes from the file.

    Code:
        Path file = ...;
        InputStream in = null;
        try {
            in = file.newInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException x) {
            System.err.println(x);
        } finally {
            if (in != null) in.close();
        }

Similar Threads

  1. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  2. How to read 2 text files and write it into one
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:05 PM
  3. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  4. How to read and write Excel files in PHP ?
    By Sudra in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 05:18 PM
  5. How to create read write lock in java
    By AlienKing in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 08:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,735,480.40724 seconds with 17 queries