知识回顾
- 字节流
-
- 拷贝任意类型的文件
- 字符流
-
- 读取纯文本文件中的数据
-
- 往纯文本文件中写出数据
练习一:拷贝
需求:拷贝一个文件夹,考虑子文件夹
public class Test1 {
public static void main(String[] args) throws IOException {
File src = new File("/Users/jessy/Desktop/offer50/aaa");
File dest = new File("/Users/jessy/Desktop/offer50/aaacopy");
copydir(src, dest);
}
private static void copydir(File src, File dest) throws IOException {
dest.mkdirs();
//递归
//1。进入数据源
File[] files = src.listFiles();
//2。遍历数组
for (File file : files) {
if (file.isFile()) {
//3。判断文件,拷贝, 文件开始,文件结束
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(dest, file.getName()));
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
} else {
//4。判断文件夹,递归
copydir(file, new File(dest, file.getName()));
}
}
}
}
练习二:文件加密
问了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
- 加密原理:
-
- 对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新的文件中。
- 解密原理
-
- 读取加密之后的文件,按照加密的规则反向操作,变成原始文件。
public class Test2 {
public static void main(String[] args) throws IOException {
// ^ 异或
// 两边相同:false
// 两边不同:true
// 如果两边是数字,二进制后异或运算
// 100 ^ 10 = 110
// 100 1100100
// 10 ^ 1010
// -----------
// 1101110 = 110
// 110 ^ 10 = 100
//1。创建对象关联原始文件
FileInputStream fis = new FileInputStream("/Users/jessy/Desktop/offer50/src/com/itheima/mycharstream1/kit.jpg");
//2。创建对象关联加密文件
FileOutputStream fos = new FileOutputStream("/Users/jessy/Desktop/offer50/src/com/itheima/mycharstream1/kitcopy.jpg");
//3。加密处理
int b;
while ((b = fis.read()) != -1) {
fos.write(b ^ 2);
}
fos.close();
fis.close();
}
}
public class Test3 {
public static void main(String[] args) throws IOException {
//解密回来
FileInputStream fis = new FileInputStream("/Users/jessy/Desktop/offer50/src/com/itheima/mycharstream1/kitcopy.jpg");
FileOutputStream fos = new FileOutputStream("/Users/jessy/Desktop/offer50/src/com/itheima/mycharstream1/kit1.jpg");
int b;
while ((b = fis.read()) != -1) {
fos.write(b ^ 2);
}
fos.close();
fis.close();
}
}
练习三:修改文件中的数据
文本文件中有以下的数据:
2-1-9-4-7-8
将文件中的数据进行排序,变成以下的数据
1-2-4-7-8-9
public class Test4 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/jessy/Desktop/offer50/aaa/aaa.txt");
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = fr.read()) != -1) {
sb.append((char) ch);
}
fr.close();
//2-1-9-4-7-8
System.out.println(sb);
String str = sb.toString();
String[] arrstr = str.split("-");
ArrayList<Integer> list = new ArrayList<>();
for (String s : arrstr) {
int i = Integer.parseInt(s);
list.add(i);
}
//[2, 1, 9, 4, 7, 8]
System.out.println(list);
Collections.sort(list);
//[1, 2, 4, 7, 8, 9]
System.out.println(list);
FileWriter fw = new FileWriter("/Users/jessy/Desktop/offer50/aaa/aaa.txt");
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
//a
//fw.write(97);
//97
//fw.write(97 + "");
fw.write(list.get(i) + "");
} else {
fw.write(list.get(i) + "-");
}
}
fw.close();
//1-2-4-7-8-9
}
}
- 改进方法
public class Test5 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/jessy/Desktop/offer50/aaa/aaa.txt");
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = fr.read()) != -1) {
sb.append((char) ch);
}
fr.close();
//2-1-9-4-7-8
System.out.println(sb);
Integer[] arr = Arrays.stream(sb.toString()
.split("-"))
/*.map(new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return Integer.parseInt(s);
}
})*/
.map(Integer::parseInt)
.sorted()
.toArray(Integer[]::new);
//[1, 2, 4, 7, 8, 9]
System.out.println(Arrays.toString(arr));
FileWriter fw = new FileWriter("/Users/jessy/Desktop/offer50/aaa/aaa.txt");
/*String s = Arrays.toString(arr);
//[1, 2, 4, 7, 8, 9]
System.out.println(s);*/
String s = Arrays.toString(arr).replace(", ", "-");
//[1-2-4-7-8-9]
System.out.println(s);
String result = s.substring(1, s.length() - 1);
//1-2-4-7-8-9
System.out.println(result);
fw.write(result);
fw.close();
}
}
细节:
- 文件中的数据不要换行
-
- 会读取到
\r
换行符,影响排序
- 会读取到
- bom头(文件开头隐含的bom头)
-
- 本地文件,重新另存为UTF-8格式
-
- idea里默认是没有bom头的
Settings -> Editor -> File Encodings -> Create UTF-8 files: with NO BOM
- idea里默认是没有bom头的