In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of File classes and IO flows in Java", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of File classes and IO flows in Java".
IO stream:
Overview of IO streams:
IO: input / output (Input/Output)
Stream: an abstract concept, a general term for data transmission. In other words, the transmission of data between devices is called stream, the essence of flow is data transfer IO stream is used to deal with data transfer problems between devices, common applications: file copy, file upload, file download and so on, in a word, related to transmission, all involve streaming.
IO flow system diagram:
Since IO involves file operations, it must be inseparable from the technology of file manipulation:
File class:
The File class is the only object in the java.io package that represents the disk file itself. The File class defines methods to manipulate files, which are mainly used to obtain or process information related to disk files, such as file names, file paths, access permissions, and modification dates, as well as browsing the subdirectory hierarchy.
The File class represents information about processing files and file systems. The File class does not have the ability to read information from and write information to a file, it only describes the properties of the file itself. So it is paired with IO for reading and writing operations.
First, take a look at a summary diagram of the methods commonly used in the File class:
Create a file using createNewFile ():
Public class test01 {public static void main (String [] args) throws IOException {/ / first create a File object and pass in the path File file1 = new File ("G://abc.txt"); / / create an empty file, create a new one if it does not exist, and return True, and return false System.out.println (file1.createNewFile ()) if it exists;}
If the directory does not have this file after execution, one will be created and true will be returned, and if it already exists, false will be returned, indicating that the creation failed.
Create a directory using mkdir ():
File file2 = new File ("Granger file2.mkdir a"); System.out.println (file2.mkdir ()); / / create a directory, create a new one if it doesn't exist, and return True, and return false if it exists
Create a multi-level directory using mkdirs ():
File file3 = new File ("G://a//b//c"); System.out.println (file3.mkdirs ()); / / create a multi-level directory, create a new one if it does not exist, and return True, and false if it exists
Then we need to use the functions in the IO stream to import and export the file:
Let's start with four commonly used streams:
Byte input stream: InputStream
Byte output stream: OutputStream
Character input stream: Reader
Character output stream: Writer
Why are there two streams of bytes and characters?
In ASCII code, an English letter (regardless of case) is one byte, and a Chinese character is two bytes.
In UTF-8 coding, an English word is one byte and a Chinese word is three bytes.
In Unicode coding, one English is one byte and one Chinese is two bytes.
So we know that the data read by the computer is read one by one, when there are numbers or English in the file, it can be read out normally because it takes up one byte.
What if it's Chinese characters? It takes at least two bytes. If you split a Chinese character and read it, there must be something wrong with it.
Summary: if the data is opened through the notepad software included with Window, we can still read the contents and use the character stream, otherwise use the byte stream. If you don't know which type of stream to use, use byte streams!
The following is a summary of the method names of the four stream corresponding functions:
Byte output stream:
We use the byte output stream to write a sentence to the abc.txt file:
Public class test01 {public static void main (String [] args) {try {/ / create an output stream object: OutputStream fos = null; fos = new FileOutputStream ("G://abc.txt"); String str = "Today's blog is an IO stream" / / break the characters to be written into an array: byte [] words = str.getBytes (); / / use the write function fos.write (words);} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();}
After running:
Byte input stream (read from file to console):
We know that if there are Chinese characters in the file and the byte input stream is used, then the display must be garbled. If there are four words "I love China" in the file now, use the following code:
Public class test02 {public static void main (String [] args) {/ / create byte input stream object: InputStream fis = null; try {fis = new FileInputStream ("G://abc.txt"); int data / / fis.read () takes each byte and converts it into an integer between 0 and 255via the Ascll code table. No value returns-1 while ((data=fis.read ())! =-1) {/ / (char) data converts the read bytes into the corresponding characters / / Chinese characters are 2 + bytes to form System.out.print ((char) data) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {fis.close ();} catch (IOException e) {e.printStackTrace ();}
The result of the output is:
Then change the information in the document into English and numbers:
Conclusion: byte stream can not be used to read files with Chinese characters.
Character output stream:
We use the character output stream to write a few words into the abc.txt file:
Public class test03 {public static void main (String [] args) {try {/ / FileWriter write data using character output streams Writer fw = new FileWriter ("G://abc.txt"); fw.write ("We are learning Java"); fw.write ("come on"); fw.close () / / close the resource} catch (IOException e) {e.printStackTrace ();}
It's no use. We find it more convenient to use character stream to write Chinese characters.
Character input stream:
You can set the cache stream to improve the efficiency of getting values:
Public class test04 {public static void main (String [] args) throws IOException {/ / create character input stream object: Reader fr = null; try {fr = new FileReader ("G:/abc.txt") / / create a character cache with the help of the character stream object and put the characters one by one into the cache / / and then read and write to the program memory together, which is more efficient BufferedReader br = new BufferedReader (fr); / / first go to the cache to read String line = br.readLine () While (line! = null) {System.out.println (line); line = br.readLine ();}} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {fr.close () } catch (IOException e) {e.printStackTrace ();}
When the information in the file is multiple lines:
Summary of byte stream and character stream:
IO is the input and output of a file, and we need a stream if we want to write to a file or send a message to another user through a program.
IO stream is divided into byte stream and character stream. Byte stream is IO in bytes, and character stream is character stream in character units. IO; usually uses byte stream to read and write pictures, video and audio, etc., if you read and write the contents of a file, such as Chinese character stream is recommended.
The above is all the content of the article "sample Analysis of File classes and IO flows in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.