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=newbyte[1024x768]; 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=newchar[1024x768]; 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 Success!!!") ); }
Fourth set: can read binary (img image, etc.)
DataInputStream: loading the local img into memory
FileInputStream fis=NewFileInputStream ("E:\\5.txt"); FileOutputStream Fos=NewFileOutputStream ("D:\\55.txt"); DataInputStream Dis=NewDataInputStream (FIS); DataOutputStream dos=NULL; byte[]bytes=New byte[1024]; intdata; while((Data=dis.read (bytes))!=-1) {dos=NewDataOutputStream (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 { //creating an input stream objectFileInputStream fis=NewFileInputStream ("c:\\ Fanning. jpg"); Dis=NewDataInputStream (FIS); //creating an output stream objectFileOutputStream outfile=NewFileOutputStream ("C:\\ Fanning little Beauty 33.jpg"); out=NewDataOutputStream (OutFile); inttemp=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