File!
It is just an abstract representation. It won't do anything to your computer. File class. pic10b/io/notes.html. C:\java\bin\ C:\java\bin\, C:/java/bin, C:\java/bin. File with file name "java\hw1\hw.java". The
you should use one of the follwoing code new File("java\\hw1\\hw.java"); new File("java/hw1/hw.java");static char pathSeparatorChar to get the path
seperator. C:\windows\Cookies
C:\Submit\,then a relative path name can be
hw1.java. | Constructors/Methods | Description |
|---|---|
|
File(String pathname), File(String parent, String filename), File(File directory, String filename) |
Creates a new File instance by converting the given pathname string into an abstract pathname. Or creates a new File instance from a parent pathname string (or directory) and a child pathname string. |
| Constructors/Methods | Description |
|---|---|
| exists() | Tests whether the file denoted by this abstract pathname exists. |
| isFile() |
Tests whether the file denoted by this abstract pathname is a normal file.
Returns true if and only if the file denoted by this abstract pathname exists and is a normal file;
false otherwise.
|
| isDirectory() |
Tests whether the file denoted by this abstract pathname is a directory.
Returns true if and only if the file denoted by this abstract pathname exists and is a directory;
false otherwise.
|
| Constructors/Methods | Description |
|---|---|
| boolean mkdir() | Creates the directory named by this abstract pathname. Returns true if and only if the directory was created; false otherwise. |
| boolean mkdirs() | Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Returns true if and only if the directory was created, along with all necessary parent directories; false otherwise |
| boolean delete() | Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Returns true if and only if the file or directory is successfully deleted; false otherwise |
| boolean renameTo(File dest) | Renames the file denoted by this abstract pathname. Returns true if and only if the renaming succeeded; false otherwise |
| Constructors/Methods | Description |
|---|---|
| String[] list() | Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. |
| File[] listFiles() | Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. |
| Constructors/Methods | Description |
|---|---|
| long lastModified() | Returns the time that the file denoted by this abstract pathname was last modified. |
| long length() | Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist |
TestExistence.java, such that java TestExistance file tells you if the file exists or not. C:\myjavaprograms> java TestExistance C:\windows\cookies C:\windows\cookies exists. C:\myjavaprograms> java TestExistance abcd\fgh\ijk abcd\fgh\ijk doesn't exist.
import java.io.*; public class TestExistence { public static void main(String[] args) { File file = new File(args[0]); if(file.exists()) { System.out.println(file + " exists."); } else { System.out.println(file + " does not exist."); } } }Remark
java TestExistance C:/windows/cookies java TestExistance C:/windows\cookiesQuestion 2
Write a method list all the files (and files in the subdirectories) in a given directory.Answer
public static void listAllFiles(File file) { if(file.isFile()) { System.out.println(file); } else if(file.isDirectory()) { File[] fileList = file.listFiles(); for(int i = 0; i < fileList.length; i++) { listAllFiles(fileList[i]); } } }Question 3
Write a program to create 3 directoriespic10a, pic10b, pic20aunder the directoryclasses.Answer : Question3.java
import java.io.File; public class Question3 { public static void main(String[] args) { String[] dirNames = {"pic10a", "pic10b", "pic20a"}; for(int i = 0; i < dirNames.length; i++) { new File("classes/" + dirNames[i]).mkdirs(); } } }Question : do you know the difference betweenmkdirandmkdirs?
Question 4
Write a programQuestion4.javasuch thatjava Question4 fileshows the size(number of bytes) of the file.Answer: Question4.java
import java.io.File; public class Question4 { public static void main(String[] args) { File file = new File(args[0]); if(file.isFile()) { System.out.println("The size of the file is " + file.length() + " bytes."); } else { System.out.println(file + " is not a file."); } } }