Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading first word from text file

807600Nov 6 2007 — edited Dec 29 2007
Hello all,

I created a program which I can type in a line and store it into a file. What I need my next program to do is read and display just the first word from each line on the text file.

Here is what I have:
import java.io.*;

public class ReadFromFile
{
	
public static void main (String args[])
	{
	FileInputStream firstWord;		

		try
		{
		    // Open an input stream
		    firstWord = new FileInputStream ("FileofWords.txt");

		    // Read a line of text
		    System.out.println( new DataInputStream(firstWord).readLine() );

		    // Close our input stream
		    firstWord.close();		
		}

		{
			System.err.println ("Unable to read from file");
			System.exit(-1);
		}
	}	
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 26 2008
Added on Nov 6 2007
9 comments
916 views