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!

Using Scanner to read .java files - maintain structure

807601Jan 18 2008 — edited Jan 18 2008
Hi there,
I am currently reading in .java files and just printing their content out to the console.
My code is:
Scanner scnr = null;
    	try
    	{
    	    scnr = new Scanner( new File( SourceCodeFiles.get(0)) );
    	    while( scnr.hasNext() ){
    	        String word = scnr.next();
                System.out.println(word);
    	    }
    	        
    	}
    	catch( FileNotFoundException e ) 
    	{
    	     System.out.println( "File doesn't exist!" );
    	}
    	finally
    	{
    	      if( scnr != null )
    	         scnr.close();
    	}
This code prints out each word up until a space and then the next one etc etc.
I was wondering if there was a way to use the scanner to print out all the contents, maintaining their formatting structure?
If anyone has any ideas they would be much appreciated, thank you for your time,
beccy :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2008
Added on Jan 18 2008
4 comments
602 views