File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I/O- Ranchers Attention Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I/O- Ranchers Attention Please" Watch "I/O- Ranchers Attention Please" New topic

I/O- Ranchers Attention Please

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Irrespective of repeated revisions, memorizations etc I flunk in I/O.
I thought I can request the attention of fellow ranchers through this thread.
May I request u all to contribute everything related to I/O on this thread so that it can also be used by our fellow collegues as a reference.
To statrt with
- Java provides streams for dealing with I/O.
- Input stream-----Source of Data
- Output stream --- Destination of Data
more to come as this thread grows..
Make it grow please
tvs sundaram
Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
Yes, you are quite right,
InputStream is used to read data (eg from files etc) and then use the data it got (from the file) and do things with it; an eg could be: a certain type input stream (you have many, buffered, byte,char, etc) reads the contents of a txt file and then prints it out every second letter from it on the screen
OutputStream is used to write data (eg writing to a file etc)
A good example of both of them used together in the same program would be that you use a inputstream to read the contents of a file called file and then use a outputstream to write those contents to another file called file1, and write every second char of the contents of file to another file called file2
etc etc etc...
Hope that helps

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
In Bytestreams
int read() read a byte but returns an integer.
In Reader
int read() read an int in the range of 0 to 65535.
In both the method returns -1 if end of file is reached.
In Bytestreams
int read() read a byte. The byte read() is only the last eight bits of the int while the remaining bits are zeroed out.

Hello Ranchers, shoot your points...

tvs sundaram

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
All the read() and write() methods of InputStream and OutputStream throws IOException..
int read() throws IOException
int read(byte[] b) throws IOException
int read(byte[] b,int off,int len) throws IOException
void write(int b) throws IOException
void write(byte[] b) throws IOException
void write(byte[] b,int off,int len) throws IOException
void close() throws IOEception
void flush() throws IOException

Swati Kale
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

You can take any primitive value from keyboard in this way. Here
I have shown how to take int from keyboard....

It's equivalent to C's scanf("%d",num); and C++'s cin>>num;


Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Creating a File object doesn't do anything in the underlying system.
i.e; No actual file is created.
The same thing is applicable to GC ing the File Object also.
The method mkdir() and mkdirs() are different.
getCanonicalPath() should be used in try/catch block.
getCanonicalPath() and getAbsolutePath() will return the same if they are called from the directory in which the java file resides..(Am I correct??)
length() returns the number of bytes in the file
The argument to the method renameto is a String & it returns boolean
tvs sundaram
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
hi tvs,
argument to renameTo is a File not String.
I will add some more later.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Thanks Vanitha,
U r right. The argument is File and not String.
Waiting for responses from other enthusiastic ranchers.
tvs sundaram
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
And also the constructors for FileOutputStream and FileWriter are similar to each other:
FileOutputStream(File file)
FileOutputStream(FileDescriptor fdObj)
FileOutputStream(String fileName)
FileOutputStream(String fileName, boolean append)

FileWriter(File file)
FileWriter(FilDescriptor fd)
FileWriter(String fileName)
FileWriter(String fileName, boolean append)


[This message has been edited by Guoqiao Sun (edited August 19, 2001).]

Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="" target="_blank" rel="nofollow"></a>, Java resource, mock exam, forum
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
To add a few

all class constructors take
as arguments.

tvs sundaram
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153

File f= new File("File.txt");
OutputStream out=new FileOutputStream("File.txt");
Code will compile & sets the length of the file to 0.
This form of constructor of FileOutputStream (FOS) create a new File wiping out the previous contents of the file under consideration.
Adding boolean option to true in the FOS constructor, can be used to open an existing file without destroying its contents, for appending.
tvs sundaram

[This message has been edited by tvs sundaram (edited August 20, 2001).]
[This message has been edited by tvs sundaram (edited August 20, 2001).]
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
This message is deleted
[This message has been edited by tvs sundaram (edited August 20, 2001).]
I agree. Here's the link:
subject: I/O- Ranchers Attention Please
It's not a secret anymore!