PDA

Click to See Complete Forum and Search --> : Reading A text File


Dafly
Oct 29th, 2000, 01:21 AM
here is the code below, I am having trouble where I try to read the file, i am new to Java so please any help WILL HELP. What I want this code to do is when it is accesed or started it opens a file reads the number then adds 1 to it and then writes the new number to it, all it is is a basic counter.

Thank You,
Jeremy


import java.io.*;

class counter {

public static void main (String args[]) {

int Visits;
Visits = 45;


try {

FileInputStream Dis = new FileInputStream("Vist.cnt");
DataInputStream myInput = new DataInputStream(Dis);
while (true) {
Visits = myInput.readInt();
System.out.println(Visits+"Hello");
}
}
catch (Exception e) {
System.out.println(e);
}

try {

FileOutputStream fout = new FileOutputStream("Vist.cnt");
PrintStream myOutput = new PrintStream(fout);
Visits = Visits + 1;
myOutput.print(Visits);
System.out.println(Visits);

} // try ends here
catch (IOException e) {
System.out.println("Error: " + e);
System.exit(1);
}
} // main ends here

}