📜  Java I/O-File类

📅  最后修改于: 2020-09-27 07:38:17             🧑  作者: Mango

Java文件类

File类是文件和目录路径名的抽象表示。路径名可以是绝对路径,也可以是相对路径。

File类具有几种处理目录和文件的方法,例如创建新目录或文件,删除和重命名目录或文件,列出目录内容等。

领域

Modifier Type Field Description
static String pathSeparator It is system-dependent path-separator character, represented as a string for convenience.
static char pathSeparatorChar It is system-dependent path-separator character.
static String separator It is system-dependent default name-separator character, represented as a string for convenience.
static char separatorChar It is system-dependent default name-separator character.

建设者

Constructor Description
File(File parent, String child) It creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname) It creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child) It creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri) It creates a new File instance by converting the given file: URI into an abstract pathname.

有用的方法

Modifier and Type Method Description
static File createTempFile(String prefix, String suffix) It creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
boolean createNewFile() It atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
boolean canWrite() It tests whether the application can modify the file denoted by this abstract pathname.String[]
boolean canExecute() It tests whether the application can execute the file denoted by this abstract pathname.
boolean canRead() It tests whether the application can read the file denoted by this abstract pathname.
boolean isAbsolute() It tests whether this abstract pathname is absolute.
boolean isDirectory() It tests whether the file denoted by this abstract pathname is a directory.
boolean isFile() It tests whether the file denoted by this abstract pathname is a normal file.
String getName() It returns the name of the file or directory denoted by this abstract pathname.
String getParent() It returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
Path toPath() It returns a java.nio.file.Path object constructed from the this abstract path.
URI toURI() It constructs a file: URI that represents this abstract pathname.
File[] listFiles() It returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname
long getFreeSpace() It returns the number of unallocated bytes in the partition named by this abstract path name.
String[] list(FilenameFilter filter) It returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
boolean mkdir() It creates the directory named by this abstract pathname.

Java文件示例1

import java.io.*;
public class FileDemo {
public static void main(String[] args) {

try {
File file = new File("javaFile123.txt");
if (file.createNewFile()) {
System.out.println("New File is created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

输出:

New File is created!

Java文件示例2

import java.io.*;
public class FileDemo2 {
public static void main(String[] args) {

String path = "";
boolean bool = false;
try {
// createing  new files
File file  = new File("testFile1.txt");
file.createNewFile();
System.out.println(file);
// createing new canonical from file object
File file2 = file.getCanonicalFile();
// returns true if the file exists
System.out.println(file2);
bool = file2.exists();
// returns absolute pathname
path = file2.getAbsolutePath();
System.out.println(bool);
// if file exists
if (bool) {
// prints
System.out.print(path + " Exists? " + bool);
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}

输出:

testFile1.txt
/home/Work/Project/File/testFile1.txt
true
/home/Work/Project/File/testFile1.txt Exists? true

Java文件示例3

import java.io.*;
public class FileExample {
public static void main(String[] args) {
File f=new File("/Users/sonoojaiswal/Documents");
String filenames[]=f.list();
for(String filename:filenames){
System.out.println(filename);
}
}
}

输出:

"info.properties"
"info.properties".rtf
.DS_Store
.localized
Alok news
apache-tomcat-9.0.0.M19
apache-tomcat-9.0.0.M19.tar
bestreturn_org.rtf
BIODATA.pages
BIODATA.pdf
BIODATA.png
struts2jars.zip
workspace

Java文件示例4

import java.io.*;
public class FileExample {
public static void main(String[] args) {
File dir=new File("/Users/sonoojaiswal/Documents");
File files[]=dir.listFiles();
for(File file:files){
System.out.println(file.getName()+" Can Write: "+file.canWrite()+" 
Is Hidden: "+file.isHidden()+" Length: "+file.length()+" bytes");
}
}
}

输出:

"info.properties" Can Write: true Is Hidden: false Length: 15 bytes
"info.properties".rtf Can Write: true Is Hidden: false Length: 385 bytes
.DS_Store Can Write: true Is Hidden: true Length: 36868 bytes
.localized Can Write: true Is Hidden: true Length: 0 bytes
Alok news Can Write: true Is Hidden: false Length: 850 bytes
apache-tomcat-9.0.0.M19 Can Write: true Is Hidden: false Length: 476 bytes
apache-tomcat-9.0.0.M19.tar Can Write: true Is Hidden: false Length: 13711360 bytes
bestreturn_org.rtf Can Write: true Is Hidden: false Length: 389 bytes
BIODATA.pages Can Write: true Is Hidden: false Length: 707985 bytes
BIODATA.pdf Can Write: true Is Hidden: false Length: 69681 bytes
BIODATA.png Can Write: true Is Hidden: false Length: 282125 bytes
workspace Can Write: true Is Hidden: false Length: 1972 bytes