Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple IO question

Posted on 2002-06-16
4
Medium Priority
?
175 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
3 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 100 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 100 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

Hire Technology Freelancers with Gigs

Work with 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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

877 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