Solved

Simple IO question

Posted on 2002-06-16
4
162 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
ID: 7082964
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
ID: 7083010
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
ID: 8860432
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

813 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

17 Experts available now in Live!

Get 1:1 Help Now