?
Solved

java.io question...

Posted on 1998-03-18
6
Medium Priority
?
211 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
[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
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 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