javase-io-210509-02
FIle
Fiel
1、 File类和四大家族没有关系,所以FIle类不能完成对文件的读和写
2、 FIle对象代表 文件和目录路径名的抽象表示形式
C:\bgy 这是一个文件
C:\bgy\bgy.txt 这也是一个文件
Demo01.java
import java.io.File;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) {
File file01 = new File("log.txt");
File file02 = new File("testFile02");
System.out.println(file01.exists());
if (!file01.exists()){
try {
file01.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (!file02.exists()){
file02.mkdir();
}
File file03 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\a\\b\\c\\d");
if (!file03.exists()) {
file03.mkdirs();
}
File file04 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\log.txt");
String parentPath = file04.getParent();
System.out.println(parentPath);
File parentFile = file04.getParentFile();
System.out.println("绝对路径:"+parentFile.getAbsolutePath());
File file05 = new File("myfile");
System.out.println("myfile绝对路径:"+file05.getAbsolutePath());
}
}
Demo02.java
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo02 {
public static void main(String[] args) {
File file01 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\log.txt");
System.out.println("文件名:"+file01.getName());
System.out.println(file01.isDirectory());
System.out.println(file01.isFile());
long haomiao = file01.lastModified();
System.out.println(haomiao);
Date time = new Date(haomiao);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime = sdf.format(time);
System.out.println(strTime);
System.out.println(file01.length());
}
}
Demo03.java
package com.bgy07;
import java.io.File;
public class Demo03 {
public static void main(String[] args) {
File f = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509");
File[] files = f.listFiles();
for (File file : files){
System.out.println(file.getName());
}
}
}
拷贝目录,文件
package com.bgy07;
import java.io.*;
public class Copy {
public static void main(String[] args) {
File srcFile = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509");
File destFile = new File("C:\\");
copyDir(srcFile,destFile);
}
private static void copyDir(File srcFile, File destFile) {
if (srcFile.isFile()){
FileInputStream in = null;
FileOutputStream out = null;
try {
String srcPath = srcFile.getAbsolutePath();
String destPath = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : ( destFile.getAbsolutePath() + "\\" ))+ srcPath.substring(3);
in = new FileInputStream(srcPath);
out = new FileOutputStream(destPath);
byte[] bytes = new byte[1024*1024];
int readCount = 0;
while ((readCount = in.read(bytes)) != -1) {
out.write(bytes,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
File[] files = srcFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
String srcDir = file.getAbsolutePath();
String destDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : ( destFile.getAbsolutePath() + "\\" ))+ srcDir.substring(3);
File newFile = new File(destDir);
if (!newFile.exists()) {
newFile.mkdirs();
}
}
copyDir(file,destFile);
}
}
}