Solved

Simple IO question

Posted on 2002-06-16
4
157 Views
Last Modified: 2012-05-04
Hi

if you use the statment below to read from file (arg0) and write to a file from arg1. What is the statment that you must use to write into the file created for writing. I used out.write but it seems that this method is for binary files and I am just reading a text file and then do some processing and then write it again as text.

Thanks
PF

StreamTokenizer stok = new
StreamTokenizer(new FileInputStream (args[0]));

FileOutputStream out=new FileOutputStream (args[1]);
0
Comment
Question by:Paris_France
4 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 25 total points
Comment Utility
You can write bytes:
byte[] buffer;
.
.
.
out.write(buffer);

If you have a string I can do this:
String str="Java";

out.write(str.getBytes());

But.....

You should never use File Streams directly.Use filtered and buffered streams:

DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(arg[1])));

This way you can use the methods:

out.writeInt() ....writes integer;
out.writeBytes()..... writes String;

etc.



0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
Comment Utility
If you are working with text files then you should instead be using a BufferedReader to read the file:

BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));


And PrintWriter to write:

PrintWriter out
   = new PrintWriter(new FileOutputStream("foo.out"));
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- split points between msterjev and objects

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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 …

772 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

10 Experts available now in Live!

Get 1:1 Help Now