1 JAVA.IO字节流
inputstream.png
- //用ByteArrayOutputStream暂时缓存来自其他渠道的数据
- ByteArrayOutputStream data = new ByteArrayOutputStream(1024); //1024字节大小的缓存区
- data.write(System.in.read()); // 暂存用户输入数据
- //将data转为ByteArrayInputStream
- ByteArrayInputStream in = new ByteArrayInputStream(data.toByteArray());
- //java对象的写入
- FileOutputStream fileStream = new FileOutputStream("example.txt");
- ObjectOutputStream out = new ObjectOutputStream(fileStream);
- Example example = new Example();
- out.writeObject(example);
- //java对象的读取
- FileInputStream fileStream = new FileInputStream("example.txt");
- ObjectInputStream in = new ObjectInputStream(fileStream);
- Example = (Example) in.readObject();
- // 创建一个发送者对象
- Sender sender = new Sender(); // 创建一个接收者对象
- Receiver receiver = new Receiver(); // 获取输出管道流
- // 获取输入输出管道流
- PipedOutputStream outputStream = sender.getOutputStream();
- PipedInputStream inputStream = receiver.getInputStream();
- // 链接两个管道,这一步很重要,把输入流和输出流联通起来
- outputStream.connect(inputStream);
- sender.start();// 启动发送者线程
- receiver.start();// 启动接收者线程
- InputStream in1 = new FileInputStream("example1.txt");
- InputStream in2 = new FileInputStream("example2.txt");
- SequenceInputStream sequenceInputStream = new SequenceInputStream(in1, in2);
- //数据读取
- int data = sequenceInputStream.read();
- ByteArrayOutputStream out = new ByteArrayOutputStream(2014);
- //数据写入,使用DataOutputStream装饰一个InputStream
- //使用InputStream具有对基本数据的处理能力
- DataOutputStream dataOut = new DataOutputStream(out);
- dataOut.writeDouble(1.0);
- //数据读取
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- DataInputStream dataIn = new DataInputStream(in);
- Double data = dataIn.readDouble();
2 JAVA.IO字符流
21.png
- //InputStream转为Reader
- InputStream inputStream = new ByteArrayInputStream("程序".getBytes());
- InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
- //OutputStream转为Writer
- OutputStream out = new FileOutputStream("example.txt");
- OutputStreamWriter writer = new OutputStreamWriter(out);
- //以字符为单位读写
- writer.write(reader.read(new char[2]));
3 乱码问题和字符流
- ByteArrayInputStream in = new ByteArrayInputStream("程序大法好".getBytes());
- byte[] buf = new byte[2]; //读取流的两个字节
- in.read(buf); //读取数据
- System.out.println(new String(buf)); //乱码
- ---result----
- � //乱码
- InputStreamReader reader =
- new InputStreamReader(inputStream, StandardCharsets.UTF_8);
4 字符集和字符编码的概念区分
范围 Unicode(Binary) UTF-8编码(Binary) UTF-8编码byte长度 U+0000~U+007F 00000000 00000000 00000000 0XXXXXXX 0XXXXXX 1 U+0080~U+07FF 00000000 00000000 00000YYY YYXXXXXX 110YYYYY 10XXXXXX 2 U+0800~U+FFFF 00000000 00000000 ZZZZYYYY YYXXXXXX 1110ZZZZ 10YYYYYY 10XXXXXX 3 U+010000~U+10FFFF 00000000 000AAAZZ ZZZZYYYY YYXXXXXX 11110AAA 10ZZZZZZ 10YYYYYY 10XXXXXX 4
- System.out.println(Charset.defaultCharset()); //输出java默认编码
- for (byte item : "程序".getBytes(StandardCharsets.UTF_16)) {
- System.out.print("[" + item + "]");
- }
- System.out.println("");
- for (byte item : "程序".getBytes(StandardCharsets.UTF_8)) {
- System.out.print("[" + item + "]");
- }
- ----result----
- UTF-8 //java默认编码UTF-8
- [-2][-1][122][11][94][-113] //UTF_16:6个字节?
- [-25][-88][-117][-27][-70][-113] //UTF_8:6个字节 正常
- for (byte item : "程".getBytes(StandardCharsets.UTF_16)) {
- System.out.print("[" + item + "]");
- }
- ---result--
- [-2][-1][122][11]
【编辑推荐】
分享名称:一文让你读懂JAVA.IO、字符编码
链接URL:http://www.shufengxianlan.com/qtweb/news41/446741.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联