字符流与字节流
字节流和字符流的概念
Java对于文件处理是按照流的方式进行操作的(和C++ 类似 都会用到一个缓冲区),按照处理数据的单位可以分为字节流和字符流。
按照输入输出的方向可以分为输入流和输出流。
字节流:每次读入或输出的是8位二进制。
字符流:每次读入或输出的是16位二进制,即两个字节。
根据Java API规范:
FileOutputStream用于写入原始字节流,例如图像数据。
FileWriter,用于编写字符流,例如写文本。

节点流与处理流



File对象
创建文件对象相关构造器和方法
1 2 3
| new File(String pathname) new File(File paraent, String child) new File(String parent, String child)
|
在E盘下三种创建文件的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 第一种: String filePath = "e:\\news1.txt"; File file = new File(filePath); file.createNewFile();
第二种: File parentFile = new File("e:\\"); String fileName = "news2.txt";
File file = new File(parentFile, fileNa file.createNewFile();me);
第三种: String parentPath = "e:\\"; String fileName = "news4.txt"; File file = new File(parentPath, fileName); file.createNewFile()
|
获取文件的相关信息的API
1 2 3 4 5 6 7 8 9 10 11
| File file = new File("e:\\news1.txt");
System.out.println("文件名字=" + file.getName());
System.out.println("文件绝对路径=" + file.getAbsolutePath()); System.out.println("文件父级目录=" + file.getParent()); System.out.println("文件大小(字节)=" + file.length()); System.out.println("文件是否存在=" + file.exists()); System.out.println("是不是一个文件=" + file.isFile()); System.out.println("是不是一个目录=" + file.isDirectory());
|
使用FileInputStream读取hello.text文件
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
| 第一种:
String filePath = "e:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1) { System.out.print((char)readData); }
fileInputStream.close();
第二种:
String filePath = "e:\\hello.txt";
byte[] buf = new byte[8]; FileInputStream fileInputStream = null; fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen)); } fileInputStream.close();
|
FileOutputStream(字节流,文件专属)
使用 FileOutputStream 在 a.txt 文件,中写入 “hello,world”.
如果文件不存在,会创建文件(注意:前提是目录已经存在.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| String filePath = "e:\\a.txt"; FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(filePath, true);
fileOutputStream.write('H');
String str = "hsp,world!";
fileOutputStream.write(str.getBytes(), 0, 3); fileOutputStream.close();
|
完成图片/音乐的拷贝.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
String srcFilePath = "e:\\Koala.jpg"; String destFilePath = "e:\\Koala3.jpg"; FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath);
byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, readLen); } fileInputStream.close(); fileOutputStream.close();
|
网页图片的上传与下载
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
|
public R<String> upload(MultipartFile file) { String originalFilename = file.getOriginalFilename(); String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); String filePath = UUID.randomUUID().toString() + substring; File dir = new File(basePath); if(!dir.exists()) { dir.mkdirs(); } try { file.transferTo(new File(basePath + filePath)); } catch (IOException e) { e.printStackTrace(); } return R.success(filePath); }
public void download(String name, HttpServletResponse response) { try { FileInputStream fileInputStream = new FileInputStream(new File(basePath + name)); ServletOutputStream outputStream = response.getOutputStream(); response.setContentType("image/jpeg"); int len = 0; byte[] bytes = new byte[1024]; while((len = fileInputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); outputStream.flush(); } outputStream.close(); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } }
|
FileReader(字符流,文件专属)
使用 FileReader 从 story.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
| 第一种:
String filePath = "e:\\story.txt"; FileReader fileReader = null; int data = 0;
fileReader = new FileReader(filePath);
while ((data = fileReader.read()) != -1) { System.out.print((char) data); } fileReader.close();
第二种:
String filePath = "e:\\story.txt"; FileReader fileReader = null; int readLen = 0; char[] buf = new char[8];
fileReader = new FileReader(filePath);
while ((readLen = fileReader.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen)); } fileReader.close();
|
FileWriter(字符流,文件专属)
使用 FileWriter 将 “风雨之后,定见彩虹” 写入到 note.txt 文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| String filePath = "e:\\note.txt";
FileWriter fileWriter = null; char[] chars = {'a', 'b', 'c'}; fileWriter = new FileWriter(filePath);
fileWriter.write('H');
fileWriter.write(chars);
fileWriter.write("韩顺平教育".toCharArray(), 0, 3);
fileWriter.write("风雨之后,定见彩虹");
fileWriter.write("上海天津", 0, 2);
fileWriter.close();
|
BufferedReader和BufferedWriter(字符流)
综合使用BufferedReader和BufferedWriter完成文本文件拷贝,注意文件编码
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
|
String srcFilePath = "e:\\a.java"; String destFilePath = "e:\\a2.java";
BufferedReader br = null; BufferedWriter bw = null; String line;
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } System.out.println("拷贝完毕...");
br.close(); bw.close();
|
在创建流对象时会创建一个内部缓冲区数组.
使用BufferedOutputStream和BufferedInputStream,完成二进制文件拷贝.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| String srcFilePath = "e:\\a.jpg"; String destFilePath = "e:\\a3.jpg";
BufferedInputStream bis = null; BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileInputStream(srcFilePath)); bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
byte[] buff = new byte[1024]; int readLen = 0;
while ((readLen = bis.read(buff)) != -1) { bos.write(buff, 0, readLen); } System.out.println("文件拷贝完毕~~~");
bis.close(); bos.close();
|
BufferedStream与FileStream
谁快谁慢是根据实际情况来决定的,而不是说带了缓冲区就一定快;
- 每次写入的数据量小的情况下,带缓冲区的
BufferedOutputStream效率更快;
- 每次写入的数据量比较大时,不带缓冲区的
FileOutputStream 效率更快;
所以,大家在选择的时候就需要根据实际情况来决定使用哪种IO流了,而大部分情况下,FileOutputStream 就已经足够了,只需要将写入的数据量大一点即可;
功能:提供了对基本类型或对象类型的序列化和反序列化的方法
ObjectOutputStream 提供 序列化功能
ObjectInputStream 提供 反序列化功能


演示 ObjectOutputStream 的使用, 完成数据的序列化
1 2 3 4 5 6 7 8 9 10 11 12 13
| String filePath = "e:\\data.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
oos.writeInt(100); oos.writeBoolean(true); oos.writeChar('a'); oos.writeDouble(9.5); oos.writeUTF("韩顺平教育");
oos.writeObject(new Dog("旺财", 10, "日本", "白色")); oos.close(); System.out.println("数据保存完毕(序列化形式)");
|
使用ObjectInputStream读取data.dat并反序列化恢复数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\data.dat"));
System.out.println(ois.readInt()); System.out.println(ois.readBoolean()); System.out.println(ois.readChar()); System.out.println(ois.readDouble()); System.out.println(ois.readUTF()); System.out.println(ois.readObject()); System.out.println(ois.readObject()); System.out.println(ois.readObject());
ois.close(); System.out.println("以反序列化的方式读取(恢复)ok~");
|


将字节流FileInputStream包装成(转换成)字符流InputStreamReader,对文件进行读取(按照utf-8/gdk格式),进而再包装成BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| String filePath = "e:\\a.txt";
BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(filePath), "gbk"));
String s = br.readLine(); System.out.println("读取内容=" + s);
br.close();
|
将字节流FileOutputStream包装成(转换成)字符流OutputStreamWriter,对文件进行读取(按照utf-8/gdk格式),进而再包装成BufferedWriter
1 2 3 4 5 6 7
| OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\a.txt"), "gbk");
osw.write("hello,world");
osw.close();
|
Properti类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
P
properties.load(new FileReader("src\\mysql.properties"));
properties.list(System.out);
String user = properties.getProperty("user"); String pwd = properties.getProperty("pwd");
properties.setProperty("charset", "utf8"); properties.setProperty("user", "汤姆"); properties.setProperty("pwd", "888888");
properties.store(new FileOutputStream("src\\mysql2.properties"), null);
|