Java I/O stream
FileInputStream: reads one byte at a time
FileInputStream fileInputStream = new FileInputStream("log.txt");
int ch = fileInputStream.read();
while(ch != -1) {
System.out.print((char)ch);
ch = fileInputStream.read();
}
fileInputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream("log2.txt", true);
String message = "This is master ";
fileOutputStream.write(message.getBytes(StandardCharsets.UTF_8));
fileOutputStream.close();
DataOutputStream/DataInputStream: primitive writing/reading
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("log2.txt"));
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("log2.txt"));
dataOutputStream.writeInt(65);
dataOutputStream.writeDouble(22.78);
System.out.println(dataInputStream.readInt());
System.out.println(dataInputStream.readDouble());
dataOutputStream.close();
OutputStreamWriter, InputStreamReader: character to byte, byte to char
String message = "ফারহান";
OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream("output.txt"));
output.write(message);
System.out.println(output.getEncoding());
//output.flush(); // forces the content on buffer to be written
output.close(); // auto calls the flush method then closes the stream
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("output.txt"));
int data = inputStreamReader.read();
while(data != -1) {
System.out.print((char)data);
data = inputStreamReader.read();
}
inputStreamReader.close();
BufferedReader, BufferedWriter
BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer.
InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case. Ref: Click here
String message = "ফারহান";
OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream("output.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("output.txt"));
bufferedWriter.write(message);
bufferedWriter.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader("output.txt"));
String line = null;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
BufferedReader for System.in
As to the choice, use the
Scanner
if you want to parse the file, use theBufferedReader
if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.Parsing = interpreting the given input as tokens (parts). It’s able to give back you specific parts directly as int, string, decimal, etc. See also all those
nextXxx()
methods inScanner
class.Reading = dumb streaming. It keeps giving back you all characters, which you in turn have to manually inspect if you’d like to match or compose something useful. But if you don’t need to do that anyway, then reading is sufficient. Ref: Click here
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String name = bufferedReader.readLine();
System.out.println(name);
PrintStream
PrintStream printStream = new PrintStream("output.txt");
printStream.write("ফারহান".getBytes(StandardCharsets.UTF_8));
printStream.close();
PrintWriter
The main reason to use the PrintWriter is to get access to the printXXX methods like println(). You can essentially use a PrintWriter to write to a file just like you would use System.out to write to the console.
A BufferedWriter is an efficient way to write to a file (or anything else), as it will buffer the characters in Java memory before (probably, depending on the implementation) dropping to C to do the writing to the file.
There is no such concept as a “PrintReader”; the closest you will get is probably java.util.Scanner. Ref: Click Here