aspose file tools*
The moose likes Beginning Java and the fly likes Reading a file using Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a file using Scanner" Watch "Reading a file using Scanner" New topic
Author

Reading a file using Scanner

john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
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)

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

create a file named SelfEmployedA.txt in the folder from where you are running the program. If what I think is right, then it will then work.

Also please UseCodeTags to make your program readable...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
Thank you for the reply. I have placed a 'SelfEmployedA.txt' file in the same directory from which I am running the application. It also does not seem to want to work. I really am baffled.

//this is the Application program
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TSD2.java

//this is the file in the same directory
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\SelfEmployedA.txt


//these are picked up in the hard coded directory in the program (File f = new File("C:\\Installs\\..........\\TestDirectory"))
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\SelfEmployedA.txt
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TestDirectory\TestDirectoryOne
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
john smith wrote:Thank you for the reply. I have placed a 'SelfEmployedA.txt' file in the same directory from which I am running the application. It also does not seem to want to work. I really am baffled.

//this is the Application program
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\TSD2.java

//this is the file in the same directory
C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects\src\packagetwo\SelfEmployedA.txt
...


Nope, the program gets executed from "C:\Installs\Java\Workspace\NetBeans\Projects\MyProjects" which is where your 'SelfEmployedA.txt' file should be.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39810
    
  28
If you are facing that sort of problem, turn NetBeans off and use a simple class called from the command line.
Beware: when you call nextInt and enter "123return" the Scanner will interpret the "123" bit as the int and the return as a line which is blank. If you go back through this forum you will find at least two threads in the last week where people have problems with Scanners: try here and here (towards the bottom of the page) and maybe even here, and see whether there is anything helpful there.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reading a file using Scanner