IO输入输出流练习

  1. 1. 1.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 处理流 实现 )
  2. 2. 2.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 节点流 实现 )
  3. 3. 3.创建文件 Demo1.txt 写入文本 hello 创建文件 Demo2.txt 写入文本 Neuedu,将两个文件内容 提取出来输出到 第三个文件 Test.txt 通过 文件与流方式实现
  4. 4. 4.在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:[必做题]

1.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 处理流 实现 )
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
public class WorkDemo1 {

/**
* 处理流
* @param args
*/
public static void main(String[] args) {
boolean result = copyFile("D:\\5.png","E:\\5.png");
System.out.println(result);
}

/**
* 文件的复制
* @param source 源文件
* @param target 目标文件
* @return false:复制失败 true:复制成功
* @throws IOException
*/
public static boolean copyFile(String source,String target){

//参数的判断
if(StringUtil.isEmpty(source) || StringUtil.isEmpty(target)){
throw new RuntimeException("参数都不能为空");
}

if(!new File(source).exists()){
throw new RuntimeException("参数错误");
}

File targetFile = new File(target);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}

BufferedInputStream bufferedInputStream=null;
BufferedOutputStream bufferedOutputStream=null;

try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(source));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(target));

byte [] cache = new byte[1024];

int len = -1;
while((len=bufferedInputStream.read(cache))!=-1){
bufferedOutputStream.write(cache, 0, len);
bufferedOutputStream.flush();
}
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedInputStream!=null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}
2.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 节点流 实现 )
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
public class WorkDemo1 {

public static void main(String[] args) {
boolean result = copyFile("D:\\4.png","E:\\4.png");
System.out.println(result);
}


public static boolean copyFile(String source,String target){

//参数的判断
if(StringUtil.isEmpty(source) || StringUtil.isEmpty(target)){
throw new RuntimeException("参数都不能为空");
}

if(!new File(source).exists()){
throw new RuntimeException("参数错误");
}

File targetFile = new File(target);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}

InputStream inputStream=null;
OutputStream outputStream=null;

try {
inputStream = new FileInputStream(source);
outputStream = new FileOutputStream(target);

byte [] cache = new byte[1024];

int len = -1;
while((len=inputStream.read(cache))!=-1){
outputStream.write(cache, 0, len);
outputStream.flush();
}
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}
3.创建文件 Demo1.txt 写入文本 hello 创建文件 Demo2.txt 写入文本 Neuedu,将两个文件内容 提取出来输出到 第三个文件 Test.txt 通过 文件与流方式实现

FileUtil.java 工具类

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class FileUtil {

/**
* 创建文件
* @param fileName 文件名
*/
public static boolean createFile(File fileName) {
boolean flag = false;
if (!fileName.exists()) {
try {
flag = fileName.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}

/**
* 读取文件
*
* @param fileName
* @return
*/
public static String readTxtFile(File fileName) {
Reader reader = null;
try {
reader = new FileReader(fileName);
StringBuilder sb = new StringBuilder();
int len = -1;
char[] cache = new char[1024];

while ((len = reader.read(cache)) != -1) {
sb.append(new String(cache, 0, len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

public static boolean copyFile(String source, String target) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(target));
bufferedWriter.write(source);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

public static boolean writeFile(String content,String target) {
File file = new File(target);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

Writer writer=null;
try {
writer = new FileWriter(target);
writer.write(content);
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

}

WorkDemo1.java 主入口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class WorkDemo1 {

public static void main(String[] args) {
File file1 = new File("D:\\demo1.txt");
File file2 = new File("D:\\demo2.txt");
FileUtil.createFile(file1);
FileUtil.createFile(file2);
StringBuilder sb = new StringBuilder();
sb.append(FileUtil.readTxtFile(file1));
sb.append(FileUtil.readTxtFile(file2));

boolean flag = FileUtil.writeFile(sb.toString(), "D:\\test.txt");

System.out.println(flag);
}

}
4.在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:[必做题]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class WorkDemo1 {

public static void main(String[] args) throws IOException {
listFileName("D:\\my_code\\Java_workplace\\xxxxx\\src\\_05Io", "文件名:");
}
public static void listFileName(String path, String prefix) {

File file = new File(path);
File childrent[] = file.listFiles();
for (File child : childrent) {

System.out.println(prefix + child.getName());
System.out.println("路径名:" + child.getAbsolutePath());
System.out.println("----------------------------------------");
}

}

}