1. 什么是流
Java中的流是对字节序列的抽象,咱们能够想象有一个水管,只不过如今流动在水管中的再也不是水,而是字节序列。和水流同样,Java中的流也具备一个“流动的方向”,一般能够从中读入一个字节序列的对象被称为输入流;可以向其写入一个字节序列的对象被称为输出流java
2. 字节流
Java中的字节流处理的最基本单位为单个字节,它一般用来处理二进制数据。Java中最基本的两个字节流类是InputStream和OutputStream,它们分别表明了一组基本的输入字节流和输出字节流。InputStream类与OutputStream类均为抽象类,咱们在实际使用中一般使用Java类库中提供的它们的一系列子类。app
3. 字符流
Java中的字符流处理的最基本的单元是Unicode码元(大小2字节),它一般用来处理文本数据。所谓Unicode码元,也就是一个Unicode代码单元,范围是0×0000~0xFFFF。在以上范围内的每一个数字都与一个字符相对应,Java中的String类型默认就把字符以Unicode规则编码然后存储在内存中。然而与存储在内存中不一样,存储在磁盘上的数据一般有着各类各样的编码方式。使用不一样的编码方式,相同的字符会有不一样的二进制表示。ui
4. 字符流与字节流的区别
通过以上的描述,咱们能够知道字节流与字符流之间主要的区别体如今如下几个方面:
字节流操做的基本单元为字节;字符流操做的基本单元为Unicode码元。
字节流默认不使用缓冲区;字符流使用缓冲区。
字节流一般用于处理二进制数据,实际上它能够处理任意类型的数据,但它不支持直接写入或读取Unicode码元;字符流一般处理文本数据,它支持写入及读取Unicode码元。
示例一:文件读取和写入(支持编码选择、文件内容追加)编码
/** * 读取文件文本内容。 * * @param filePath 文件路径 * @param charset 字符编码格式 * @throws Exception */ public static String readAllText(String filePath, String charset) throws Exception { StringBuilder sb = new StringBuilder(); try ( InputStreamReader reader = new InputStreamReader(new FileInputStream(filePath), charset); BufferedReader br = new BufferedReader(reader) ) { String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append(SystemCharUtils.getNewLine()); } } catch (Exception e) { throw e; } return sb.toString(); } /** * 将字符串追加到文件。 * * @param str 待写入字符串 * @param filePath 文件路径(文件不存在则自动建立) * @param charset 字符编码 * @throws Exception */ public static void appendFile(String str, String filePath, String charset) throws Exception { // 建立新文件对象。 File file = new File(filePath); //自动建立目录。 if (!file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { throw new RuntimeException("文件目录建立失败:" + file.getParentFile().getAbsolutePath()); } } //若是文件不存在则自动建立。 if (!file.exists()) { file.createNewFile(); } try ( OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, true), charset); BufferedWriter bw = new BufferedWriter(writer); ) { bw.write(str); } catch (Exception e) { throw e; } }
示例二:对象序列化到文件流 及 从文件流反序列化为对象
注意:实体对象要实现Serializable接口。spa
/** * 序列化文件路径。 */ String localFilePath = SystemCharUtils.getUserDir() + SystemCharUtils.getFileSeparator() + "data" + SystemCharUtils.getFileSeparator() + "test" +SystemCharUtils.getFileSeparator() + "testData.data"; /** * 从文件流反序列化为对象。 * * @param filePath 文件路径 * @throws Exception */ public static <T> T getObject(String filePath) throws Exception { try ( ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filePath)); ) { return (T) inputStream.readObject(); } catch (Exception e) { throw e; } } /** * 将对象序列化到文件流。 * * @param obj 须要序列化的对象 * @param filePath 文件路径 * @throws Exception */ public static void saveObject(Object obj, String filePath) throws Exception { // 建立新文件对象。 File file = new File(filePath); //自动建立目录。 if (!file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { throw new RuntimeException("文件目录建立失败:" + file.getParentFile().getAbsolutePath()); } } //若是文件不存在则自动建立。 if (!file.exists()) { file.createNewFile(); } try ( ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file)); ) { outputStream.writeObject(obj); } catch (Exception e) { throw e; } }