Skip to main content

Reading from a file

1 reply [Last post]
rinc
Offline
Joined: 2009-04-14

Hi,
I am quite new to java and am going through some of the basics using the Netbeans 6.5 IDE. I seem to be having trouble reading from a file using the InPutFileStream Class as I keep getting a FileNotException when the program runs. I have added the file to be read as a .dat file and it is in the same folder as the .class file of my program.
Can anyone help?
my code is as follows:

import java.io.*;

public class ByteReader {
public static void main(String[] arguments){
try {
FileInputStream file = new FileInputStream("C:\\Documents and Settings\\My Documents\\NetBeansProjects\\JavaTut\\build\\class.dat");
boolean eof = false;
int count = 0;
while (!eof) {
int input = file.read();
System.out.print(input + " ");
if (input == -1)
eof = true;
else
count++;
}
file.close();
System.out.println("\nBytes read: " + count);
}catch (IOException e){
System.out.print("Error - " + e.toString());
}
}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

[code]C:\\Documents and Settings\\My Documents\\NetBeansProjects\\JavaTut\\build\\class.dat[/code]
Aren't you missing a folder in the hierarchy? Your username?

[code]C:\\Documents and Settings\\%USERNAME%\\My Documents\\NetBeansProjects\\JavaTut\\build\\class.dat[/code]