Solved

java.io question...

Posted on 1998-03-18
6
210 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Technology Partners: 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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

630 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