Solved

Simple IO question

Posted on 2002-06-16
4
172 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
[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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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