recursion
What is recursion
The phenomenon of calling the method itself in the method definition
be careful
- Recursion needs an exit, otherwise it is dead recursion
- The number of times should not be too many, otherwise the memory will overflow
- Constructors cannot be used recursively
For example, find a Fibonacci sequence
public static void main(String[] args) { //1,1,2,3,5,8,13,21 //The third starts with the sum of the first two to find the twentieth number int []arr = new int[20]; arr[0] = 1; arr[1] = 1; for(int i=2;i<arr.length;i++){ arr[i] = arr[i-2]+arr[i-1]; } System.out.println(arr[arr.length-1]); System.out.println("***************"); System.out.println(recursion(20)); } private static int recursion(int i){ int sum = 0; if(i==1 || i==2){ return 1; }else{ sum += recursion(i-1) + recursion(i-2); } return sum; }
IO stream
Used to solve the problem of data transmission between devices
In a word:
If you open something that you can understand with your computer notebook, use character stream,
If you can't read the notebook, use byte stream
Classification of IO streams
flow to:
Input stream read data
Output stream write data
Data type:
Byte stream:
Byte input stream read data InputStream
Byte output stream write data OutputStream
Character stream:
Character input stream read data Reader
Character output stream write data Writer
Write
Write a data into the file
We are now trying to write a data to the file. We can understand it with Notepad. It is best to use character stream, but we use byte stream here
Operation steps of byte output stream:
1. Create byte output stream object
2. Write data
3. Release resources
We write text files through programs, so we use OutputStream here
public static void main(String[] args) throws IOException { //Create byte output stream object File file = new File("a.txt");//If the file does not exist, it is created automatically FileOutputStream fileOutputStream = new FileOutputStream(file); System.out.println(fileOutputStream); /* JVM What did you do when creating the byte output stream object 1,Call system functions to create files 2,Create fileOutputStream object 3,Point the fileOutputStream object to a file */ fileOutputStream.write("big data".getBytes()); /* Why do I need a close file 1,Make the stream object garbage so that the garbage collector can recycle it 2,Notify the system to release the resources related to the file */ fileOutputStream.close(); }
What does the JVM do when creating byte output streams
Call system functions to create files
Create fileOutputStream object
Point the fileOutputStream object to the file
Why do I need a close file
Make the stream object garbage so that the garbage collector can recycle it
Notify the system to release the resources related to the file
Three ways to write data to a file using a byte output stream
public void write(int b)
public void write(byte[] b)
public void write(byte[] b,int off,int len)
public static void main(String[] args) throws IOException { /* How to write data using byte output stream 1,Create byte output stream object 2,Write data using the write method 3,Release resources */ FileOutputStream fileOutputStream = new FileOutputStream("a.txt"); //public void write(int b) // fileOutputStream.write(97); //a.txt file content is a, you can see this way, stored in 97 ASCII code //public void write(byte[] b) byte[] b = new byte[]{104,105,106};//a.txt is hij, which is also stored in the underlying ASCII code // fileOutputStream.write(b); //public void write(byte[] b,int off,int len) fileOutputStream.write(b,0,2);//Offset, also ASCII }
write in
Operation steps of byte input stream
- Create byte input object
- Call the read method to read the data and display the data on the console
- Release resources
Two ways to read data from byte input stream
- public int read()
- public int read(byte[] b)
Buffer class (efficient class)
We can see that when using byte I / O stream and copying photos or videos, the speed is very slow. Here we use byte buffer stream
Byte buffered output stream
BufferedOutputStream
Byte buffered input stream
BufferedInputStream
Byte buffered output stream
In BufferedOutputStream, the specific subclass implementation object of an OutputStream to be passed in
public static void main(String[] args) throws IOException{ BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("a.txt")); bufferedOutputStream.write("big data yyds".getBytes()); bufferedOutputStream.close(); }
Byte buffered input stream Input to console: public static void main(String[] args) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("a.txt")); byte[] bytes = new byte[1024]; int len = 0; while ((len = bufferedInputStream.read(bytes)) != -1){ System.out.println(new String(bytes,0,len)); } bufferedInputStream.close(); } }//Big data yyds