• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

File input out put

Hiii, visual basic was my major so im having problems learning this.. lol

What i want to do is a simple program that inputs and outputs information to a file, i also want to be able to search for certian lines of information for deleting and updating.. im using the java.io.* for reading and writing.. its just not working very well.. can someone help me out? or point me to a very easy piece of source code??  im giving myself such a headache over this

Grace.
0
grace20
Asked:
grace20
1 Solution
 
venkat2000120699Commented:
Here is a basic tutorial from Sun that solves your problem.
http://www.uni-klu.ac.at/unihome/header/tutorial/javatutorial122/essential/io/index.html

Go on clicking next button at the bottom left corner.  U will find answers for many things you wanted to do.
0
 
venkat2000120699Commented:
And here is a list of examples that illustrates the usage of all classes in io package.
http://tecfa.unige.ch/guides/java/examples/JavaClassLibExamples/io/
0
 
surendraboppanaCommented:
may u should visit this site


www.oreilly.com/catalog/


where u can find some excellent examples on  i/o on files


with regards

surendra
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sumeetshCommented:
hi
The code mention below is for writing to and reding for a file

DataOutputStream, like other filtered output streams, must be attached to some other OutputStream. In this case, it's attached to a FileOutputStream that's set up to write to a file named test.txt.


DataOutputStream dos = new DataOutputStream(
                           new FileOutputStream("test.txt"));


Next, DataOutputStream's specialized writeXXX methods to write the data (contained within arrays in the program) according to the type of data being written:

for (int i = 0; i < prices.length; i ++) {
    dos.writeDouble(prices[i]);
    dos.writeChar('\t');
    dos.writeInt(units[i]);
    dos.writeChar('\t');
    dos.writeChars(descs[i]);
    dos.writeChar('\n');
}
dos.close();

DataInputStream also must be attached to some other InputStream; in this case, a FileInputStream set up to read the file just written test.txt.Then just reads the data back in using DataInputStream's specialized readXXX methods.

DataInputStream dis = new DataInputStream(
                          new FileInputStream("invoice1.txt"));

when reading you see loops like this:

while ((input = dis.readLine()) != null) {
    . . .
}

The readLine method returns a value, null, that indicates that the end of the file has been reached. Many of the DataInputStream readXXX methods can't do this because any value that could be returned to indicate end-of-file may also be a legitimate value read from the stream. For example, suppose that you wanted to use -1 to indicate end-of-file? Well, you can't because -1 is a legitimate value that can be read from the input stream using readDouble, readInt, or one of the other read methods that reads numbers. So DataInputStream's readXXX methods throw an EOFException instead. When the EOFException occurs the while (true) terminates.



For Update and delete u could use Random Access Files Perform the following steps:

>Open the same file for both reading and writing:
new RandomAccessFile("test.txt", "rw");
>Read the content using readXXX methods.
          if the content matches your search
                go to the begining of the record and overwrite it
 

RandomAccessFile contains three methods for explicitly manipulating the file pointer.
skipBytes
Moves the file pointer forward the specified number of bytes.
seek
Positions the file pointer just before the specified byte.
getFilePointer
Returns the current byte location of the file pointer.


Hope this helps
Regards
Sumeet







0
 
vemulCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- split points between venkat2000 and sumeetsh
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
moduloCommented:
Finalized as proposed

sumeetsh see:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20438756.html

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now