Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple IO question

Posted on 2002-06-16
4
Medium Priority
?
174 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 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

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

718 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