转载请注明出处:http://blog.youkuaiyun.com/github_39430101/article/details/77142235
File类
Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。
常用构造方法
通过将给定路径名字符串转换为抽象路径名来创建一个新的File实例
File(String pathname)
常用方法
| 返回类型 | 方法 | 用法 |
|---|---|---|
| boolean | canExecute() | 测试应用程序是否可以执行此抽象路径名表示的文件。 |
| boolean | createNewFile() | 当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。 |
| static File | createTempFile(String prefix,String suffix,File directory) | 在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。 |
| boolean | delete() | 删除此抽象路径名表示的文件或目录。 |
| boolean | deleteOnExit() | 在虚拟机终止时,请求删除此抽象路径名表示的文件或目录 |
| boolean | exists() | 测试此抽象路径名表示的文件或目录是否存在。 |
| File | getAbsoluteFile() | 返回此抽象路径名的绝对路径名形式。 |
| String | getAbsolutePath() | 返回此抽象路径名的绝对路径名字符串。 |
| String | getName() | 返回由此抽象路径名表示的文件或目录的名称。 |
| String | getParent() | 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null |
| String | getPath() | 将此抽象路径名转换为一个路径名字符串。 |
| boolean | isDirectory() | 测试此抽象路径名表示的文件是否是一个目录。 |
| boolean | isFile() | 测试此抽象路径名表示的文件是否是一个文件。 |
| long | length() | 返回此抽象路径名表示的文件的长度 |
| String[] | list() | 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。 |
| long | length() | 返回此抽象路径名表示的文件的长度 |
| File[] | listFiles(FilenameFilter filter) | 返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。 |
示例
创建文件
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
if(!file.exists()){
boolean flag = file.createNewFile(); //创建文件夹 file.mkdir()
if(flag == true){
System.out.println("文件创建成功");
} else
System.out.println("文件创建失败");
}
}
}
删除文件
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/a.txt");
if(file.exists()){
boolean flag = file.delete();
if(flag == true){
System.out.println("文件删除成功");
} else
System.out.println("文件删除失败");
}
}
}
输出一个目录中的内容
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/");
File[] subs = file.listFiles();
for(File a :subs){
System.out.println(a);
}
}
}
输出一个目录下所有扩展名为txt的文件
public class TestFile {
public static void main(String[] args) throws IOException{
File dir = new File("D:/io/");
File[] subs = dir.listFiles(new FileFilter(){
public boolean accept(File file){
return file.getName().endsWith(".txt");
}
});
for(File sub: subs){
System.out.println(sub);
}
}
}
如何遍历目录中所有的文件
public class TestFile {
public static void main(String[] args) throws IOException{
File file = new File("D:/io/");
fileList(file);
}
public static void fileList(File file){
if(file.isFile()){
System.out.println("文件名:"+file.getName());
} else if(file.isDirectory()){
System.out.println("文件夹目录是:"+file.getName());
File[] f = file.listFiles();
for(File s : f){
fileList(s);
}
}
}
}
RandomAccessFile
RandomAccessFile适用于由大小已知的记录组成的文件,所以我们可以使用seek()将记录从一处转移到另一处,然后读取或者修改记录。RandomAccessFile不是InputStream或者OutputStream继承层次结构中的一部分。从本质上来说,RandomAccessFile的工作方式类似于把DataInputStream和DataOutputStream组合起来使用,还添加了一些方法。其中方法getFilePointer()用于查找当前所处的文件位置。
使用RandomAccessFile类的write(byte[])方法向文件写数据
public class TestFile {
public static void main(String[] args) throws IOException{
RandomAccessFile raf = new RandomAccessFile("D:/io/raf.txt","rw");
byte[] buf = "HelloWorld".getBytes();
raf.write(buf);
raf.close();
}
}
使用RandomAccessFile类的read(byte[])方法从文件读取数据
public class TestFile {
public static void main(String[] args) throws IOException{
RandomAccessFile raf = new RandomAccessFile("D:/io/raf.txt","r");
//创建长度为10的字节数组
byte[] b = new byte[10];
int len = raf.read(b);
System.out.println("读取到了:"+len+"个字节");
System.out.println(new String(b));
raf.close();
}
}


被折叠的 条评论
为什么被折叠?



