?
Solved

File input out put

Posted on 2001-06-22
6
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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