Original link Author: Jakob Jenkov Translator: Li Jing ([email protected])
In a Java application, a file is a common source of data or a medium for storing data. So this section will provide a brief overview of the use of files in Java. This article will not explain every technical detail, but will provide you with the necessary knowledge points for the method of file access. In a later article, these methods or classes are described in more detail, including method examples, and so on.
throughJava io Read file
If you need to read files between different ends, you can choose to use FileInputStream or FileReader depending on whether the file is a binary or a text file. These two classes allow you to read one byte or character at a time from the beginning of a file to the end of a file, or to write a read to a byte array or an array of characters. Instead of reading the entire file at once, you can sequentially read the bytes and characters in the file.
If you need to read some parts of the file in leaps and bounds, you can use Randomaccessfile.
throughJava io Write file
If you need to write files between different ends, you can choose FileOutputStream or FileWriter depending on whether the data you want to write is binary or character data. You can write one byte or character to a file at a time, or you can write a byte array or character data directly. Data is stored in the file in the order in which they are written.
throughJava io random Access file
As I mentioned, you can randomly access files via Randomaccessfile.
Random access does not mean that you can read and write in a really random location, it simply means that you can skip some parts of the file and support simultaneous reads and writes without requiring a particular access order. This allows Randomaccessfile to overwrite portions of a file, append content to the end of it, or delete some of its contents, and of course, it can start reading files from anywhere in the file.
access to file and directory information
Sometimes you may need to read the file's information instead of the contents of the file, for example, if you need to know the size of the file and the properties of the file. The same is true for catalogs, such as you need to get a list of files in a directory. You can get information about files and directories through the file class.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java IO: File
Java IO: File