File I/O....which class to use?

D

Darth Tyrannus

I am barely learning Java and so far I have come across multiple
classes you can use for File I/O....


Which classes are most frequently used or preferred?

For example

FileOutputStream or FileWriter
FileInputStream or FileReader

Should I use BufferedReader? Is it necessary?

I know it depends on what is needed, but what is the most commonly used
classes for most programmers.

Thank you,
Paul
 
K

Knute Johnson

Darth said:
I am barely learning Java and so far I have come across multiple
classes you can use for File I/O....


Which classes are most frequently used or preferred?

For example

FileOutputStream or FileWriter
FileInputStream or FileReader

Should I use BufferedReader? Is it necessary?

I know it depends on what is needed, but what is the most commonly used
classes for most programmers.

Thank you,
Paul

Readers for text, streams for bytes. Buffer as necessary for performance.
 
T

Thomas Weidenfeller

Darth said:
I am barely learning Java and so far I have come across multiple
classes you can use for File I/O....


Which classes are most frequently used or preferred?

It is not a question about preference. It is a question about a class
doing what you need or not.
I know it depends on what is needed, but what is the most commonly used
classes for most programmers.

It does not matter what that class might be. If it is not the right one
*for your application* it will not work for you.

Go, learn the properties and function of the classes and then select the
ones which do what you need, not the ones which happen to work in other
people applications.

/Thomas
 
J

Josh Falter

The most common and simplest way to read files is to use a
BufferedReader, which I think goes something like
BufferedReader br = new BufferedReader(new FileReader(new
File(filename)));
String st = br.readLine();
while(st != null)
{
//process data
st = br.readLine();
}
br.close();

that may not be the exact syntax, but it is close enough to get you
started reading simple text files
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Darth said:
I am barely learning Java and so far I have come across multiple
classes you can use for File I/O....

Which classes are most frequently used or preferred?

For example

FileOutputStream or FileWriter
FileInputStream or FileReader

Should I use BufferedReader? Is it necessary?

I know it depends on what is needed, but what is the most commonly used
classes for most programmers.

Reader/Writer - for text
InputStream/OutputStream - for binary bytes or as basis for others
DataInputStream/DataOutputStream - for binary simple data types
ObjectInputStream/ObjectOutputStream - for binary Java objects

You use buffered if you read/write small chunks of data and
needs good performance.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top