Solved

java.io question...

Posted on 1998-03-18
6
200 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json format text only 4 65
JDeveloper 12c for 32 bit 4 36
Securing Jmx Console and web console 2 48
backtracking recursion  code 19 41
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 …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

9 Experts available now in Live!

Get 1:1 Help Now