[Java] How to use the File class

Programming study diary

November 1, 2020 Last time we dealt with the Math class, but this time we will briefly explain how to use the File class.

What is 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.

How to write

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.

Basic writing


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, \ like 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(); 

References

File manipulation! How to use File class in Java [For beginners] File class

Recommended Posts

[Java] How to use the File class
[Java] How to use the HashMap class
[Processing × Java] How to use the class
[Java] How to use the Calendar class
How to use java class
How to use the wrapper class
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
[Java] How to use the hasNext function
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use Map
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use the replace () method (Java Silver)
How to use Java variables
[Java] How to use Optional ①
[Java] How to use Calendar class and Date class
How to find out the Java version of a compiled class file
[Java] How to use compareTo method of Date class
[Java] How to extract the file name from the path
How to use Java HttpClient (Get)
How to use the form_with method
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
How to get the class name / method name running in Java
Multilingual Locale in Java How to use Locale
How to delete the wrong migration file
How to use submit method (Java Silver)
How to delete the migration file NO FILE
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload