文件创建操作
获取文件相关信息
目录操作
复制操作
方法1:new file(String pathname)
@Test
public void create01(){
String filePath = "d:\news2.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
方法二:new File(File parent,String child),根据父目录文件+子路径构建
// d://news3.txt
@Test
public void create02(){
File parentfile = new File("d:\");
String fileName = "news3.txt";
//这里的file对象只是一个java对象
File file1 = new File(parentfile, fileName);
try {
//只有执行了createNewFile方法才会将该对象写入磁盘
file1.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
方法三:new file(String parent,String child)
@Test
public void create03(){
String parentpath = "d:\";
String fileName = "news4.txt";
File file = new File(parentpath, fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
获取文件相关信息
-
getName:获取文件名
-
package com.yuecheng.io.File; import org.junit.jupiter.api.Test; import java.io.File; public class Demo03 { public static void main(String[] args) { } @Test //获取文件相关信息 public void info(){ //先创建文件对象 File file = new File("d:\李玥骋.txt"); //调用相关方法获取对应信息 System.out.println("文件名字="+ file.getName()); System.out.println("绝对路径"+file.getAbsolutePath()); System.out.println("文件的父级目录"+ file.getParent()); System.out.println("文件的大小(字节)"+file.length()); } }
代码
package com.yuecheng.io.File;
import org.junit.jupiter.api.Test;
import java.io.File;
public class Demo04 {
public static void main(String[] args) {
}
public void m1() {
//判断d:\news3.txt是否存在,如果存在就删除
String filenPath = "D:\news3.txt";
File file = new File(filenPath);
if (file.exists()) {
if (file.delete()) {
System.out.println(filenPath + "文件删除成功");
} else {
System.out.println(filenPath + "文件删除失败");
}
} else {
System.out.println("文件不存在");
}
}
public void m2() {
//判断目录是否存在,如果存在就删除
String filenPath = "D:\图片";
File file = new File(filenPath);
if (file.exists()) {
if (file.delete()) {
System.out.println(filenPath + "文件删除成功");
} else {
System.out.println(filenPath + "文件删除失败");
}
} else {
System.out.println("目录不存在");
}
}
@Test
// 判断目录是否存在,存在则提示已存在,不存在则创建。
// D:\demo\a\b\c
public void m3() {
String directoryPath = "D:\demo\a\b\c";
File file = new File(directoryPath);
if (file.exists()) {
System.out.println(directoryPath+"该目录已存在");
} else {
if (file.mkdirs()){
//创建多级目录使用mkdirs方法,创建单目录使用makdir方法.
System.out.println(directoryPath+"文件创建成功");
}else{
System.out.println(directoryPath+"文件创建失败");
}
}
}
}
复制操作
package com.yuecheng.io.Filecopy;
import org.junit.jupiter.api.Test;
import java.io.*;
public class copy {
public static void main(String[] args) {
}
@Test
public void copy(){
//完成对文件的拷贝将 d:\李玥骋.txt,复制一遍(拷贝到d:\)
String srcFilePath = "d:\李玥骋.txt";
String destFilePath = "d:\李玥骋2.txt";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream= null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组来提高效率
byte [] buf = new byte[1024];
int readLen = 0;
while ((readLen=fileInputStream.read(buf))!=-1){
//读取到最后,就写入目标文件通过fileOutputStream
//即一边读一边写
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null){
fileInputStream.close();
}
if (fileOutputStream!=null){
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}



