文件和文件夹管理
File类
只是创建一个对象,不会创建文件。
创建一个文件使用f.creatNewFile();
且只能创建文件,不能创建文件夹。
练习:输出指定文件下的所有Java文件(包含子目录),删除文件夹
import java.io.File;
public class Demo {
public static void main(String[] args) {
File f = new File("C:\\Users\\50339\\Documents\\TIME1");
deleteFile(f);
}
//输出文件下的所有文件(包含子文件)
public static void method(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
for(File f : files){
if(f.isFile()){
if(f.getName().endsWith(".java")){
System.out.println(f.getName());
}
}
//子文件夹下的文件
else{
method(f);
}
}
}
}
//删除指定目录下的所有文件和文件夹
public static void deleteFile(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
for(File f: files){
if(f.isFile()){
f.delete();
}
else if(f.isDirectory())
System.out.println(f.getName());
deleteFile(f);
}
System.out.println(file.getName());
file.delete();
}
}
}
练习:复制文件
没有文件可以创建文件,但是不会自动创建文件夹,如果没有就会报错。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\Test.java");
FileWriter fw = new FileWriter("C:\\Copy\\Test.java");
//mechod(fr, fw);
method2(fr, fw);
fw.close();
fr.close();
}
public static void method2(FileReader fr, FileWriter fw) throws IOException {
//一次读写一个字符数组
int len;
char[] chs = new char[1024];
while((len = fr.read(chs)) != -1){
fw.write(chs,0,len);
fw.flush();
}
}
public static void mechod(FileReader fr, FileWriter fw) throws IOException {
//一次读写一个字符
int ch;
while((ch = fr.read())!= -1){
fw.write(ch);
fw.flush();
}
}
}
练习:复制图片(只能使用字节流)
字节流不需要刷新,直接写入文件
import java.io.*;
public class CopyPicture {
public static void main(String[] args) throws IOException {
method();
method2();
return;
}
//字节流可以复制图片
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\test.png");
FileOutputStream fos = new FileOutputStream("C:\\CopyPicture\\zijie.png");
int len;
byte[] b = new byte[1024];
while((len = fis.read(b)) != -1)
{
fos.write(b,0,len);
}
fos.close();
fis.close();
}
//字节流不能复制图片
public static void method() throws IOException {
FileReader fr = new FileReader("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\test.png");
FileWriter fw = new FileWriter("C:\\CopyPicture\\jietu.png");
int len;
char[] chs = new char[1024];
while((len = fr.read(chs)) != -1){
fw.write(chs,0,len);
fw.flush();
}
fw.close();
fr.close();
}
}
String构造源码:没有文件,创建一个文件new File
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
File构造:没有文件抛出异常
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name);
FileCleanable.register(fd); // open set the fd, register the cleanup
}
字节流复制文本文件
字符串中的文件路径有两种方式:
“C:/a.txt"
"C:\\a.txt"
第二种创建文件夹比较安全。
剩下的自己看文档。