Java from getting started to giving up JavaSE entry: file operations,
File Operations in Java are a little complicated ···
But it doesn't matter. I will make it very simple !!!
Before talking about file operations in Java, we should first understand a concept-"stream". For example, we need to pour water from one cup to another cup of the same size, it is processed as a stream (if you have frozen the water into ice and then put it in it, I will not say it --).
In fact, my understanding is very simple. "stream" is a string of data. As to the size of each data unit in this string, Java provides two solutions:Byte stream and byte stream.
Whether it is a byte stream or a batch stream, the processing method can be dividedInput stream and output stream. In this article, we will simply learn the input and output of the byte stream and the ghost stream.
1. byte stream
All bytes know?
What? I don't know !!! Well, I don't know if I don't know. Simply put, the minimum unit of data stored by a computer is bit, which is a binary unit with values of 0 and 1.
A Byte is a Byte (note that this is Big B, that is small B), and a Byte occupies 8 bits. What are the differences between 32-bit and 64-bit operating systems in terms of data processing? The minimum unit of 32-bit processing is 4 bytes, 64 is 8 bytes. There are many differences. We don't need to elaborate on them here. If you are interested, start Baidu !!!
In fact, the file operation is very simple, just like the steps for putting elephants in the refrigerator in Zhao Benshan's sketch.
Step 1: open the refrigerator door
Step 2: Put the elephant in (or take it out)
Step 3: Close the refrigerator
Are these three steps super simple !!!
Because the steps for reading and writing files are the same, but the classes used are different, let's take a look at how to copy files.
123456789101112131415161718192021222324252627282930 |
/** * Copy a file in byte stream mode (any file can be copied) * @ Param oldFile: The file is copied. * @ Param newFile: the copied File */ public static void copyFile(String oldFile, String newFile){ File srcFile = new File(oldFile); // Source object File destFile = new File(newFile); // Target file object try { if (!(destFile.exists())) { // Determine whether the target file exists destFile.createNewFile(); // Create a new file if it does not exist } // Use the source object to create a file input stream object FileInputStream fis = new FileInputStream(srcFile); // Create a file output stream object using the target file object FileOutputStream fos = new FileOutputStream(destFile); byte [] buf = new byte [ 1024 ]; // Create a byte array as a temporary buffer and read the data volume each time System.out.println( "Start copying files ..." ); while (fis.read(buf) != - 1 ) { // Read data from the input file stream cyclically.-1 indicates that the file has been read. fos.write(buf); // Write to the file output stream } System.out.println( "The file has been copied! " ); fis.close(); // Close the stream fos.close(); } catch (Exception e) { e.printStackTrace(); } } |
Let's briefly introduce several objects involved in it.
1. File class: Indicates a file or a directory. The common methods are as follows:
2. FileInputStream class: Inherits from the InputStream class and is used to read files. data in files is input to the memory in bytes.
3. FileOutputStream class: Inherits from the OutputStream class, which is used to write files and outputs data in memory to files in byte mode.
Note: because it is read or written in bytes, files cannot be used during processing, just as we download files online..
Ii. Ghost stream
The ghost stream is easy to understand. It processes data in the smallest unit of characters and is generally used to process text files. Many novel readers use this method to process TXT novels.
Next, write two methods to process text files in the form of a forward stream.
2.1 read the file content row by row and display it on the console
1234567891011121314151617181920212223 |
/** * Read text file content * @ Param path: file path */ public static void txtRead(String path){ File file = new File(path); try { // Create a file through the file object to read the Stream Object FileReader fr = new FileReader(file); // Package the File Read stream into a buffer read stream BufferedReader br = new BufferedReader(fr); String str; // Read data row by row while ((str = br.readLine()) != null ){ System.out.println(str); } br.close(); // Close the stream fr.close(); // Close the stream } catch (Exception e) { e.printStackTrace(); } } |
2.2 Save the input text to a file
123456789101112131415161718192021 |
/** * Write a text file * @ Param path: file path */ public static void txtWriter(String path){ File file = new File( "test.txt" ); try { // Create a file and output the upload Stream object through the file object FileWriter fw = new FileWriter(file); // Package the file output response stream into a buffer stream BufferedWriter bw = new BufferedWriter(fw); bw.write( "Hello everyone! " ); bw.write( "I am planning to give up Java ," ); bw.newLine(); // Change the new line bw.write( "Please kindly advise. " ); bw.close(); // Close the stream fw.close(); // Close the stream } catch (Exception e) { e.printStackTrace(); } } |
OK. Here is the basic file operations. In fact, one of the more practical methods of development is:Serialization and deserializationThe code for this operation is relatively simple. I will not introduce it here. If you are interested, Baidu.
"Software thinking" blog address:51CTO,BlogInterested friends can visit other related blog posts.