sanko50
asked on
file handling in java
hi , i am in a trouble for input/output in java file handling. suppose i have a file
"data.txt" which conatins a matrix (say n x m dimension) . elements are some number. i want to read each of those numbers and want to do some mathematical operation on each of this , then want a output in a file. how can i do this?
something like this............
read a number from file
do some operation on that number.
send that to the output file.
read next number.
repeat the above steps.
I AM IN A CONFUSED STATE BY WATCHING SO MANY STREAMS AND FILE HANDLING MANNERS IN THE BOOK. I THINK MOST OF THE DATA FILE ARE LIKE ABOVE . SO I WANT A GENERAL PROCEDURE TO DO THE FILE-HANDLING OPERATIONS. CAN ANYBODY HELP PLZZZZZZZ....
THANKS
"data.txt" which conatins a matrix (say n x m dimension) . elements are some number. i want to read each of those numbers and want to do some mathematical operation on each of this , then want a output in a file. how can i do this?
something like this............
read a number from file
do some operation on that number.
send that to the output file.
read next number.
repeat the above steps.
I AM IN A CONFUSED STATE BY WATCHING SO MANY STREAMS AND FILE HANDLING MANNERS IN THE BOOK. I THINK MOST OF THE DATA FILE ARE LIKE ABOVE . SO I WANT A GENERAL PROCEDURE TO DO THE FILE-HANDLING OPERATIONS. CAN ANYBODY HELP PLZZZZZZZ....
THANKS
ASKER
ok...but how do the file understanad the end of file(EOF).
i.e something like below...
while( file data does not end) // HOW DO I CHECK THIS??
{
read data one by one and do mathematical operation
}
PLZ WHAT I SHOULD WRITE TO CHECK WHEN THE FILE DATA DOES NOT END. B'COZ AT LAST IT WILL FACE EOF FLAG.
THANKS...
ASKER
hello.... is there anybody ??? plz give some comments.
thanks
thanks
well...assuming when you read..your statement would look something like this:
String filepath = "data.txt"; //this is where the data is going to be read from
int[] data; //this is the data that is going to be stored
int counter;
try{
File inFile = new File(filepath);
FileInputStream inFileStream = new FileInputStream(inFile);
DataInputStream inDataStream = new DataInputStream(inFileStre am);
counter=0;
while (counter>=0)
{
data[counter] = inDataStream.readInt();
counter++;
}
inDataStream.close();
}
catch (EOFException e) { //some code to say that it has reached EOF} //I cant remember the exception for EOF, but i think thats it
hope it works
String filepath = "data.txt"; //this is where the data is going to be read from
int[] data; //this is the data that is going to be stored
int counter;
try{
File inFile = new File(filepath);
FileInputStream inFileStream = new FileInputStream(inFile);
DataInputStream inDataStream = new DataInputStream(inFileStre
counter=0;
while (counter>=0)
{
data[counter] = inDataStream.readInt();
counter++;
}
inDataStream.close();
}
catch (EOFException e) { //some code to say that it has reached EOF} //I cant remember the exception for EOF, but i think thats it
hope it works
ASKER
...but counter is always positive (as it is counter++) . it will never bcome negative.....so the loop will not be terminated. are u sure the above code works???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks.i got it..... it is the EOFException that will handle the end-of-file. thank u very much.
to write a file in java (assuming you want to write an integer):
String filepath = "data.txt"; //this is where the data is going to be written to
int data = 5; //this is the data that is going to be written
File outFile = new File(filepath);
FileOutputStream outFileStream = new FileOutputStream(outFile);
DataOutputStream outDataStream = new DataOutputStream(outFileSt
outDataStream.writeInt(dat
outDataStream.close();
to read a file in java (assuming you want to read an integer):
String filepath = "data.txt"; //this is where the data is going to be read from
int data; //this is the data that is going to be stored
File inFile = new File(filepath);
FileInputStream inFileStream = new FileInputStream(inFile);
DataInputStream inDataStream = new DataInputStream(inFileStre
data = inDataStream.readInt();
inDataStream.close();
to repeat the steps..just loop the code..or repeat it several times..it might be useful to give an example input file n output of it...hope this helps!