Solved

File input out put

Posted on 2001-06-22
6
166 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 33
Java - Why doesn't this JFrame work 3 43
servlet doXXX methods 3 38
eclipse apache tomcat admin console 52 66
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

896 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

16 Experts available now in Live!

Get 1:1 Help Now