The file class is used to access the properties of files or directories
Flow: Refers to a series of flowing characters, which is a channel for sending information in FIFO mode. Between the program and the data source is connected by a stream.
First set: Byte stream read Write scheme
FileInputStream: Byte stream read text file
FileInputStream fis=new FileInputStream ("e:\\ read file. txt"); Byte[]bytes=new byte[1024]; int data; while ((Data=fis.read (bytes))!=-1) { string str=new string (bytes,0,data); System.out.println (str); } Fis.close (); }
FileOutputStream: Byte stream write to hard disk
FileOutputStream fos=new FileOutputStream ("E:\\1.txt"); String word= "College entrance examination is the watershed of life"; byte[] bytes = Word.getbytes (); Fos.write (bytes); Fos.close (); System.out.println ("Write succeeded!");} }
Second set: Character Stream read Write scheme
FileReader: Character Stream reads text
FileReader fr=new FileReader ("e:\\ read file. txt"); Char[]chars=new char[1024]; int data; while ((Data=fr.read (chars))!=-1) { string str=new string (chars); System.out.println (str);} }
FileWriter: Character Stream writes text
FileWriter fw=new FileWriter ("E:\\2.txt"); Fw.write ("New June"); System.out.println ("Write succeeded!"); Fw.close (); }
Third set: <bufferedreader, bufferedwriter> General and FileReader and FileWriter combined use
BufferedReader: Custom cache size, read text 8,192 Char
FileReader fr=new FileReader ("e:\\ read file. txt"); BufferedReader br=new BufferedReader (FR); String Line; while ((Line=br.readline ())!=null) { System.out.println (line); } Br.close (); Fr.close ();}
BufferedWriter: Writing text
FileWriter fw=new FileWriter ("E:\\5.txt"); BufferedWriter bw=new BufferedWriter (FW); Bw.write ("ok!!"); Bw.close (); Fw.close (); System.out.println ("Write Succeeded!!!"); }
Fourth set: can read binary (img image, etc.)
DataInputStream: Loading the local img into memory
FileInputStream fis=new FileInputStream ("E:\\5.txt"); FileOutputStream fos=new FileOutputStream ("D:\\55.txt"); DataInputStream dis=new datainputstream (FIS); DataOutputStream Dos=null; Byte[]bytes=new byte[1024]; int data; while ((Data=dis.read (bytes))!=-1) { dos=new dataoutputstream (FOS); Dos.write (bytes); } Dos.close (); Dis.close (); Fos.close (); Fis.close (); System.out.println ("Copy succes!!!"); }
DataOutputStream: Writes the In-memory binary data to a file on the hard disk
DataOutputStream Out=null; DataInputStream Dis=null; try {//Create input Stream object FileInputStream fis=new fileinputstream ("c:\\ Fanning. jpg"); Dis=new DataInputStream (FIS); Create an output stream object FileOutputStream outfile=new fileoutputstream ("c:\\ Fanning Little Beauty 33.jpg"); Out=new DataOutputStream (OutFile); int Temp=dis.read (); while (temp!=-1) {out.write (temp); Temp=dis.read (); } System.out.println ("Copy succeeded"); Fis.close (); Outfile.close (); } catch (Exception e) {System.out.println ("file does not exist"); }finally{try {if (dis!=null) {dis.close (); } if (Out!=null) {out.close (); }} catch (Exception E2) {e2.printstacktrace (); } }
Note: In Java, how are byte arrays and string strings converted?
1. String Turn byte[]
String str = "Hello";
byte[] Srtbyte = Str.getbytes ();
2, byte[] turn string
Byte[] Srtbyte;
String str = new string (srtbyte);
System.out.println (str);
Four sets of reading and writing programs