IO字节流和缓冲流
IO字节流的读取和写入
读取
1 import java.io.FileInputStream;
2 import java.io.FileNotFoundException;
3 import java.io.IOException;
4
5 public class Test {
6 public static void main(String[] args) {
7 try (FileInputStream fis = new FileInputStream("java.txt")
8 ) {
9 byte[] bytes = new byte[40];
10 int temp;
11 while ((temp = fis.read(bytes)) != -1) {
12 System.out.println(new String(bytes, 0, temp));
13 }
14 } catch (FileNotFoundException e) {
15 e.printStackTrace();
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19 }
20 }
写入
1 import java.io.*;
2
3 public class Test {
4 public static void main(String[] args) {
5 try (FileOutputStream fos = new FileOutputStream("file" + File.separator + "1024.txt", true) //写入时不清除原有数据
6 ) {
7 fos.write("Hello".getBytes());
8 fos.write("
".getBytes()); //换行
9 fos.write("Wrold!".getBytes());
10 fos.flush(); //刷新
11 } catch (FileNotFoundException e) {
12 e.printStackTrace();
13 } catch (IOException e) {
14 e.printStackTrace();
15 }
16 }
17 }
IO字节流的文件拷贝
1 import java.io.*;
2
3 public class Test {
4 public static void main(String[] args) {
5 try (
6 FileOutputStream fos = new FileOutputStream("file" + File.separator + "new.txt");
7 FileInputStream fis = new FileInputStream("java.txt")
8 ) {
9 int temp;
10 byte[] bytes = new byte[50];
11 while ((temp = fis.read(bytes)) != -1) {
12 fos.write(bytes);
13 }
14 } catch (FileNotFoundException e) {
15 e.printStackTrace();
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19 }
20 }
IO缓冲流的读取和写入
1 import java.io.*;
2
3 /**
4 * IO缓冲字节流的读取
5 */
6 public class Test {
7 public static void main(String[] args) {
8 try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("java.txt"));
9 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new.txt"));
10 ) {
11 int temp;
12 while ((temp = bis.read()) != -1) {
13 bos.write(temp);
14 }
15 } catch (FileNotFoundException e) {
16 e.printStackTrace();
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20 }
21 }
IO字符流和缓冲流
IO字符流的文件拷贝
1 import java.io.*;
2
3 /**
4 * IO字符流的文件拷贝
5 */
6 public class Test {
7 public static void main(String[] args) {
8 try (FileReader fr = new FileReader("java.txt");
9 FileWriter fw = new FileWriter("file" + File.separator + "new.txt")
10 ) {
11 int temp;
12 while ((temp = fr.read()) != -1) {
13 fw.write(temp);
14 }
15 } catch (FileNotFoundException e) {
16 e.printStackTrace();
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20 }
21 }
IO缓冲字符流的文件拷贝
1 import java.io.*;
2
3 /**
4 * IO缓冲字符流的文件拷贝
5 */
6 public class Test {
7 public static void main(String[] args) {
8 try (
9 BufferedReader br = new BufferedReader(new FileReader("java.txt"));
10 BufferedWriter bw = new BufferedWriter(new FileWriter("file" + File.separator + "new.txt"))
11 ) {
12 String msg;
13 while ((msg = br.readLine()) != null) {
14 bw.write(msg);
15 bw.newLine(); //换行
16 bw.flush(); //刷新
17 }
18
19 } catch (FileNotFoundException e) {
20 e.printStackTrace();
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 }
25 }
练习:图片简单的加密和解密
加密
1 import java.io.*;
2
3 /**
4 * 图片的简单加密
5 * 通过异或的方式
6 */
7 public class Test {
8 public static void main(String[] args) {
9 try (
10 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "123.jpg"));
11 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new123.jpg"))
12 ) {
13 int temp;
14 while ((temp = bis.read()) != -1) {
15 bos.write(temp ^ 88);
16 }
17 } catch (FileNotFoundException e) {
18 e.printStackTrace();
19 } catch (IOException e) {
20 e.printStackTrace();
21 }
22 }
23 }
解密
1 import java.io.*;
2
3 /**
4 * 图片的简单解密
5 * 通过异或的方式
6 */
7 public class Test {
8 public static void main(String[] args) {
9 try (
10 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "code.jpg"));
11 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "decode.jpg"))
12 ) {
13 int temp;
14 while ((temp = bis.read()) != -1) {
15 bos.write(temp ^ 88);
16 }
17 } catch (FileNotFoundException e) {
18 e.printStackTrace();
19 } catch (IOException e) {
20 e.printStackTrace();
21 }
22 }
23 }