• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

I/O- Ranchers Attention Please

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tvs,
argument to renameTo is a File not String.
I will add some more later.
Vanitha.
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vanitha,
U r right. The argument is File and not String.
Waiting for responses from other enthusiastic ranchers.
tvs sundaram
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This message is deleted
[This message has been edited by tvs sundaram (edited August 20, 2001).]
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic