File-操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//打印所以文件和目录
public static void printFile(File file) {
for (File f : file.listFiles()) {
if (f.isDirectory()) {
System.out.println(f.getPath());
printFile(f);
} else {
System.out.println(f.getPath());
}
}
}

//删除目录下全部文件
public static void delAllFile(File file) {
for (File f : file.listFiles()) {
if (f.isFile()) {
f.delete();
} else if (f.isDirectory()) {
delAllFile(f);
f.delete();
}
}
}

FileOutputStream

1
2
3
4
5
6
7
FileOutputStream fos=new FileOutputStream("src\\22.txt",true); //true追加写入

byte[] b="文本写入操作".getBytes();
fos.write(b);

fos.write("\n".getBytes());
fos.close();

FileInputStream

1
2
3
4
5
6
7
8
9
10
FileInputStream fis=new FileInputStream("src\\11.txt");

int b= fis.read();
System.out.println((char) b);

//读取
int by;
while ((by=fis.read())!=-1){
System.out.print((char) by);
}

复制

1
2
3
4
5
6
FileInputStream fis=new FileInputStream("src\\11.txt");
FileOutputStream fos=new FileOutputStream("src\\66.txt");
int by;
while ((by=fis.read())!=-1){
fos.write(by);
}