Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the read and write operation of Java IO stream creation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Java IO stream creation read and write operation is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java IO stream creation read and write operation is what" it!

Concept

IO stream can be understood as the transfer between data, we will enter a group of data: 1234567, transfer them from the hello file to the file, and use the program method to transfer them, that is, a byte-by-byte transfer is required, that is, we can only pass in or read one byte at a time, which is the general flow of the io stream. Io stream can read any type of file. Such as: text files, pictures, songs mp3, video and so on.

Because the io stream is read byte by byte, we need to use the byte single-byte variable to get the length. If you get too much content, you need to use the corresponding array.

The method of io flow correspondence

All io stream methods need to write the corresponding file operation path, and all io stream methods have a common parent class interface (Exception), so we need to link the corresponding interfaces such as:

Public static void main (String [] args) throws Exception

1. File method (create file)

Declaration method:

File file1 = new File ("D:\\ java production\\ Advanced Features\\ hehe.txt")

The File method is mainly used to create files, and when using this method, you must enter the specific path of the file to be created. We need to write down the corresponding file type suffix. If there is no path, the default is the folder format. The method to create a file is as follows:

File.createNewFile (); / / create the corresponding file file.mkdirs (); / / create a folder

.createNewFile (): if and only if a file with that name does not exist, a corresponding file will be created in the corresponding path

.mkdirs (): if and only if a folder with that name does not exist, a corresponding folder will be created in the corresponding path

Since the File method has a way to create a file, it is also necessary to remove the method to determine whether the file exists or not.

Boolean decide = file.exists (); / / determine whether the file exists file.delete (); / / delete the file

.upload (): test whether the file represented by this path exists, and return true if it exists. Otherwise, return false.

.delete (): delete files or folders under this abstract path

In addition, there are some corresponding methods to view files, such as name, path, and size.

System.out.println ("File name:" + file.getName ()); System.out.println ("relative path:" + file.getPath ()); System.out.println ("absolute path:" + file.getAbsolutePath ()); System.out.println ("File size:" + file.length () + "bytes")

.getName (): according to the meaning of the English word, you can know that this method is to get the file name of the corresponding file.

.getPath (): converts the path of the corresponding file to a string

.getAbsolutePath (): converts the absolute path of the corresponding file to a string format, which is more accurate than the above method.

.length (): returns the length of the file, that is, the length of internal bytes.

2. FileInputStream (get byte method) input stream

Declaration method:

FileInputStream fis = new FileInputStream ("D:\\ java production\\ Advanced Features\\ hello.txt")

FileInputStream is a method used to read the contents of bytes in a file, and when using this method, you must enter the specific path of the file to be created. We usually read and write the internal content as follows:

Byte [] data = new byte [fis.available ()]; / / get the contents of the file and store them in bytes, such as System.out.println ((char) fis.read ()) in the byte [] array; / / read out the corresponding bytes and output as char / / cycle through all the bytes byte [] data = new byte [fis.available ()]; for (int I = 0; I < data.length) System.out.print +) {System.out.print ((char) fis.read ());} System.out.println ((char) fis.read (data,0,data.length))

.available (): reads the remaining number of bytes, and must use the byte [] array to store the corresponding length, because byte is used for special processing of bytes, this method reads the number of bytes, although there is no error in the output, there will be errors in the method loop.

.read (): read the file in the first byte, because it is a byte format so we need to use char (single character variable) conversion, its output, note that each time can only read one, and will not read the same position of the byte, each read one will be less, if after reading continue to read will allow a black border space. You can also call the corresponding subscript byte on demand, such as the last line of code above.

Fis.close ()

.close (): close the input stream of this file and release any system resources associated with the stream. The input stream has been opened by default when we refer to the FileInputStream stream. When we do not use it, we should close it, just as we need to open the door when we enter the holiday and close the door when we leave the room after we enter and take the needed files, otherwise it will always be open and occupy the performance of the computer.

3. FileOutputStream (write byte method) output stream

Declaration method:

FileOutputStream fos = new FileOutputStream ("D:\\ java production\\ Advanced Features\\ hehe.txt")

FileInputStream is used to read the contents of bytes in a file, and when using this method, you must enter the specific path of the file to be created. We usually write the internal content in the following way:

String str = "up every day"; / / store the content that needs to be stored into the variable byte [] words = str.getBytes (); / / change the string into bytes to store fos.write (words); / / finally store the content in bytes.

.write (): save the specified content into the file output stream and then save it to the file by the output stream. When saving, we need to convert the file format to a computer-readable way, octal bytes, so we need to use byte to convert the stored content to octal bytes.

Fos.close (); / / close the output stream

.close (): close the output stream of this file and release any system resources associated with the stream before we refer to the

The FileInputStream stream has been opened by default. When we don't use it, we should close it, just like when we enter the holiday, we need to open the door, and when we enter and take away the necessary files, we have to close the door when we leave the room, otherwise it will always open and occupy the performance of the computer.

Thank you for your reading, the above is the content of "what is the read and write operation of Java IO stream creation". After the study of this article, I believe you have a deeper understanding of what the read and write operation of Java IO stream creation is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report