
文件就是保存数据的地方 文件流
文件在程序中是以流的形式来 *** 作的
常用的文件 *** 作 创建文件相关的构造器和方法流:数据在数据源(文件)和程序(内存)之间经历的路径
- 方式1public File(String pathname)
public void create01(){
String filePath = "D:\Test\news1.txt";
File file = new File(filePath);//此时文件还在内存,不会真正产生
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件创建成功...");
}
- 方式2 public File(File parent, String child) //根据父目录文件+子路径构建
public void create02(){
File file = new File("D:\Test\");
String fileName = "news2.txt";
File file1 = new File(file, fileName);
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件创建成功...");
}
- 方式3 public File(String parent, String child) //根据父目录+子路径构建
public void create03(){
String file = "D:\Test\";
String fileName = "news3.txt";
File file1 = new File(file, fileName);
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件创建成功...");
}
获取文件相关的信息注意:路径写成"D:\Test\news1.txt"和"D:/Test/news1.txt"效果等同
以下为一些File基本方法的演示
public void info() {
//先创建文件
File file = new File("D:\Test\news0.txt");
//调用相应方法得到对应信息
//getName()
System.out.println("文件名字 = " + file.getName());
//getAbsolutePath()
System.out.println("绝对路径 = " + file.getAbsolutePath());
//getParent()
System.out.println("文件父目录 = " + file.getParent());
//length()
System.out.println("文件字节大小 = " + file.length());
//exists()
System.out.println("文件是否存在 = " + file.exists());//false
//isFile()
System.out.println("是否是个文件 = " + file.isFile());//false
//isDirectory()
System.out.println("是否是个目录= " + file.isDirectory());//false
}
目录 *** 作和文件删除
- 判断 “D:Testnews1.txt” 是否存在,如果存在就删除
public void m1(){
String filePath = "D:\Test\news1.txt";
File file = new File(filePath);
if (file.exists()){
if (file.delete()){
System.out.println(filePath+" was deleted...");
} else {
System.out.println(filePath + " was failed to deleted...");
}
}else {
System.out.println("The file does not exist...");
}
}
- 判断 “D:Test01” 是否存在,如果存在就删除
public void m2(){
String filePath = "D:\Test01";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()){
if (file.delete()){
System.out.println(filePath+" was deleted...");
} else {
System.out.println(filePath + " was failed to deleted...");
}
}else {
System.out.println("The directory does not exist...");
}
}
- 演示创建一级目录 mkdir()
public void m3(){
String dirPath = "D:\Test01\";
File file = new File(dirPath);
if (file.exists()){
System.out.println(dirPath+" already exists...");
}else {
if (file.mkdir()) {//创建一级目录,使用mkdir
System.out.println(dirPath+" was created successfully...");
} else {
System.out.println("Unable to make this directory...");
}
}
}
- 演示创建多级目录 mkdirs()
public void m4(){
String dirPath = "D:\Test01\a\b\c\";
File file = new File(dirPath);
if (file.exists()){
System.out.println(dirPath+" already exists...");
}else {
if (file.mkdirs()) {//创建多级目录,使用mkdir
System.out.println(dirPath+" was created successfully...");
} else {
System.out.println("Unable to make this directory...");
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)