1. Byte stream:
FileInputStream
Specific methods:
FileInputStream fis = new FileInputStream("temp.txt");
1.int num = fis.available(); // total number of bytes returned to the file
2.fis.read()// Returns an int number, if no bytes are read, Returns - 1
1 //Use byte Array optimization 2 public static void m3()throws IOException 3 { 4 5 FileInputStream fis = new FileInputStream("temp.txt"); 6 7 byte[] arr=new byte[10]; 8 int len=0; 9 while((len=fis.read(arr))!=-1){ 10 11 System.out.print(new String(arr,0,len)); 12 } 13 14 fis.close(); 15 16 17 }
1 //Read files using byte streams:One byte at a time 2 public static void m2()throws IOException 3 { 4 FileInputStream fis = new FileInputStream("temp.txt"); 5 6 int num; 7 while((num=fis.read())!=-1){ 8 9 System.out.print((char)num); 10 } 11 12 fis.close(); 13 14 }
2. FileOutputStream (byte output stream)
Method:
FileOutputStream fos = new FileOutputStream("temp.txt");
Fos. write ("abc". getBytes (); //byte [] getBytes () encoding
Note: 1. When output, flush() should be performed after each input. If it is not refreshed, it will not be output.
2. close() at the end of the program, close the file, and close() automatically refreshes by default.
3. Byte buffer stream: Buffered Input Stream Buffered Output Stream
These methods throw exceptions.
1 Example: 2 import java.io.*; 3 class Demo4 4 { 5 public static void main(String[] args) 6 { 7 //Byte buffer stream: BufferedInputStream BufferedOutputStream 8 9 //Copy pictures 10 BufferedInputStream bis = null; 11 BufferedOutputStream bos = null; 12 13 try{ 14 15 bis = new BufferedInputStream(new FileInputStream("tou1.jpg")); 16 bos = new BufferedOutputStream(new FileOutputStream("tou1_copy.jpg")); 17 18 byte[] arr=new byte[1024]; 19 int len=0; 20 while((len=bis.read(arr))!=-1){ 21 22 bos.write(arr,0,len); 23 } 24 25 }catch(IOException e){ 26 e.printStackTrace(); 27 }finally{ 28 29 if(bis!=null) 30 try{ 31 bis.close(); 32 }catch(IOException e){ 33 throw new RuntimeException("Byte buffer read stream shutdown failed"); 34 } 35 36 if(bos!=null) 37 try{ 38 bos.close(); 39 }catch(IOException e){ 40 throw new RuntimeException("Byte buffer output stream shutdown failed"); 41 } 42 } 43 44 } 45 }
4. Standard input (associated with keyboard): System.in(in is a byte input stream object (default device associated with keyboard)
// int m = kk.read();// Blocking method of reading data from keyboard, as long as it is not input, it will wait all the time.
//System.out.print((char)m);
Standard Output (already associated with console devices) System.out;(out is a byte output stream object (the default associated device is the console)//is a byte output stream object
Using System.in to implement keyboard input is too cumbersome
System.in implements keyboard input code similar to BufferedReader's one-read-one-line function (readLine())
System.in -- Byte Read Stream
BufferedReader - Character Reader Stream
5. Conversion flow:
Input Stream Reader: A bridge from byte stream to character stream
InputStreamReader(InputStream in)
OutputStreamWriter : OutputStreamWriter(OutputStream out)
1 Example: 2 class Demo6 3 { 4 public static void main(String[] args) throws IOException 5 { 6 //Define standard keyboard byte input stream objects,Or definitions FileInputStream()object 7 InputStream in=System.in; 8 9 //Conversion streams are used to convert byte streams to character streams 10 InputStreamReader isr = new InputStreamReader(in); 11 12 //Using Character Buffer Stream 13 BufferedReader br = new BufferedReader(isr); 14 15 16 17 //Define standard byte output stream objects, or define FileOutputStream()object 18 PrintStream out = System.out;//Is a byte output stream object (already associated with the console device) 19 20 //Converting byte output stream to character output stream 21 OutputStreamWriter osw = new OutputStreamWriter(out); 22 23 //Using Character Buffer Stream 24 BufferedWriter bw = new BufferedWriter(osw); 25 26 27 String line = null; //You can't read the end sign from the keyboard 28 while((line=br.readLine())!=null){ 29 if("over".equals(line)) 30 break; 31 bw.write(line); 32 bw.newLine(); 33 // bw.flush(); 34 //System.out.println(line);//Byte stream 35 } 36 37 38 br.close(); 39 bw.close(); 40 41 } 42 }
6.File:java Object-Oriented Paths or Files
Instance object:
// 1. Encapsulate files or folders into File-type objects
File file = new File ("E: Java exercise day17 exercise tt.txt");
// 2. Suitable for file name change
File file = new File ("E: Java exercise day17 exercise " tt.txt ");
// 3. Separate definitions
File FF = new File ("E:\ Java exercise \ day17 exercise");
File file=new File(ff,"tt.txt");
Method:
getName()// Get the filename
getPath()// Gets the path, the relative is defined, the absolute is defined, and the absolute is obtained.
getParent()// Returns the path name string of the abstract path name parent directory; if the path name does not specify the parent directory, returns null.
getAbsolutePath()// Gets the absolute path
lastModified()// Get the last modification time of the file
delete()// delete files or folders
listRoots()// Get the root directory (Get the system disk)
list()// Gets the names of all files and folders in a directory and returns String []
listFile()// Return File [] Encapsulates all files and folders in a directory as File objects
listFile(FilenameFilter filter)
7. Recursion: The function calls itself. When the number of calls is too many, the memory will overflow. All you have to pay attention to, the number of calls should not be too many.
Note: There must be an end condition for the call
8.PrintStream: Byte print stream, which inherits OutputStream, has the function of byte output stream and adds printing function.
To which devices (purposes):
1: File-type files
2: OutputStream-type byte output stream
3: File name of string type
Example: PS. write (353); / / / 00000000000000000001 01100001 - "Cut off the first three bytes 01100001 -"
9. The parent class is Writer
PrintWriter: Character printing stream, the basic functions of character output stream are all available, and print function is added.
To which devices (purposes):
1:File-type files
2: Byte Output Stream Object
3: Character Output Stream Object
4: File name of string type
1 Example: 2 3 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 4 5 // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 6 7 PrintWriter pw = new PrintWriter(System.out,true);//With the second parameter, it will refresh automatically 8 9 String line = null; 10 while((line=br.readLine())!=null){ 11 if("over".equals(line)) 12 break; 13 pw.println(line);//Will output line breaks 14 //pw.write(line); 15 // pw.flush(); 16 }