ComputersProgramming

Java: working with files - writing, reading, deleting

Java has many tools for working with files, because they serve as a data store in many programs. Especially often access to files is required in Java Android. All classes for working with files are located in the package java.io, which also stores classes for working with input and output streams. Strictly speaking, in Java, work with files occurs mainly through byte and character streams of input-output. An exception in this case is the File class. We'll start with it.

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 file properties and move hierarchies of subdirectories. It does not define how to extract and save data to a file, but with this class you can get a lot of information about the file itself: date, time, access rights and path to the directory. In fact, 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 path to the folder, String file name).

Let's consider the main methods of this class, which allow you to obtain information about a file or directory:

  • GetName () - returns the name of the file;
  • Exists () - returns a Boolean value true if the file exists, or false otherwise;
  • IsFile () - determines whether the object points to a file, returning the corresponding boolean value;
  • IsDirectory () - returns true if this 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 size of the file 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 the file or folder (if it is empty) pointed to by the calling object;
  • List () - retrieves a list of object names stored in this directory as an array of strings (applicable only to directories).

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

Working with files using the FileInputStream and FileOutputStream classes

Earlier we mentioned that in Java, work with files is carried out mainly through I / O streams. Byte streams for working with files are represented in the form of FileInputStream and FileOutputStream classes. These classes are the heirs of the basic abstract classes InputStream and OutputStream respectively, so the methods of these classes are available for working with files.

Consider first the FileInputStream. The constructors of this class are shown in the figure below:

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

The constructors of the FileOutputStream class are shown below:

The situation is similar to FileInputStream, but it can also take a logical value of "append", which at true indicates that the data written to the target file will be appended to the ones already available, and if false, the file will be completely overwritten; The old data will not be saved.

Let's look at an example using these classes:

The input stream FileInputStream reads the data from file1.txt by byte using the read () method. Each read byte is stored in a variable in integer form. 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. At the end, both threads are closed with the close method.

Classes FileReader and FileWriter

If you know that when dealing 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 occurs in much the same way as in the previous case. They are descendants of the Reader and Writer classes respectively, which define the basic methods for input and output of data.

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

Agree, 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 can not do. This is how Java works with files. An example of using these classes is shown below:

As you can see, there are no differences. It's 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 assumes that an int argument is received, only the lower 16 bits are written to the stream, since the characters in Java are represented by exactly that number of bits.

Conclusion

So, we disassembled 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 tool.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.