A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
OutputStream
Java application uses an output stream to write data to a destination, it may be a file,an array,peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source, it may be a file,an array,peripheral device or socket.
FileInputStream and FileOutputStream (File Handling)
In Java, FileInputStream and FileOutputStream classes are used to read and write data in file. In another words, they are used for file handling in java.
import java.io.FileOutputStream;
class Test {
public static void main(String args[]) {
try {
FileOutputStream fout = new FileOutputStream("C:\\abc.txt"); // It is creating a new file
String s = "Sachin Tendulkar is my favourite player!!!!!";
byte b[] = s.getBytes();// converting string into byte array
fout.write(b); //Sending Byte[] to write method in FileOutputStream
//fout.close();
/*FileOutputStream fout1 = new FileOutputStream("C:\\abc.txt");*/
String str = "New char";
byte[] b1 = str.getBytes();
fout.write(b1);
System.out.println("success...");
} catch (Exception e) {
System.out.println(e);
}
}
}
No comments:
Post a Comment