All of this is JAVA in NETBEANS 12 Assuming the file words.txt holds the followi
ID: 3773371 • Letter: A
Question
All of this is JAVA in NETBEANS
12 Assuming the file words.txt holds the following data:
CSI Java Illuminated
What is the output of this code sequence:
If the file is found?
If the file is not found?
Scanner file = new Scanner(new File( “words.txt” ) );
String results = “”;
While ( file.hasNext( ) )
{
String s = file.next( );
result += s;
System.out.println( “result is “ + result );
File.close( );
Catch ( FileNotFoundException fnfe)
System.out.println( “Unable to find words.txt” );
Catch ( IOException ioe )
ioe.printStackTrace( );
46 Write a program that reads a file and counts how many lines it contains. Page 807
Explanation / Answer
/**The java file FileProgram that reads words.txt file
* and read strings word by word and add to the result string
* and print to console
* */
//FileProgram.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileProgram
{
public static void main(String[] args)
{
try
{
//Create a Scanner class
Scanner file = new Scanner(new File( "words.txt"));
//Set a result to empty
String result = "";
//read string till end of file
while ( file.hasNext( ))
{
String s = file.next( );
//append string s to result
result += s;
//print text in result
System.out.println( "result is " + result );
}
}
catch (FileNotFoundException ioe)
{
System.out.println("Unable to find words.txt");
ioe.printStackTrace( );
}
}
}
-------------------------------------------------------------------------------------
words.txt
CSI Java Illuminated
--------------------------------------------------------------------------------
Sample output with text file "words.txt" exists :
result is CSI
result is CSIJava
result is CSIJavaIlluminated
------------------------------------------------------------------------------------------
Sample output without text file "words.txt":
Unable to find words.txt
java.io.FileNotFoundException: words.txt (The system cannot find the file specified)
---------------------------------------------------------------------------------------------
Program 2:
/**The java program CountFile that reads a text file words.txt
* and finds the number of lines in the words.txt and prints
* to console*/
//CountFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CountFile
{
public static void main(String[] args)
{
try
{
//Create an instance of Scanner class and open
//a text file called words.txt
Scanner file = new Scanner(new File( "words.txt"));
//Set linecount to zero
int linecount=0;
//Read lines from text file till end of file
while ( file.hasNextLine())
{
//read line in string line
String line=file.nextLine();
//increment the linecount by one
linecount++;
}
//print number of lines in words.txt
System.out.println("Number of Lines in words.txt file: "
+linecount);
//close Scanner file object
file.close();
}
catch (FileNotFoundException ioe)
{
System.out.println("Unable to find words.txt");
ioe.printStackTrace( );
}
}
}
------------------------------------------------------------------------------------
words.txt
CSI Java Illuminated
------------------------------------------------------------------------------------
Sample output:
Number of Lines in words.txt file: 1