Project5Read
I am taking a Java Programming Course and running into an issue with a
project. The program for this project will use the file created in
Project5Write to create a formatted report that displays a list of the
information in the file you've created. Include a line at the bottom
that lists the total number of records in the file. Call this program
Project5Read. Include a test to return an error if the filename
selected doesn't exist.
The output is:
Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.
Im pretty sure the problem is in my while loop but cant figure out how
to resolve it. When I remove the while loop the program reads the
first input but not the second.
Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.
The Employee's Number is: 1
The Employee's Name is: James Smith
The Employee's Department is: Sales
The Employee has 15 Vacation Days Available
The total number of Employees is: 2
It should read:
Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.
The Employee's Number is: 1
The Employee's Name is: James Smith
The Employee's Department is: Sales
The Employee has 15 Vacation Days Available
The Employee's Number is: 2
The Employee's Name is: John Darme
The Employee's Department is: Security
The Employee has 20 Vacation Days Available
The total number of Employees is: 2
Any help would be great.
import java.io.*;
public class Project5Read {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader
(System.in));
String fileName = new String();
System.out.println("Enter a file name: ");
fileName = stdin.readLine();
File f = new File(fileName);
if (f.exists()) {
System.out.println(f + " is a valid file.");
System.out.println(" The file is " + f.length() + " bytes
long.");
System.out.println(" It was last modified on " +
f.lastModified() + " in Java Standard Time.");
if (f.canRead()) {
System.out.println(" The file can be read.");
}
if (f.canWrite()) {
System.out.println(" The file can be written to.");
}
} else {
System.out.println("File does not exist! Please check the
filename and try again!");
System.exit(1);
}
DataInputStream istream;
istream = new DataInputStream(new BufferedInputStream(new
FileInputStream(f)));
OutputStream ostream;
int c;
ostream = System.out;
try {
int counter = 1;
c = istream.read();
//while ((c = istream.read()) != -1) {
while (c != -1) {
String empName = new String();
String dept = new String();
int numVacaDays;
empName = istream.readUTF();
dept = istream.readUTF();
numVacaDays = istream.readInt();
System.out.println("The Employee's Number is: " +
counter);
System.out.println("The Employee's Name is: " + empName);
System.out.println("The Employee's Department is: " +
dept);
System.out.println("The Employee has " + numVacaDays + "
Vacation Days Available");
++counter;
}
System.out.println("The total number of Employees is: " +
counter);
istream.close();
ostream.close();
} catch (EOFException e) {
//closeFile();
}
}
}