当前位置:网站首页>IO stream_ recursion
IO stream_ recursion
2022-06-30 16:49:00 【The cool city is not warm and the night is slightly cool】
Catalog
- 1 IO Introduction to the stream
- 2 Byte stream output stream
- 3 Byte input stream
- 4 Byte buffer stream
- 5 Properties aggregate
- 6 ResourceBundle Tool class
- 7 recursive
1 IO Introduction to the stream
1.1 Why study IO flow
- Through the variable , Array , Or a collection of stored data
- Can not be stored permanently , Because the data is stored in memory
- As long as the code runs , All data will be lost
- Use IO flow
- 1, Write data to a file , Realize the permanent storage of data
- 2, Read the data in the file into memory (Java Program )
1.2 What is? IO flow
- I Express input , It's the process of data from hard disk to memory , Call it reading .
- O Express output , It's the process of data from memory to hard disk . Call it writing .
- IO Data transmission of , It can be seen as a flow of data , In the direction of the flow , With memory as a reference , Read and write
- Simply speaking : Memory is reading , Memory is writing
1.3 IO Classification of flows
- Distinguish according to the flow direction
- Input stream : To read data
- Output stream : To write data
- Distinguish by type
- Byte stream
- Character stream
- Be careful :
- Byte stream can manipulate any file ( All types of files ) —> Audio 、 video 、 picture
- Character stream can only operate on Plain text file -->java、go file
- use windows Notepad open can read understand , Then such a file is a plain text file .
2 Byte stream output stream
2.1 Introduction to byte output stream
FileOutputStream class :
OutputStreamThere are many subclasses , Let's start with the simplest subclass .java.io.FileOutputStreamClass is the file output stream , Used to write data to a file
Construction method :
public FileOutputStream(File file): Creates a file output stream to write to the File File represented by object .public FileOutputStream(String name): Create a file output stream to write to the file with the specified name .
public class FileOutputStreamConstructor throws IOException { public static void main(String[] args) { // Use File Object to create a flow object File file = new File("a.txt"); FileOutputStream fos = new FileOutputStream(file); // Create a stream object with a file name FileOutputStream fos = new FileOutputStream("b.txt"); } }Byte output stream write data quick start
- Create byte output stream object
- Writing data
- Release resources
package com.itheima.outputstream_demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* Byte output stream write data quick start : 1 Create byte output stream object . 2 Writing data 3 Release resources */ public class OutputStreamDemo1 { public static void main(String[] args) throws IOException { // Create byte output stream object // If the specified file does not exist , Will automatically create files // If the file exists , Will empty the contents of the file FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt"); // Writing data // Writing to a file is in the form of bytes // Just the file helps us translate the bytes into the corresponding characters , Convenient view fos.write(97); fos.write(98); fos.write(99); // Release resources // while(true){} // Break the relationship between stream and file fos.close(); } }
2.2 Method of writing data in byte output stream
Method of byte stream writing data
- 1 void write(int b) Write one byte of data at a time
- 2 void write(byte[] b) Write array data one byte at a time
- 3 void write(byte[] b, int off, int len) Write part of a byte array at a time
package com.itheima.outputstream_demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* Byte stream to write data 3 Ways of planting 1 void write(int b) Write one byte of data at a time 2 void write(byte[] b) Write array data one byte at a time 3 void write(byte[] b, int off, int len) Write part of a byte array at a time */ public class OutputStreamDemo2 { public static void main(String[] args) throws IOException { // Create byte output stream object FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt"); // Writing data // 1 void write(int b) Write one byte of data at a time fos.write(97); fos.write(98); fos.write(99); // 2 void write(byte[] b) Write array data one byte at a time byte[] bys = { 65, 66, 67, 68, 69}; fos.write(bys); // 3 void write(byte[] b, int off, int len) Write part of a byte array at a time fos.write(bys, 0, 3); // Release resources fos.close(); } }
2.3 Line feed and append write of write data
package com.itheima.outputstream_demo;
import java.io.FileOutputStream;
import java.io.IOException;
/*
Line feed and append write of byte stream write data
1 How to wrap byte stream write data ?
After writing the data , Add line breaks
windows : \r\n Be careful \r\n It's a piece of
linux : \n
mac : \r
2 How to realize additional writing of byte stream write data ?
By construction method : public FileOutputStream(String name,boolean append)
Create a file output stream to write to the file with the specified name . If the second parameter is true , The contents of the file will not be emptied
*/
public class OutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// Create byte output stream object
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt");
// void write(int b) Write one byte of data at a time
fos.write(97);
// Because the byte stream cannot write a string , Convert a string into a byte array and write it to
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// Release resources
fos.close();
}
}
package com.itheima.outputstream_demo;
import java.io.FileOutputStream;
import java.io.IOException;
/* Line feed and append write of byte stream write data 1 How to wrap byte stream write data ? After writing the data , Add line breaks windows : \r\n linux : \n mac : \r 2 How to realize additional writing of byte stream write data ? By construction method : public FileOutputStream(String name,boolean append) Create a file output stream to write to the file with the specified name . If the second parameter is true , The contents of the file will not be emptied */
public class OutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// Create byte output stream object
// Additional write data
// By construction method : public FileOutputStream(String name,boolean append) : Additional write data
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt" , true);
// void write(int b) Write one byte of data at a time
fos.write(97);
// Because the byte stream cannot write a string , Convert a string into a byte array and write it to
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// Release resources
fos.close();
}
// After writing the data line feed operation
private static void method1() throws IOException {
// Create byte output stream object
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt");
// void write(int b) Write one byte of data at a time
fos.write(97);
// Because the byte stream cannot write a string , Convert a string into a byte array and write it to
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// Release resources
fos.close();
}
}
3 Byte input stream
3.1 Byte input stream introduction
Byte input stream class
- InputStream class : The topmost class of the byte input stream , abstract class
— FileInputStream class : FileInputStream extends InputStream
- InputStream class : The topmost class of the byte input stream , abstract class
Construction method
- public FileInputStream(File file) : from file Read data from the path of type
- public FileInputStream(String name) : Read data from string path
step
- Create an input stream object
- Reading data
- Release resources
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.IOException; /* Byte input stream write data quick start : Read one byte at a time The first part : Byte input stream class InputStream class : The topmost class of the byte input stream , abstract class --- FileInputStream class : FileInputStream extends InputStream The second part : Construction method public FileInputStream(File file) : from file Read data from the path of type public FileInputStream(String name) : Read data from string path The third part : Byte input stream steps 1 Create an input stream object 2 Reading data 3 Release resources */ public class FileInputStreamDemo1 { public static void main(String[] args) throws IOException { // Create byte input stream object // The read file must exist , If it doesn't exist, it's wrong FileInputStream fis = new FileInputStream("day11_demo\\a.txt"); // Reading data , Read a byte from the file // Back to a int Bytes of type // If you want to read characters , A strong turn is needed int by = fis.read(); System.out.println((char) by); // Release resources fis.close(); } }
3.2 Byte input stream reads multiple bytes
package com.itheima.inputstream_demo;
import java.io.FileInputStream;
import java.io.IOException;
/*
Byte input stream write data quick start : Read multiple bytes
The first part : Byte input stream class
InputStream class : The topmost class of the byte input stream , abstract class
--- FileInputStream class : FileInputStream extends InputStream
The second part : Construction method
public FileInputStream(File file) : from file Read data from the path of type
public FileInputStream(String name) : Read data from string path
The third part : Byte input stream steps
1 Create an input stream object
2 Reading data
3 Release resources
*/
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// Create byte input stream object
// The read file must exist , If it doesn't exist, it's wrong
FileInputStream fis = new FileInputStream("day11_demo\\a.txt");
// Reading data , Read a byte from the file
// Back to a int Bytes of type
// If you want to read characters , A strong turn is needed
// int by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
//
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// Cycle improvement
int by;// Record the bytes read each time
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
// Release resources
fis.close();
}
}
3.3 A copy of the picture
package com.itheima.inputstream_demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/* demand : hold " Picture path \xxx.jpg" Copy to the current module analysis : Copy file , In fact, read the contents of the file from a file ( data source ), And then write it to another file ( Destination ) data source : xxx.jpg --- Reading data --- FileInputStream Destination : Module name \copy.jpg --- Writing data --- FileOutputStream */
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// Create byte input stream object
FileInputStream fis = new FileInputStream("D:\\ Spreading wisdom Podcast \\ Installation package \\ Nice picture \\liqin.jpg");
// Create a byte output stream
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg");
// Read and write one byte at a time
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// Release resources
fis.close();
fos.close();
}
}
3.4 Exception capture processing
JDK7 Before version : Manual resource release
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* demand : Use the capture method to process the code of the last assigned image */ public class FileInputStreamDemo4 { public static void main(String[] args) { FileInputStream fis = null ; FileOutputStream fos = null; try { // Create byte input stream object fis = new FileInputStream("D:\\ Spreading wisdom Podcast \\ Installation package \\ Nice picture \\liqin.jpg"); // Create a byte output stream fos = new FileOutpu tStream("day11_demo\\copy.jpg"); // Read and write one byte at a time int by; while ((by = fis.read()) != -1) { fos.write(by); } } catch (IOException e) { e.printStackTrace(); } finally { // Release resources if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } // Release resources if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }JDK7 Version optimization processing method : Automatically release resources
JDK7 After optimization, you can use try-with-resource sentence , This statement ensures that each resource is automatically closed at the end of the statement .
Simple understanding : Use this statement , Will automatically release resources , You don't have to write it yourself finally Code blockBe careful : The use premise is that the resource type must be AutoCloseable Implementation class of interface
Format :
Format : try ( Create a stream object statement 1 ; Create a stream object statement 2 ...) { // Read and write data } catch (IOException e) { Code to handle exceptions ... } give an example : try ( FileInputStream fis1 = new FileInputStream("day11_demo\\a.txt") ; FileInputStream fis2 = new FileInputStream("day11_demo\\b.txt") ) { // Read and write data } catch (IOException e) { Code to handle exceptions ... }
Code practice
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* JDK7 Version optimization processing method demand : Use the capture method to process the code of the last assigned image */ public class FileInputStreamDemo5 { public static void main(String[] args) { try ( // Create byte input stream object FileInputStream fis = new FileInputStream("D:\\ Spreading wisdom Podcast \\ Installation package \\ Nice picture \\liqin.jpg"); // Create a byte output stream FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg") ) { // Read and write one byte at a time int by; while ((by = fis.read()) != -1) { fos.write(by); } // Release resources , It was found that it was already gray , Prompt for extra code , So use try-with-resource Mode will automatically close the flow // fis.close(); // fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.4 Byte input stream reads one byte array at a time
FileInputStream class :
- public int read(byte[] b) : Read most from input stream b.length Bytes of data , The number of data actually read is returned
package com.itheima.inputstream_demo; import javax.sound.midi.Soundbank; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /* FileInputStream class : public int read(byte[] b): 1 Read most from input stream b.length Bytes of data 2 The number of data actually read is returned */ public class FileInputStreamDemo6 { public static void main(String[] args) throws IOException { // Create byte input stream object FileInputStream fis = new FileInputStream("day11_demo\\a.txt"); // public int read(byte[] b): // 1 Read most from input stream b.length Bytes of data // 2 The number of data actually read is returned byte[] bys = new byte[3]; // int len = fis.read(bys); // System.out.println(len);// 3 // System.out.println(new String(bys));// abc // // len = fis.read(bys); // System.out.println(len);// 2 // System.out.println(new String(bys));// efc System.out.println("========== Code improvements ==============="); // int len = fis.read(bys); // System.out.println(len);// 3 // System.out.println(new String(bys, 0, len));// abc // // len = fis.read(bys); // System.out.println(len);// 2 // System.out.println(new String(bys, 0, len));// ef System.out.println("========== Code improvements ==============="); int len; while ((len = fis.read(bys)) != -1) { System.out.print(new String(bys , 0 , len)); } fis.close(); } }The code for copying pictures is improved by reading and writing a byte array at a time
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* demand : The code for copying pictures is improved by reading and writing a byte array at a time FileInputStream class : public int read(byte[] b): 1 Read most from input stream b.length Bytes of data 2 The number of data actually read is returned */ public class FileInputStreamDemo7 { public static void main(String[] args) throws IOException { // Create byte input stream object FileInputStream fis = new FileInputStream("D:\\ Spreading wisdom Podcast \\ Installation package \\ Nice picture \\liqin.jpg"); // Create a byte output stream FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg"); byte[] bys = new byte[1024]; int len;// The number of data actually read each time int by; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } // Release resources fis.close(); fos.close(); } }
4 Byte buffer stream
4.1 Byte buffer stream Overview
Byte buffer stream :
- BufferOutputStream: Buffering the output stream
- BufferedInputStream: Buffered input stream
Construction method :
- Byte buffered output stream :BufferedOutputStream(OutputStream out)
- Byte buffered input stream :BufferedInputStream(InputStream in)
Why a constructor needs a byte stream , Not a specific file or path ?
- Byte buffer streams only provide buffers , No read / write function , The real reading and writing data also depends on the basic byte stream object
4.2 Byte buffer stream case
package com.itheima.bufferedstream_demo;
import java.io.*;
/* Byte buffer stream : BufferOutputStream: Buffering the output stream BufferedInputStream: Buffered input stream Construction method : Byte buffered output stream :BufferedOutputStream(OutputStream out) Byte buffered input stream :BufferedInputStream(InputStream in) Why a constructor needs a byte stream , Not a specific file or path ? Byte buffer streams only provide buffers , No read / write function , The real reading and writing data also depends on the basic byte stream object demand : Use buffered streams to copy files */
public class BufferedStreamDemo1 {
public static void main(String[] args) throws IOException {
// Create efficient byte input stream objects
// In the bottom layer, a length of 8192 Array of
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ Spreading wisdom Podcast \\ Installation package \\ Nice picture \\liqin.jpg"));
// Create efficient byte output streams
// In the bottom layer, a length of 8192 Array of
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.jpg"));
// Use efficient streams , Read and write one byte at a time
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// byte[] bys = new byte[1024];
// int len;// The number of data actually read each time
// while ((len = bis.read(bys)) != -1) {
// bos.write(bys, 0, len);
// }
// Release resources
// At the bottom, the basic flow will be closed
bis.close();
bos.close();
}
}
4.3 The buffer stream reads and writes one byte at a time

4.4 Buffer stream reads and writes one byte array at a time

- The efficiency comparison of four ways to copy video files
package com.itheima.bufferedstream_demo;
import java.awt.image.DataBufferDouble;
import java.io.*;
/* demand : hold “xxx.avi” Copy to... In the module directory “copy.avi” , There are four ways to copy files , Time spent printing Four ways : 1 The basic byte stream reads and writes one byte at a time : The time spent is :196662 millisecond 2 The basic byte stream reads and writes one byte array at a time : The time spent is :383 millisecond 3 The buffer stream reads and writes one byte at a time : The time spent is :365 millisecond 4 The buffer stream reads and writes an array of bytes at a time : The time spent is :108 millisecond */
public class BufferedStreamDemo2 {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
// method1();
// method2();
// method3();
method4();
long endTime = System.currentTimeMillis();
System.out.println(" The time spent is :" + (endTime - startTime) + " millisecond ");
}
// 4 The buffer stream reads and writes an array of bytes at a time
private static void method4() throws IOException {
// Create an efficient byte input stream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
// Create efficient byte output streams
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
// Read and write one byte array at a time
byte[] bys = new byte[1024];
int len;// The number of data actually read each time
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// Release resources
bis.close();
bos.close();
}
// 3 The buffer stream reads and writes one byte at a time
private static void method3() throws IOException {
// Create an efficient byte input stream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
// Create efficient byte output streams
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
// Read and write one byte at a time
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// Release resources
bis.close();
bos.close();
}
// 2 The basic byte stream reads and writes one byte array at a time
private static void method2() throws IOException {
// Create a basic byte input stream object
FileInputStream fis = new FileInputStream("D:\\a.wmv");
// Create a basic byte output stream object
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
// Read and write one byte array at a time
byte[] bys = new byte[1024];
int len;// The number of data actually read each time
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// Release resources
fis.close();
fos.close();
}
// 1 The basic byte stream reads and writes one byte at a time
private static void method1() throws IOException {
// Create a basic byte input stream object
FileInputStream fis = new FileInputStream("D:\\a.wmv");
// Create a basic byte output stream object
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
// Read and write one byte at a time
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// Release resources
fis.close();
fos.close();
}
}
5 Properties aggregate
5.1 Properties Overview of sets
properties It's a Map The set class of the system
public class Properties extends Hashtable <Object,Object>
Why is it IO Flow part learning Properties
- Properties There's something in it IO Related methods
Use as a two column set
- You don't need to add generics , Only string is stored in the work
package com.itheima.properties_demo; import java.util.Map; import java.util.Properties; import java.util.Set; /* 1 properties It's a Map The set class of the system - `public class Properties extends Hashtable <Object,Object>` 2 Why is it IO Flow part learning Properties - Properties There's something in it IO Related methods 3 Use as a two column set - You don't need to add generics , Only string is stored in the work */ public class PropertiesDemo1 { public static void main(String[] args) { // Create a collection object Properties properties = new Properties(); // Additive elements properties.put("it001" , " Zhang San "); properties.put("it002" , " Li Si "); properties.put("it003" , " Wang Wu "); // Ergodic set : Key search Set<Object> set = properties.keySet(); for (Object key : set) { System.out.println(key + "---" + properties.get(key)); } System.out.println("========================"); // Ergodic set : Get the collection of objects , Get key and value Set<Map.Entry<Object, Object>> set2 = properties.entrySet(); for (Map.Entry<Object, Object> entry : set2) { Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + "---" + value); } } }
5.2 Properties As a unique method of collection
- Object setProperty(String key, String value) Set the key and value of the collection , All are String type , amount to put Method
- String getProperty(String key) Use the key specified in this property list to search for properties , amount to get Method
- Set stringPropertyNames() Returns a non modifiable key set from the attribute list , Where the key and its corresponding value are strings , amount to keySet Method
package com.itheima.properties_demo;
import java.util.Properties;
import java.util.Set;
/* Properties As a unique method of collection Object setProperty(String key, String value) Set the key and value of the collection , All are String type , amount to put Method String getProperty(String key) Use the key specified in this property list to search for properties , amount to get Method Set<String> stringPropertyNames() Returns a non modifiable key set from the attribute list , Where the key and its corresponding value are strings , amount to keySet Method */
public class PropertiesDemo2 {
public static void main(String[] args) {
// Create a collection object
Properties properties = new Properties();
// Additive elements
properties.setProperty("it001", " Zhang San ");
properties.setProperty("it002", " Li Si ");
properties.setProperty("it003", " Wang Wu ");
// Ergodic set : Key search
Set<String> set = properties.stringPropertyNames();
for (String key : set) {
System.out.println(key + "---" + properties.getProperty(key));
}
}
}
5.3 properties neutralization IO Related methods
- void load(InputStream inStream) As a byte stream , Match the key value in the file , Read into collection
- void load(Reader reader) As a character stream , Match the key value in the file , Read into collection
- void store(OutputStream out, String comments) Set the key value pairs in the set , Write to file as byte stream , Parameter 2 is annotation
- void store(Writer writer, String comments) Set the key value pairs in the set , Write to file as character stream , Parameter 2 is annotation
package com.itheima.properties_demo;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/* Properties and IO The method of stream combination void load(InputStream inStream) As a byte stream , Match the key value in the file , Read into collection //void load(Reader reader) As a character stream , Match the key value in the file , Read into collection void store(OutputStream out, String comments) Set the key value pairs in the set , Write to file as byte stream , Parameter 2 is annotation //void store(Writer writer, String comments) Set the key value pairs in the set , Write to file as character stream , Parameter 2 is annotation */
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
// establish Properties A collection of objects
Properties properties = new Properties();
// void load(InputStream inStream) As a byte stream , Match the key value in the file , Read into collection
properties.load(new FileInputStream("day11_demo\\prop.properties"));
// Print the data in the set
System.out.println(properties);
}
}
package com.itheima.properties_demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/* Properties and IO The method of stream combination void load(InputStream inStream) As a byte stream , Match the key value in the file , Read into collection //void load(Reader reader) As a character stream , Match the key value in the file , Read into collection void store(OutputStream out, String comments) Set the key value pairs in the set , Write to file as byte stream , Parameter 2 is annotation //void store(Writer writer, String comments) Set the key value pairs in the set , Write to file as character stream , Parameter 2 is annotation */
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("zhangsan" , "23");
properties.setProperty("lisi" , "24");
properties.setProperty("wangwu" , "25");
properties.store(new FileOutputStream("day11_demo\\prop2.properties") , "userMessage");
}
private static void method1() throws IOException {
// establish Properties A collection of objects
Properties properties = new Properties();
// void load(InputStream inStream) As a byte stream , Match the key value in the file , Read into collection
properties.load(new FileInputStream("day11_demo\\prop.properties"));
// Print the data in the set
System.out.println(properties);
}
}
6 ResourceBundle Tool class
The goal is :
- Be able to use ResourceBundle The tool class can quickly read the value of the property file
summary :
java.util.ResourceBundle It's an abstract class , We can use its subclasses PropertyResourceBundle To read to .properties Profile at the end .
Get objects directly through static methods :
static ResourceBundle getBundle(String baseName) You can directly get the attribute resources in the default locale according to the name .
Parameter note : baseName
1. The property set name does not contain an extension .
2. The property set file is in src In the directory
such as :src There is a file in user.properties
ResourceBundle bundle = ResourceBundle.getBundle("user");
ResourceBundle Common methods in :
String getString(String key) : Pass key , Get the corresponding value
Code practice
adopt ResourceBundle Tool class
Put a properties file Put it in src Directory , Use ResourceBundle To get the key value pair data
package com.itheima.resourcebundle_demo;
import java.util.ResourceBundle;
/* 1 java.util.ResourceBundle : It's an abstract class We can use its subclasses PropertyResourceBundle To read to .properties Profile at the end 2 static ResourceBundle getBundle(String baseName) You can directly get the attribute resources in the default locale according to the name . Parameter note : baseName 1. The property set name does not contain an extension . 2. The property set file is in src In the directory such as :src There is a file in user.properties ResourceBundle bundle = ResourceBundle.getBundle("user"); 3 ResourceBundle Common methods in : String getString(String key) : Pass key , Get the corresponding value advantage : Quickly read the values of the properties file demand : adopt ResourceBundle Tool class Put a properties file Put it in src Directory , Use ResourceBundle To get the key value pair data */
public class ResourceBundleDemo {
public static void main(String[] args) {
// public static final ResourceBundle getBundle(String baseName)
// baseName : properties Name of file , Be careful : The extension does not need to be added , properties Must be in src Under the root directory of
ResourceBundle resourceBundle = ResourceBundle.getBundle("user");
// String getString(String key) : Pass key , Get the corresponding value
String value1 = resourceBundle.getString("username");
String value2 = resourceBundle.getString("password");
System.out.println(value1);
System.out.println(value2);
}
}
Summary of content
If you want to use ResourceBundle Load properties file , Where does the properties file need to be placed ?
src Root directoryPlease describe the use of ResourceBundle What are the general steps to get the attribute values ?
1 obtain ResourceBundle object 2 adopt ResourceBundle Class getString(key) : Find the value according to the key
7 recursive
Recursion is the method itself , Recursion must have a stop condition ( exit ), Otherwise StackOverflowError abnormal
The benefits of recursion :
Turn a complex problem layer by layer into a smaller problem similar to the original problem to solve
Small problems solved , Big problems will be solved one by one
Recursion considerations :
- Recursive export : Otherwise, a stack overflow exception will occur
- Recursive rule : You need to find a smaller problem that is similar to the original problem
// Find the factorial
public class Test1 {
public static void main(String[] args) {
int result = jiecheng(5);
System.out.println(result);
}
private static int jiecheng(int n) {
if (n == 1){
// exit
return 1;
}else {
return n * jiecheng(n - 1);
}
}
}
// Recursively delete folders and their contents
public class Test2 {
public static void main(String[] args) {
File file = new File("basic-code\\date\\directory");
deleteFiles(file);
}
// Accept one file route , Delete all contents under this path
public static void deleteFiles(File file){
// First get the documents of all the sons
File[] files = file.listFiles();
// Improve code robustness
if (files == null){
return;
}
// Traversal array
for (File f : files) {
if (f.isFile()){
// If it's a file , Delete directly
f.delete();
}else{
// Catalog
deleteFiles(f);
}
}
// remove the current directory
file.delete();
}
}
边栏推荐
- CMakeLists 基础
- Distributed machine learning: model average Ma and elastic average easgd (pyspark)
- 异常类_日志框架
- 2022蓝桥杯国赛B组-2022-(01背包求方案数)
- 药品管理系统加数据库,一夜做完,加报告
- Half year inventory of new consumption in 2022: the industry is cold, but these nine tracks still attract gold
- Arcmap操作系列:80平面转经纬度84
- [Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)
- Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
猜你喜欢

Bc1.2 PD protocol
![[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection](/img/c2/d6760826b81589781574aebff61f9a.png)
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection

声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏

猎头5万挖我去VC

容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
![[machine learning] K-means clustering analysis](/img/5f/3199fbd4ff2129d3e4ea518812c9e9.png)
[machine learning] K-means clustering analysis

Hologres shared cluster helps Taobao subscribe to the extreme refined operation

Mathematical modeling for war preparation 36 time series model 2

TCP Socket与TCP 连接

异常类_日志框架
随机推荐
MicroBlaze serial port learning · 2
register_chrdev和cdev_init cdev_add用法区别
In order to make remote work unaffected, I wrote an internal chat room | community essay
POJ Project Summer
JS Es5 can also create constants?
Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
15年做糊21款硬件,谷歌到底栽在哪儿?
RT thread heap size Setting
ArcMap operation series: 80 plane to latitude and longitude 84
名单揭晓 | 2021年度中国杰出知识产权服务团队
备战数学建模35-时间序列预测模型
备战数学建模34-BP神经网络预测2
php7.3 centos7.9安装sqlserver扩展
Etcd教程 — 第九章 Etcd之实现分布式锁
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
BC1.2 PD协议
抖快B为啥做不好综艺
Tutoriel etcd - chapitre 8 API compacte, Watch et lease pour etcd
Two methods for MySQL to open remote connection permission