Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-09-2009
Member
 
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?
Reply With Quote
  #2  
Old 02-09-2009
Member
 
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);
		}
	}	
}
Reply With Quote
  #3  
Old 02-09-2009
Member
 
Join Date: May 2008
Posts: 2,008
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();
}
Reply With Quote
  #4  
Old 02-09-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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();
    }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:33 AM.