Hi, I'm trying to learn using Scanner to analyse a file. The running of the program does not work. I have included below code, directory structure and out. Could someone help please. I am using Netbeans.
*****************************************************************************************
************************************ The Code *******************
******************************************************************************************
package packagetwo;
import java.io.*;
import java.io.File;
import java.util.Scanner;
/**
*
* @author user
*/
public class TSD2 {
public static void main(
String[] args){
String[] fileList = null;
//use this for
testing purposes
File f = new File("C:\\Installs\\Java\\Workspace\\NetBeans\\" +
"Projects\\MyProjects\\src\\packagetwo\\TestDirectory");
//is this main path a directory
if (f.isDirectory()){
System.out.println("");
System.out.println("");
System.out.println("it is a directory");
fileList = f.list();
for(int i=0;i<fileList.length;i++){
File firstFile = new File(fileList[i]);
System.out.println(firstFile.toString());
try{
if (!(firstFile.isDirectory())){
System.out.println("it is not a directory");
Scanner scannedFile = new Scanner(firstFile);
System.out.println("done");
}
if ((firstFile.isDirectory())){
String name = firstFile.toString();
System.out.println(name + "is a directory .... " + "\n");
}
}catch(FileNotFoundException fne){fne.printStackTrace();}
}
}
}
}
*****************************************************************************************
************************************ The Directory Structure of the test file *******************
******************************************************************************************
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\SelfEmployedA.text
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\TestDirectoryOne
******************************************************************************
************************************ The Output ******************************
******************************************************************************
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src>
java packagetwo.TSD2
it is a directory //this is just a test statement output - main directory
SelfEmployedA.txt //this is just a test statement output - found first file
it is not a directory //this is just a test statement output - confirming it is not a directory
java.io.FileNotFoundException: SelfEmployedA.txt (The system cannot find the fil
e specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at packagetwo.TSD2.main(TSD2.java:43)
TestDirectoryOne
it is not a directory
java.io.FileNotFoundException: TestDirectoryOne (The system cannot find the file
specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at packagetwo.TSD2.main(TSD2.java:43)