Solved

java.io question...

Posted on 1998-03-18
6
205 Views
Last Modified: 2010-04-16
I am trying to write some information out to a file.  The contents I want to write out are ints and Strings.  I actually want to write out 5 ints, 1 String, then 5 more ints and 1 String, and I will have to repeat this sequence a number of times.  I want to write them in that order also, because when I read them out, I want to read them in that order.  So I want to know what is the best way to write these values out and then what is the best way to read them back in?
0
Comment
Question by:cupOjava
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1233660
Best is a relative concept of course. It depends on the criteria you use to measure. However, if we assume ease of development and portability your best bet is to use the DataInputStream and the DataOutputStream. To write you would do something like:

DataOutputStream do=new DataOutputStream(new FileOutputStream("filename"));
do.writeInt(i);
do.writeByte(s.length());
do.writeChars(String s);

to read:

DataInputStream di=new DataInputStream(new FileInputStream("filename"));
i=do.readInt();
l=do.readByte();
StringBuffer sb=new StringBuffer();
for(s=0; s<l; ++s)sb.append(do.readChar());
String s=sb.toString();

0
 
LVL 16

Expert Comment

by:imladris
ID: 1233661
The extra bits w.r.t. the String are because DataInputStream and -OutputStream don't provide methods for writing or reading Strings.

Note also that, as indicated with the FileInputStream and FileOutputStream, the stream classes can be cascaded together very easily. The DataOutputStream could just as easily have be plugged into a ByteArrayOutputStream or a BufferedOutputStream (which in turn might have been plugged into a FileOutputStream).
Therefore, if you find a Stream method that's better suited to your purposes, it is easy to add or substitute it.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1233662
Sorry, all the do's in the read example should be di.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:cupOjava
ID: 1233663
How come you have to writeByte() and then writeChar()?  Is that writing excess data?
0
 

Author Comment

by:cupOjava
ID: 1233664
Would your suggestion work ok if the data in the file was formatted as follows:

30170160Test1100016Testing

Is the compiler smart enough to know that 30, 17, 0, 16, 0 are five different ints?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1233665
Yes, it is writing some extra data. As pointed out, that is because the DataOutputStream does not natively support the writing of strings. So I suggest compensating for that by, effectively, writing a series of characters.

No, this mechanism will probably not work for the data you describe (30170160Test1100016Testing). The solution I proposed depends on the DataOutputStream creating the file. The DataOutputStream specifically commits to writing out Java primitives in a portable way. However it notes that it will write them out in binary, not ASCII format. I thought this would work since your question indicated that you wished to write data, then read it.

Outputting ASCII data can be done with a PrintStream. This is its purpose in life. Reading ASCII data is more complicated. Certainly I know of no method whatsoever (in any language) of processing the undelimited input you are referencing, unless the number of characters for each field were known in advance. (Is the first number 30 or 301? If the first is 30, is the second 1, 17, or 170?). I don't know what you're requirements are for the file. If the fields can have known lengths you can read the right number of bytes into a String and convert as needed. If you have delimiters (spaces, commas, whatever) you can use a StreamTokenizer to feed back the elements one at a time.

Let me know if there is some specific additional information I could give that would help.


0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

816 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

8 Experts available now in Live!

Get 1:1 Help Now