Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File input out put

Posted on 2001-06-22
6
Medium Priority
?
174 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
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.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

618 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