This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I/O- Ranchers Attention Please" Watch "I/O- Ranchers Attention Please" New topic
Author

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
Hello,
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
Kamil.

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..
i.e.
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
SCJP
SCWCD
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370


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;

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Files
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.
Vanitha.
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:
<code>
FileOutputStream(File file)
FileOutputStream(FileDescriptor fdObj)
FileOutputStream(String fileName)
FileOutputStream(String fileName, boolean append)
</code>

<code>
FileWriter(File file)
FileWriter(FilDescriptor fd)
FileWriter(String fileName)
FileWriter(String fileName, boolean append)
</code>

Guoqiao

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


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

Joined: Jan 28, 2001
Posts: 153
To add a few
<pre>
File
FileInputStream
FileOutputStream
FileReader
FileWriter

all class constructors take
(FIle)
(FileDescriptor)
(String)
as arguments.
</pre>

HTH
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: http://aspose.com/file-tools
 
subject: I/O- Ranchers Attention Please
 
Similar Threads
Java/J2EE jobs in Singapore?
Final Mock Test Question 10 Head First and Servlets
A request for moderator
Welcome Seema Manivannan and Valentin Crettaz
SOA in Part 2 Assignment