白城网站建设公司网站做seo第一步
news/
2025/10/8 16:16:40/
文章来源:
白城网站建设公司,网站做seo第一步,做网站ddos攻击,应用公园app在线制作文章目录 缓冲流字节缓冲流字符缓冲流 转换流InputStreamReader类OutputStreamWriter类 序列化ObjectOutputStream类ObjectInputStream类 打印流 缓冲流
缓冲流,也叫高效流#xff0c;是对4个基本的 FileXxx 流的增强#xff0c;所以也是4个流 基本原理#xff1a; 缓冲流的… 文章目录 缓冲流字节缓冲流字符缓冲流 转换流InputStreamReader类OutputStreamWriter类 序列化ObjectOutputStream类ObjectInputStream类 打印流 缓冲流
缓冲流,也叫高效流是对4个基本的 FileXxx 流的增强所以也是4个流 基本原理 缓冲流的基本原理是在创建流对象时会创建一个内置的默认大小的缓冲区数组通过缓冲区读写减少系统IO次数从而提高读写的效率。
字节缓冲流
构造方法
public BufferedInputStream(InputStream in) 创建一个 新的缓冲输入流public BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流。
通过读入一个程序与写出一个程序来举例缓冲流的使用方法。 首先准备一个较大的压缩包文件放在了D:\file1\java.zip这个路径下面。 使用缓冲流读写数据
public class BufferedDemo {public static void main(String[] args) throws FileNotFoundException {// 记录开始时间long start System.currentTimeMillis();// 创建流对象try (BufferedInputStream bis new BufferedInputStream(new FileInputStream(D:\\file1\\java.zip));BufferedOutputStream bos new BufferedOutputStream(new FileOutputStream(D:\\file2\\javaCopy.zip));) {// 读写数据int len;byte[] bytes new byte[8 * 1024];while ((len bis.read(bytes)) ! -1){bos.write(bytes, 0, len);}} catch (IOException e) {e.printStackTrace();}// 记录结束时间long end System.currentTimeMillis();System.out.println(缓冲流使用数组复制时间: (end - start) 毫秒);}
}使用基本流读写数据
public class BufferedDemo {public static void main(String[] args) throws FileNotFoundException {// 记录开始时间long start System.currentTimeMillis();// 创建流对象try (FileInputStream fis new FileInputStream(D:\\file1\\java.zip);FileOutputStream fos new FileOutputStream(D:\\file2\\javaCopy.zip)) {// 读写数据int b;while ((b fis.read()) ! -1){fos.write(b);}} catch (IOException e) {e.printStackTrace();}// 记录结束时间long end System.currentTimeMillis();System.out.println(普通流复制时间: (end - start) 毫秒);}
}从这里我们可以看到普通流对象读写数据与缓冲流高效流读取数据的差异性了高效流通过一次读入多个数据到缓冲区然后一次读出减少IO操作提高了读写效率。
字符缓冲流
在D:\file1目录下面创建一个in.txt文件。
构造方法
public BufferedReader(Reader in) 创建一个 新的缓冲输入流public BufferedWriter(Writer out) 创建一个新的缓冲输出流。
特殊方法
BufferedReader public String readLine() : 读一行文字。BufferedWriter public void newLine() : 写一行行分隔符,由系统属性定义符号。
代码示例
public class BufferedReaderDemo {public static void main(String[] args) throws IOException {// 创建流对象BufferedReader br new BufferedReader(new FileReader(D:\\file1\\in.txt));BufferedWriter bw new BufferedWriter(new FileWriter(D:\\file3\\out.txt));// 定义字符串,保存读取的一行文字String line null;// 循环读取,读取到最后返回nullwhile ((line br.readLine()) ! null) {bw.write(line, 0, line.length());bw.newLine();}// 释放资源bw.close();br.close();}
}转换流
字符编码和字符集
将字符存储到计算机中称为编码,反之将存储在计算机中的二进制数按照某种规则解析显示出来称为解码 。
字符编码 Character Encoding : 就是一套自然语言的字符与二进制数之间的对应规则。字符集 Charset 也叫编码表。是一个系统支持的所有字符的集合包括各国家文字、标点符号、图形符号、数字等。
常见的字符集
ASCII字符集 ASCIIAmerican Standard Code for Information Interchange美国信息交换标准代码是基于拉丁字母的一套电脑编码系统ISO-8859-1字符集拉丁码表别名Latin-1用于显示欧洲使用的语言GBxxx字符集GB就是国标的意思是为了显示中文而设计的一套字符集。Unicode字符集 Unicode编码系统为表达任意语言的任意字符而设计是业界的一种标准也称为统一码、标准万国码。
问题引入 准备一个D:\file\in.txt文件
public class ReaderDemo {public static void main(String[] args) throws IOException {FileReader fileReader new FileReader(D:\\file1\\in.txt);int read;while ((read fileReader.read()) ! -1) {System.out.print((char)read);}fileReader.close();}
}出现了中文乱码这是因为Windows系统的默认是GBK编码就会出现乱码。这个编码问题我们可以通过转换流来解决。
InputStreamReader类
转换流 java.io.InputStreamReader 是Reader的子类是从字节流到字符流的桥梁。 构造方法
InputStreamReader(InputStream in) : 创建一个使用默认字符集的字符流。InputStreamReader(InputStream in, String charsetName) : 创建一个指定字符集的字符流。
package demo10;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;public class ReaderDemo2 {public static void main(String[] args) throws IOException {// 定义文件路径,文件为gbk编码String FileName D:\\file1\\in.txt;// 创建流对象,默认UTF8编码InputStreamReader isr new InputStreamReader(new FileInputStream(FileName));// 创建流对象,指定GBK编码InputStreamReader isr2 new InputStreamReader(new FileInputStream(FileName) , GBK);// 定义变量,保存字符int read;// 使用默认编码字符流读取,乱码while ((read isr.read()) ! -1) {System.out.print((char)read);}isr.close();System.out.println();// 使用指定编码字符流读取,正常解析while ((read isr2.read()) ! -1) {System.out.print((char)read);}isr2.close();}
}这里我们使用了俩种构造方法来创建我们的InputStreamReader对象当我们指定了解码采用GBK格式来对我们的in.txt进行解码时可以看到我们输出的字符是正常的而不是乱码。
OutputStreamWriter类
转换流 java.io.OutputStreamWriter 是Writer的子类是从字符流到字节流的桥梁。 构造方法:
OutputStreamWriter(OutputStream in) : 创建一个使用默认字符集的字符流。OutputStreamWriter(OutputStream in, String charsetName) : 创建一个指定字符集的字符流。
import java.io.*;public class TransDemo {public static void main(String[] args) throws IOException {// 1.定义文件路径String srcFile D:\\file1\\in.txt;String destFile D:\\file3\\out.txt;// 2.创建流对象// 2.1 转换输入流,指定GBK编码InputStreamReader isr new InputStreamReader(new FileInputStream(srcFile) , GBK);// 2.2 转换输出流,默认utf8编码OutputStreamWriter osw new OutputStreamWriter(new FileOutputStream(destFile));// 3.读写数据// 3.1 定义数组char[] cbuf new char[1024];// 3.2 定义长度int len;// 3.3 循环读取while ((len isr.read(cbuf))!-1) {// 循环写出osw.write(cbuf,0,len);}// 4.释放资源osw.close();isr.close();}
}转换流理解图解:
序列化
Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象该字节序列包含该对象的数据、对象的 类型和对象中存储的属性等信息。
反序列化: 从文件中读取字节序列重构对象
序列化流理解图解
ObjectOutputStream类
java.io.ObjectOutputStream 类将Java对象的原始数据类型写出到文件,实现对象的持久存储。
序列化条件
该类必须实现 java.io.Serializable 接口 Serializable 是一个标记接口不实现此接口的类将不会使任何状态序列化或反序列化会抛出 NotSerializableException 。该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的则该属性必须注明是瞬态的使用transient 关键字修饰。
import java.io.Serializable;public class Employee implements Serializable {public String name;public String address;public transient int age; // transient瞬态修饰成员,不会被序列化public void addressCheck() {System.out.println(Address check : name ‐‐ address);}
}员工类实现类Serializable接口表示该类可以进行序列化操作该类中的age属性修饰了transient瞬态代表该字段不会被序列化。
如何进行序列化 创建一个D:\file2目录。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;public class SerializeDemo{public static void main(String [] args) {Employee e new Employee();e.name huge;e.address huangshi;e.age 20;try {// 创建序列化流对象ObjectOutputStream out new ObjectOutputStream(new FileOutputStream(D:\\file2\\employee.txt));// 写出对象out.writeObject(e);// 释放资源out.close();System.out.println(Serialized data is saved); // 姓名地址被序列化年龄没有被序列化。} catch(IOException i) {i.printStackTrace();}}
}我们可以看到对象已经被序列化到了指定文件之中。那么我们这么进行反序列化来检查我们是否序列化成功呢那就要使用到ObjectInputStream类了。
ObjectInputStream类
ObjectInputStream反序列化流将之前使用ObjectOutputStream序列化的原始数据恢复为对象。
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;public class DeserializeDemo {public static void main(String [] args) {Employee e null;try {// 创建反序列化流FileInputStream fileIn new FileInputStream(D:\\file2\\employee.txt);ObjectInputStream in new ObjectInputStream(fileIn);// 读取一个对象e (Employee) in.readObject();// 释放资源in.close();fileIn.close();}catch(IOException i) {// 捕获其他异常i.printStackTrace();return;}catch(ClassNotFoundException c) {// 捕获类找不到异常System.out.println(Employee class not found);c.printStackTrace();return;}// 无异常,直接打印输出System.out.println(Name: e.name); // hugeSystem.out.println(Address: e.address); // huangshiSystem.out.println(age: e.age); // 0}
}通过控制台显示数据我们可以看到我们进行序列化的对象是正确的。 反序列化失败情况
该类的序列版本号与从流中读取的类描述符的版本号不匹配该类包含未知数据类型该类没有可访问的无参数构造方法
Serializable 接口给需要序列化的类提供了一个序列版本号。 serialVersionUID 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。
public class Employee implements java.io.Serializable {// 加入序列版本号private static final long serialVersionUID 1L;public String name;public String address;// 添加新的属性 ,重新编译, 可以反序列化,该属性赋为默认值.public int eid;public void addressCheck() {System.out.println(Address check : name ‐‐ address);}
}打印流
java.io.PrintStream 类该类能够方便地打印各种数据类型的值 构造方法:
public PrintStream(String fileName) 使用指定的文件名创建一个新的打印流。
改变流向: public class PrintDemo { public static void main(String[] args) throws IOException { // 调用系统的打印流,控制台直接输出97 System.out.println(97); // 创建打印流,指定文件的名称 PrintStream ps new PrintStream(“ps.txt”); // 设置系统的打印流流向,输出到ps.txt System.setOut(ps); // 调用系统的打印流,ps.txt中输出97 System.out.println(97); } } 我们可以看到我们的数据输出的流向通过System.setOut(ps);改变了输出到D:\file3\out.txt中了。日志输出相似
欢迎java热爱者了解文章作者将会持续更新中期待各位友友的关注和收藏。。。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/931694.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!