目录
File类
File的构造方法:
File的应用方法:
创建文件:
删除文件:
创建文件夹(目录):
创建一个路径下的某一个文件,且这个路径还不一定存在(封装工具类):
File类
在我的理解就是一个工具类,是一个操作文件的类
在学习IO流之前我们先介绍一下:
IO流就是对于文件的操作:
Input:把数据从物理内存加载到运行内存。(读文件)所谓的输入就是读文件
Output:把数据从运行内存写道物理内存。(写文件)
计算机的输入输出都是通过二进制完成的。
我们书写文件的路径:
正斜杠:左斜杠:撇:/
反斜杠:右斜杠:捺:
在Unix/Linux,路径的分隔采用正斜杠/,
在windows,中路径分隔采用的都是反斜杠。
在File类中,定义了路径分隔符的常量:
File.separator-----------反斜杠:
File.pathSeparator-----分号:;
在java里,代表了转义字符:
r | 回车 |
n | 换行 |
f | 走纸换页 |
t | 横向跳格 |
b | 退格 |
File的构造方法:
File file1 = new File(URI uri);URI转换为抽象路径名来创建新的File实例
File file2 = new FIle(String parent,String child);从父路径名字符串和子路径名字符串创建新的File实例
File file3 = new File(File parent,String child);从抽象路径名和子路径名字创建新的File实例。
实际操作:
File file1 =new File("C:\summerjava"); System.out.println("file1="+file1); File file2 = new File("C:\summerjava","homework0713"); System.out.println("file2="+file2); File file3 = new File(file1,"homework0713"); System.out.println("file3="+file3);
File的应用方法:
创建文件:
file.createNewFile();
file.createNewFile();
创建文件的时候会抛异常:
try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
为什么?答:因为怕你没有上一级菜单,所以会抛异常
删除文件:
file.delete()
file.delete()
删除有返回值,返回值为boolean
删除是没有异常的,如果存在就删除,不存在这个文件就拉到。
ps:file类删除时时不走回收站的
创建文件夹(目录):
file.mkdir(); //只能创建一级目录
file.mkdirs(); //可以创建多级目录
创建一个路径下的某一个文件,且这个路径还不一定存在(封装工具类):
import java.io.File;
import java.io.IOException;
public class FileUtil {
public static boolean createDirAndFile(String filepath) throws IOException {
File file = null;
if(filepath.contains("/")){
if(filepath.contains("\")){
throw new RuntimeException("路径不合法");
}else {
// e:\a\b\c\a.txt
int index = filepath.lastIndexOf("/");
String filename = filepath.substring(index,filepath.length());
filepath = filepath.substring(0, index+ 1);
file = new File(filepath);
file.mkdirs();
// 创建文件
File newFile = new File(filepath,filename);
newFile.createNewFile();
return true;
}
}
if(filepath.contains("\")){
if(filepath.contains("/")){
throw new RuntimeException("路径不合法");
}else {
// e:\a\b\c\a.txt
int index = filepath.lastIndexOf("\");
String filename = filepath.substring(index,filepath.length());
filepath = filepath.substring(0, index+ 1);
file = new File(filepath);
file.mkdirs();
// 创建文件
File newFile = new File(filepath,filename);
newFile.createNewFile();
return true;
}
}
throw new RuntimeException("路径不合法");
}
public static void main(String[] args) {
// String str = "e:\a\b\c\a.txt";
// System.out.println(str.contains("/"));
try {
createDirAndFile("d");
System.out.println("文件创建成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
}
file.mkdir(); //只能创建一级目录
file.mkdirs(); //可以创建多级目录
import java.io.File; import java.io.IOException; public class FileUtil { public static boolean createDirAndFile(String filepath) throws IOException { File file = null; if(filepath.contains("/")){ if(filepath.contains("\")){ throw new RuntimeException("路径不合法"); }else { // e:\a\b\c\a.txt int index = filepath.lastIndexOf("/"); String filename = filepath.substring(index,filepath.length()); filepath = filepath.substring(0, index+ 1); file = new File(filepath); file.mkdirs(); // 创建文件 File newFile = new File(filepath,filename); newFile.createNewFile(); return true; } } if(filepath.contains("\")){ if(filepath.contains("/")){ throw new RuntimeException("路径不合法"); }else { // e:\a\b\c\a.txt int index = filepath.lastIndexOf("\"); String filename = filepath.substring(index,filepath.length()); filepath = filepath.substring(0, index+ 1); file = new File(filepath); file.mkdirs(); // 创建文件 File newFile = new File(filepath,filename); newFile.createNewFile(); return true; } } throw new RuntimeException("路径不合法"); } public static void main(String[] args) { // String str = "e:\a\b\c\a.txt"; // System.out.println(str.contains("/")); try { createDirAndFile("d"); System.out.println("文件创建成功..."); } catch (IOException e) { e.printStackTrace(); } } }