Solved

file handling in java

Posted on 2003-11-12
7
2,215 Views
Last Modified: 2007-12-19
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

0
Comment
Question by:sanko50
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:Traltixx
ID: 9737626
this is the simple way:
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(outFileStream);

outDataStream.writeInt(data);

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(inFileStream);

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!
0
 

Author Comment

by:sanko50
ID: 9745751

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...
0
 

Author Comment

by:sanko50
ID: 9747825
hello.... is there anybody ??? plz give some comments.

thanks
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:Traltixx
ID: 9752670
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(inFileStream);
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
0
 

Author Comment

by:sanko50
ID: 9752954

...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???




0
 
LVL 2

Accepted Solution

by:
Traltixx earned 30 total points
ID: 9757172
There is a EOFException which will "catch" the code if it reach EOF, thus counter should not be a problem (and yes it works)...the loop will be terminated at the catch statement.
hope it helps
0
 

Author Comment

by:sanko50
ID: 9757943
thanks.i got it.....  it is the  EOFException that will handle the  end-of-file. thank u very much.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
GUI builder for Eclipse? 8 35
nextBoolean(double p) for Random class 3 40
more than one jdk and one jre 1 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now