Forums Register Login

I/O- Ranchers Attention Please

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
hi tvs,
argument to renameTo is a File not String.
I will add some more later.
Vanitha.
+Pie Number of slices to send: Send
Thanks Vanitha,
U r right. The argument is File and not String.
Waiting for responses from other enthusiastic ranchers.
tvs sundaram
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
To add a few
<pre>
File
FileInputStream
FileOutputStream
FileReader
FileWriter

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

HTH
tvs sundaram
+Pie Number of slices to send: Send

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).]
+Pie Number of slices to send: Send
This message is deleted
[This message has been edited by tvs sundaram (edited August 20, 2001).]
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
This thread has been viewed 1028 times.
Similar Threads

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:36:09.