Solved

File input out put

Posted on 2001-06-22
6
165 Views
Last Modified: 2010-03-31
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
Comment
Question by:grace20
6 Comments
 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6220118
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
 
LVL 2

Accepted Solution

by:
venkat2000120699 earned 50 total points
ID: 6220150
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
 

Expert Comment

by:surendraboppana
ID: 6222563
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:sumeetsh
ID: 6226363
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
 
LVL 5

Expert Comment

by:vemul
ID: 7620939
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
 

Expert Comment

by:modulo
ID: 7643034
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 93
mapShare challenge 13 70
JDeveloper 12c for 32 bit 4 36
Groovy:unable to resolve class error 2 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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