Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Raghavan Srinivasan
Greenhorn
+ Follow
1
Posts
0
Threads
0
Cows
since Apr 01, 2002
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (1/100)
Number Threads Started (0/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (1/10)
Number Threads Started (0/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Raghavan Srinivasan
How do I read and write a binary file?
Here is a simple example for reading from a text file line by line.
/* * TextLineReadExample.java * * Created on April 2, 2002, 10:29 AM */ import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; /** * A simple example to demonstrate the reading from a text file line by line. * @author Raghavan */ public class TextLineReadExample { private BufferedReader m_fileReader; private String m_fileName; /** Creates a new instance of TextLineReadExample */ public TextLineReadExample() { m_fileReader = null; } /** * Sets the fileName to read * @param p_fileName File name to read */ public void setFileName( String p_fileName ) throws FileNotFoundException, IOException { m_fileName = p_fileName; if ( m_fileReader != null ) { m_fileReader.close(); //Close the file already opened. } m_fileReader = new BufferedReader( new FileReader( p_fileName ) ); } /** * Reads the file content line by line and prints it to the standard output */ public void printFileContentLineByLine() throws IOException { if( m_fileReader == null ) { return; } String aLine = m_fileReader.readLine(); while( aLine != null ) //null is returned when EOF is reached { System.out.println( aLine ); System.out.println(); aLine = m_fileReader.readLine(); } } /** * Closes the file */ public void close() throws IOException { if( m_fileReader != null ) { m_fileReader.close(); } } /** * * @param args the command line arguments */ public static void main(String[] args) { TextLineReadExample myExampleObj = new TextLineReadExample(); try { myExampleObj.setFileName( "TextLineReadExample.java" ); myExampleObj.printFileContentLineByLine(); myExampleObj.close(); } catch( Exception e ) { e.printStackTrace(); } } } // End of Example
show more
21 years ago
Beginning Java