November 1, 2020 Last time we dealt with the Math class, but this time we will briefly explain how to use the File class.
A class used to handle files in Java. File is a class that represents a file or directory, and is used in the entire file system such as reading and writing files and getting a list of files in a folder.
Associate one File class object with the file you want to handle. You can read and write the contents of a file using the file object of the created File class. Therefore, you can handle local files in Java by using the File class.
File object name= new File(file name); //Concrete example File file = new File("c:\\test\\test.txt");
As shown in the concrete example, when specifying the file name,
c: \\ test \\ test.txt instead of
c: \ test \ test.txt </ B>.
In the above example, the file location is specified with an absolute path. Absolute specification and relative specification are possible for file specification, and in the case of relative specification, the position where the program file is placed is used as a reference.
Specify by relative path
//Text in the same directory as the program files.txt File file = new File("test.txt"); //Text in the txt directory in the program files directory.txt File file = new File(".\\txt\\test.txt");
Operation using File instance
//Get the name String name = f.getName(); //True if the target exists boolean b = f.exists(); //Delete the target f.delete(); //True if file boolean b = f.isFile(); //True if it is a folder boolean b = f.isDirectory(); //Get size int length = f.length();