The stream takes the memory as the object,
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bdtvo6ws-1620884654001) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506081448354. PNG)]
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bmo7q08a-1620884654003) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506081600249. PNG)]
[external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-x5wcvhlf-1620884654004) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506092005527. PNG)]
mkdir,,, and mkdirs are used to create the first level directory, and mkdirs is used to create the multi-level directory
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-sx6cw4qg-1620884654007) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506095216592. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-escmvm3x-1620884654008) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210506100127015. PNG)]
Byte stream and character stream
Use byte stream to transfer files, fine and lossless operation
Generally, character stream is used to transfer files
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ci4n27pq-1620884654010) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506103048829. PNG)]
Use the stream object to associate the files in the hard disk. Operate the files through the stream. Be sure to close the stream after using the stream
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-nfxkso1u-1620884654011) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210506121758423. PNG)]
Byte stream reading data is inefficient and Chinese is garbled, because Chinese is composed of three bytes.
read() reads data one byte at a time. If there are no bytes to input, this method will block it. Return - 1, indicating that the reading is completed
public static void FileInputStram() { String filePath = "F:\\code\\javaIO Files created.txt"; int readData = 0; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read()) != -1){ System.out.print((char) readData); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ //Be sure to close the flow try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } Use byte array to receive data. How many bytes are received at a time is user-defined and fast FileInputStream fileInputStream = null; byte[] but = new byte[8]; try { fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read(but)) != -1){ System.out.println(new String(but,0,readData)); }
Byte output stream
String filePath = "F:\\code\\a.txt"; FileOutputStream fileOutputStream = null; try { //fileOutputStream = new FileOutputStream(filePath, true); Additional output //fileOutputStream = new FileOutputStream(filePath); This will overwrite the contents of the original file fileOutputStream = new FileOutputStream(filePath); String txt = "Hello.world"; //fileOutputStream.write('H'); Bytes can be converted to int fileOutputStream.write(txt.getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ptfiz5jn-1620884654011) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506135200605. PNG)]
Character stream, Reader
[external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-mpgnlgnj-1620884654012) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210506144627179. PNG)]
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-fhozbo0j-1620884654013) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506144806019. PNG)]
Character stream, fileReader
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-usmxxjei-1620884654013) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210506145512218. PNG)]
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ztdwtwjj-1620884654014) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210506151055784. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-cczpblym-1620884654014) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210506170345285. PNG)]
BufferedWriter has an attribute Writer in the buffer stream, that is, it can receive all subclasses of the Writer. The same is true of BufferedReader, which eliminates the restrictions of node stream on the format of data source
Process binary files (video, pictures) with byte stream. Processing text documents with character streams
BufferedReader reads files
String filePath = "F:\\code\\aa.txt"; //Create bufferedReader BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); //read String line;//Read by line, high efficiency BufferedReader Readline() reads files by line //When BufferedReader readLine(); When the return value is null, it indicates that the file has been read while ((line = bufferedReader.readLine()) != null){ System.out.println(line); } //Be sure to close the flow bufferedReader.close();//fileReader will close automatically
//1. BufferedReader and BufferedWriter operate according to characters //2. Do not use them to operate binary files, which may be damaged String inputPath = "F:\\code\\a.txt"; String outputPath = "F:\\code\\a1.txt"; BufferedReader bf = null; BufferedWriter bw = null; String line; try { bf = new BufferedReader(new FileReader(inputPath)); bw = new BufferedWriter(new FileWriter(outputPath)); while ((line = bf.readLine()) != null){ //Write one line for each line you read bw.write(line); bw.newLine(); } }catch(IOException e){ e.printStackTrace(); }finally { if (bf != null) { try { bf.close(); } catch (IOException e) { e.printStackTrace();
Use byte buffer stream (wrapper stream) to copy binary files,
//Copy pictures. Because pictures are binary, they have to be transmitted by byte stream String Apath = "F:\\code\\1.jpeg"; String Bpath = "F:\\code\\2.jpeg"; BufferedInputStream bi = null; BufferedOutputStream bo = null; //It is used to store the data read out at one time byte[] buf = new byte[1024]; int line = 0; try { bi = new BufferedInputStream(new FileInputStream(Apath)); bo = new BufferedOutputStream(new FileOutputStream(Bpath,true)); //When the result is - 1, it indicates that the file reading is completed while ((line = bi.read(buf)) != -1){ bo.write(buf,0,line); } } catch (Exception e) { e.printStackTrace(); }finally {//The open stream interface must be closed if (bi != null){ try { bi.close(); } catch (IOException e) { e.printStackTrace(); } } if (bo != null){ try { bo.close();
BufferedInputStream can operate on both binary files and text documents without garbled code. The character wrapper stream cannot manipulate binary files,
ObjectInputStream
[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-f8ujfzhr-1620884654015) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507093010081. PNG)]
When ObjectOutputStream writes to an object, the object needs to be serialized (implement the Serializable interface)
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-c1kq2coz-1620884654016) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210507094507611. PNG)]
Deserialization
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-pcf5ctgz-1620884654017) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-202105070954157. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-uki9l6w7-1620884654017) (C: \ users \ 47669 \ appdata \ roaming \ typora \ typora user images \ image-20210507095707894. PNG)]
[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-wryml4nh-1620884654018) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507100619992. PNG)]
matters needing attention
[external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-rsmgyywi-1620884654019) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507100815611. PNG)]
Handle the garbled code caused by the encoding problem of the character stream, because the byte stream processes the file in utf by default,
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG clxfacsx-1620884654019) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507132848830. PNG)]
Byte stream scrambling is solved, and the byte stream is converted into character stream
String Apath = "F:\\code\\a.txt"; byte[] buf = new byte[8]; int line; FileInputStream fis = new FileInputStream(Apath); while ((line = fis.read(buf)) != -1){ System.out.println(new String(buf,0,line)); } Output garbled code Hello.wo rld hand Pneumatic valve� �Fen Shi� ��husband The wrapper stream is used to convert the byte stream into character stream, and the encoding format can be specified String Apath = "F:\\code\\a.txt"; String line; //Can you change what text you open InputStreamReader psr = new InputStreamReader(new FileInputStream(Apath), "utf-8"); BufferedReader br = new BufferedReader(psr); while ((line = br.readLine()) != null){ System.out.println(line); } Be sure to close the flow br.close();
Interesting standard print stream (System.out.print)
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0ic993oc-1620884654020) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507152100713. PNG)]
Property file
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-eagu8rhz-1620884654020) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507153926325. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ljiaokqh-1620884654021) (C: \ users \ 47669 \ appdata \ roaming \ typora user images \ image-20210507161901852. PNG)]