Java: working with files - write, read, delete

Java has many tools for working with files, since they serve as a data warehouse in many programs. Especially often access to files is required in Java Android. All classes for working with files are located in the java.io package, where classes for working with input and output streams are also stored. Strictly speaking, in Java, working with files occurs primarily through byte and character I / O streams. The exception in this case is the File class. We’ll start with him.

File Java class - working with files

The File class interacts directly with files and the file system. An instance of this class is used primarily to access the file properties and move through the hierarchy of subdirectories. It does not define how to extract and save data to a file, but using this class you can get a lot of information about the file itself: date, time, access rights and directory path. In essence, the File class is an object interpretation of a file or directory in Java.

To create an instance of the File class, use one of its constructors: File (String is the path to the folder, String is the file name).

Consider the main methods of this class that allow you to obtain information about a file or directory:

  • getName () - returns the file name;
  • exists () - returns the boolean value true if the file exists, or false otherwise;
  • isFile () - determines whether the object points to a file, returning the corresponding logical value;
  • isDirectory () - returns true if it is a folder;
  • canWrite () - returns true if the file is writable;
  • canRead () - returns true if the file is readable;
  • setReadOnly () - makes the file read-only;
  • length () - returns the file size in bytes;
  • renameTo (File new name) - renames the current file in accordance with the passed argument. Returns true if the operation is successful;
  • delete () - deletes a file or folder (if it is empty), which the calling object points to;
  • list () - retrieves a list of the names of objects stored in this directory as an array of strings (applicable exclusively to directories).

Thus, using the File class, Java provides work with files and folders.

Working with files through the FileInputStream and FileOutputStream classes

We mentioned earlier that in Java, working with files is primarily done through input / output streams. Byte streams for working with files are presented in the form of classes FileInputStream and FileOutputStream. These classes are the heirs of the base abstract classes InputStream and OutputStream, respectively, therefore the methods of these classes are available for working with files.

Consider FileInputStream first. Constructors of this class are presented in the figure below:

java file handling

The first constructor takes as an argument the path to the target file as a string, and the second as an object representation. Although the first constructor is used more often, in the second case it is possible to study the properties of a file by methods available in the File class. When you instantiate the FileInputStream class, it opens an input stream for reading a file.

The constructors of the FileOutputStream class are shown below:

java android

The situation is similar to FileInputStream, but it can also take the logical value β€œappend”, which if true means that the data written to the target file will be added to the existing one, and if false, the file will be completely overwritten, i.e. old data will not be saved.

Let's look at an example using these classes:

java file handling example

The FileInputStream input stream reads data from file1.txt byte-by-bit using the read () method. Each byte read is stored in an integer variable. Further, in the body of the while loop, this variable is passed as an argument to the write method of the FileOutputStream instance, which writes the received byte to file2.txt. In the end, both threads are necessarily closed using the close method.

Classes FileReader and FileWriter

If you know that when working with files you will deal with text, then instead of byte streams it makes sense to use symbolic ones. These streams are represented by the FileReader and FileWriter classes in Java. Working with files using these classes is approximately the same as in the previous case. They are descendants of the Reader and Writer classes, respectively, which define the basic methods for inputting and outputting data.

The constructors of the FileReader and FileWriter classes are shown in the figure below:

java work with files and folders

You must admit that there are practically no differences in semantics compared to byte streams. The bottom line is that these classes are designed specifically for working with Unicode characters, which byte streams cannot do. This is how file handling is organized in Java. An example of using these classes is given below:

java file handling

As you can see, there are no differences. It is worth noting that although the streams are symbolic, the read method returns, and the write method takes an integer value. Although the signature of the write method involves accepting an argument of type int, only the lower 16 bits are written to the stream, since characters in Java are represented by just that many bits.

Conclusion

So, we have analyzed the main classes for working with files in Java. It is worth noting that if you are going to develop software for Java Android, you can safely use this toolkit.


All Articles