Solved

java.io question...

Posted on 1998-03-18
6
208 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
Independent Software Vendors: 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!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
jsp login check 12 52
Netbeans and org.apache.commons.lang3 issue 3 22
Html split(text) 2 26
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

740 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