Fork me on GitHub

IO流

复制文件

需要留意的是,read会返回实际的读取数量,有可能实际的读取数量小于缓冲的大小,那么把缓冲中的数据写出到目标文件的时候,就只应该写出部分数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @param srcPath 源文件
* @param destPath 目标文件
*/
public static void copyFile(String srcPath, String destPath){
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//缓存区,一次性读取1024字节
byte[] buffer = new byte[1024];
try (
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
){
while(true){
//实际读取的长度是 actuallyReaded,有可能小于1024
int actuallyReaded = fis.read(buffer);
//-1表示没有可读的内容了
if(-1==actuallyReaded)
break;
fos.write(buffer, 0, actuallyReaded);
fos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param srcPath 源文件夹
* @param destPath 目标文件夹
*/
public static void copyFolder(String srcPath, String destPath){
}
public static void main(String[] args) {
copyFile("C:\\Users\\12905\\Desktop\\xxx\\2.txt", "C:\\Users\\12905\\Desktop\\xxx\\3.txt");
}

复制文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @param srcPath 源文件
* @param destPath 目标文件
*/
public static void copyFile(String srcPath, String destPath){
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//缓存区,一次性读取1024字节
byte[] buffer = new byte[1024];
try (
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
){
while(true){
//实际读取的长度是 actuallyReaded,有可能小于1024
int actuallyReaded = fis.read(buffer);
//-1表示没有可读的内容了
if(-1==actuallyReaded)
break;
fos.write(buffer, 0, actuallyReaded);
fos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param srcPath 源文件夹
* @param destPath 目标文件夹
*/
public static void copyFolder(String srcPath, String destPath){
File srcFolder = new File(srcPath);
File destFolder = new File(destPath);
//源文件夹不存在
if(!srcFolder.exists())
return;
//源文件夹不是一个文件夹
if(!srcFolder.isDirectory())
return;
//目标文件夹是一个文件
if(destFolder.isFile())
return;
//目标文件夹不存在
if(!destFolder.exists())
destFolder.mkdirs();
//遍历源文件夹
File[] files= srcFolder.listFiles();
for (File srcFile : files) {
//如果是文件,就复制
if(srcFile.isFile()){
File newDestFile = new File(destFolder,srcFile.getName());
copyFile(srcFile.getAbsolutePath(), newDestFile.getAbsolutePath());
}
//如果是文件夹,就递归
if(srcFile.isDirectory()){
File newDestFolder = new File(destFolder,srcFile.getName());
copyFolder(srcFile.getAbsolutePath(),newDestFolder.getAbsolutePath());
}
}
}
public static void main(String[] args) {
copyFolder("C:\\Users\\12905\\Desktop\\xxx", "C:\\Users\\12905\\Desktop\\yyy");
}

查找文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @param file 查找的目录
* @param search 查找的字符串
*/
public static void search(File file, String search) {
if (file.isFile()) {
if(file.getName().toLowerCase().endsWith(".java")){
String fileContent = readFileConent(file);
if(fileContent.contains(search)){
System.out.printf("找到子目标字符串%s,在文件:%s%n",search,file);
}
}
}
if (file.isDirectory()) {
File[] fs = file.listFiles();
for (File f : fs) {
search(f, search);
}
}
}
public static String readFileConent(File file){
try (FileReader fr = new FileReader(file)) {
char[] all = new char[(int) file.length()];
fr.read(all);
return new String(all);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
File folder =new File("e:\\project");
search(folder,"Magic");
}

流关系图

1

Your support will encourage me to continue to create!