1 character input (FileReader, char)
Import Java.io.ioexception;import Java.io.filereader;public class Ep10_1 {public static void Main (string[] args) Throws ioexception{ //Reference object B filereader b = new FileReader ("/tmp/ep10_1.txt"); The reader space that defines the text store char[] A = new char[1000]; Reads the contents of object B into a, returning the number of characters int num = B.read (a); Converts the character a to str output string str = new string (a,0,num); SYSTEM.OUT.PRINTLN ("file reads: \ n" +str); B.close ();} }
2 character output (FileWrite char)
Import Java.io.filewriter;import Java.io.ioexception;public class Ep10_3 {public static void Main (string[] args) { c1/>try{ FileWriter A = new FileWriter ("/tmp/wt.txt"); for (int i=32;i<126;i++) { //char type write a.write (i); } A.close (); } catch (IOException e) {}} }
3 character input and output (Bufferedreader,bufferedwriter,char )
Import Java.io.*;import Java.nio.buffer;public class Ep10_4 {public static void Main (string[] args) { String str = new String (); try{ //bufferedreader refers to a type of string, which means that BufferedReader converts the text of the FileReader character to string bufferedreader in = new BufferedReader (New FileReader ("/tmp/ep10_1.txt")); BufferedWriter out = new BufferedWriter (New FileWriter ("/tmp/ep10_4.txt")); while ((Str=in.readline ())!=null) { System.out.println (str); Out.write (str); Out.newline (); } Out.flush (); In.close (); Out.close (); } catch (IOException e) { System.out.println ("Error contents:" +e);}}}
4 bytes of input
ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classEp10_5 { Public Static voidMain (string[] args) {Try { byte[] bt =New byte[1000]; FileInputStream ins=NewFileInputStream ("/tmp/ep10_1.txt"); intnum =Ins.read (BT); String Str=NewString (bt,0, num); System.out.println ("Contents:\n" +str); }Catch(IOException e) {System.out.println ("Error:\n" +e); } }}
Java read-Write text file