File Object Methods
The `File` methods that create, delete and check files and directories, and list what a directory holds.
Apprentice · Java · Updated
The File object has several methods that allow us to work with the file system.
createNewFile
This creates a new, empty text file at the path provided. Note: a relative path can be used to work with files relative to the directory where the application is running.
This returns a boolean indicating whether the file creation was successful. It can throw an IOException, so it goes in a try...catch block (see Working with Exceptions).
File file = new File("orders.txt");
try
{
boolean created = file.createNewFile();
}
catch(IOException ex)
{
System.out.println(ex.getMessage());
}
delete
Deletes a file or directory at the specified path.
File file = new File("orders.txt");
boolean deleted = file.delete();
exists
Checks to see if the file or directory exists.
File file = new File("orders.txt");
boolean fileExists = file.exists();
File directory = new File("data");
boolean directoryExists = directory.exists();
isFile
Checks if the path is a file
File file = new File("orders.txt");
// returns true if the path exists and if it is a file
boolean isFile = file.isFile();
isDirectory
Checks if the path is a directory
File file = new File("data");
// returns true if the path exists and if it is a directory
boolean isDirectory = file.isDirectory();
getName
Returns the name of the file or directory
String name = file.getName();
getAbsolutePath
Returns the fully qualified absolute path of the file in the file system
String path = file.getAbsolutePath();
getPath
Returns the relative or absolute path as it was defined when the File was created.
File file = new File("data.txt");
// returns the relative path of the file
String path = file.getPath();
file = new File("/Users/john/documents/data.txt");
// returns the absolute path of the file
path = file.getPath();
list
If the path is a directory, this returns a String array of file/directory names in the directory.
File file = new File("data");
// returns an array of Strings - the file names in the directory
String[] fileNames = file.list();
listFiles
If the path is a directory, this returns an array of File objects that represent the files and directories in that directory.
File file = new File("data");
// returns an array of Files - the files/directories at that path
File[] files = file.listFiles();
Do it: find the CSV files in a folder
The grade book keeps its files in a data folder, and it should load every CSV file in it, and nothing else. new File("data").list() gives the names in the folder as a String[]: files and folders, in no special order. The box has no folders, so the names come to your method as that array.
A name is a CSV file when it ends with .csv. A String's endsWith(".csv") returns true when the text ends that way.
The method gets one value:
names: the names in the folder, aString[], aslist()gives them.
For example:
String[] names = { "orders.csv", "notes.txt", "archive", "customers.csv" };
| Name | Ends with .csv? |
|---|---|
orders.csv |
Yes |
notes.txt |
No |
archive |
No: it's a folder |
customers.csv |
Yes |
So csvFiles(names) returns a list of orders.csv, then customers.csv.
Your job: write the method csvFiles(String[] names) so it returns a List<String>: the names that end with .csv.
- Keep them in the order they're in the array.
- A name that only contains
csv, such ascsv-guide.txt, isn't a CSV file. - An empty folder (an empty array) gives an empty list.
Practiced live on the lesson page: write the Java right there and check it.