There are several ways to read and write files in Java, where the following comparisons are made by code.
Comparison one: FileWriter and FileReader
Public classMyCode1 { Public Static voidMain (string[] args) throws IOException {File F=NewFile ("My.txt"); FileWriter FW=NewFileWriter (f); Fw.write ("HELLO"); Fw.close (); FileReader FR=NewFileReader (f); System. out. println (Char) Fr.read ()); Fr.close (); }}
Output: H
Visible, FileWriter can be written in batches, but FileReader can only read one character at a time.
Comparison II:outputstream and InputStream
Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); Char[] data =New Char[]{' H ', ' E ', ' l ', ' l ', ' O '}; OutputStream OS=NewFileOutputStream (f); Os.write (data[0]); Os.close (); InputStream is=NewFileInputStream (f); System.out.println ((Char) Is.read ()); Is.close (); }}
Output: H
Visible,outputstream and InputStream can read and write only one character at a time .
Comparison three:outputstreamwriter and inputstreamreader
Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); OutputStream OS=NewFileOutputStream (f); OutputStreamWriter OSW=Newoutputstreamwriter (OS); Osw.write ("HELLO"); Osw.close (); Os.close (); InputStream is=NewFileInputStream (f); InputStreamReader ISR=NewInputStreamReader (IS); System.out.println ((Char) Isr.read ()); Isr.close (); Is.close (); }}
Output: H
Visible,outputstreamwriter can be written in batches, but InputStreamReader can only read one character at a time. Effects with FileWriter and FileReader.
Comparison four:bufferedwriter and bufferedreader
Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); OutputStream OS=NewFileOutputStream (f); OutputStreamWriter OSW=Newoutputstreamwriter (OS); BufferedWriter BW=NewBufferedWriter (OSW); Bw.write ("HELLO"); Bw.close (); Osw.close (); Os.close (); InputStream is=NewFileInputStream (f); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); System.out.println (Br.readline ()); Isr.close (); Is.close (); }}
Output: HELLO
Visible,bufferedwriter and BufferedReader can be read and written in bulk .
Java InputStream, OutputStream, reader and writer comparisons