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
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.
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.
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
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
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).]
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).]