手机版
你好,游客 登录 注册
背景:
阅读新闻

Java - IO 内存流和打印流

[日期:2019-07-13] 来源:Linux社区  作者:wangyuyang1016 [字体: ]

IO 内存流和打印流操作

字符编码

  • 计算机中所有的信息组成都是二进制数据,所有能够描述的中文文字都是经过处理后的结果;所有的语言文字都会使用编码来进行描述,例如:ASCII码

常见编码

GBK/GB2312:

  • 中文的国标编码
  • GBK包含有简体中文与繁体中文两种,而GB2312只包含简体中文

ISO-8859-1:

  • 国际编码
  • 可以描述任何的文字信息

UNICODE:

  • 十六进制编码
  • 任何文字信息都用十六进制表示,会导致无用数据过多

UTF-8:*

  • 融合ISO8859-1和UNICODE两种编码的特点

字符乱码

本质:

  • 编码与解码的字符集不统一

列出系统的所有环境变量

public class TestDemo {
    public static void main(String [] args) throws IOException {
        System.getProperties().list(System.out);
    }
}
  • 输出结果
file.encoding=UTF-8

代表系统环境默认的字符集编码为:UTF-8

public class TestDemo {
    public static void main(String [] args) throws IOException {
        File file = new File("F:" + File.separator + 
                "demo" + File.separator +
                "demo.txt");
        OutputStream out = new FileOutputStream(file);
        out.write("华为".getBytes());// getBytes() > 无转码
        out.close();
    }
}

上述是以默认的系统编码方式进行输出

华为

可以看出来,利用系统默认的编码方式(不转码)编码输出,在用系统的编码方式解码,不会出现乱码现象

public class TestDemo {
    public static void main(String [] args) throws IOException {
        File file = new File("F:" + File.separator + 
                "demo" + File.separator +
                "demo.txt");
        OutputStream out = new FileOutputStream(file);
        out.write("华为".getBytes("ISO8859-1"));// getBytes() > 无转码
        out.close();
    }
}

我们利用getBytes()方法将进行转码为ISO8859-1编码方式输出

??

由结果看出,系统使用GB2312进行解码,而文件是使用ISO8859-1进行编码,编码和解码的字符集不同由此导致了 乱码现象 的出现

内存操作流

  • 在不产生新文件的情况下;利用内存流来实现输入与输出的操作

字节内存流:

public class ByteArrayInputStream
extends InputStream
public class ByteArrayOutputStream
extends OutputStream

ByteArrayInputStream

  • 构造方法
public ByteArrayInputStream(byte [] buf)

将要操作的数据设置到内存输入流

ByteArrayOutputStream

  • 构造方法
public ByteArrayOutputStream()

内存输出流(输出数据 )

toByteArray()*

public byte [] toByteArray()
  • 将所有保存在内存中的字节数据变为字节数组存在
  • 将两个文件利用 toByteArray() 进行合并输出*
public class TestDemo {
    public static void main(String [] args) throws IOException {
        File fileA = new File("F:" + File.separator + "demo" + File.separator + "demo.txt");
        File fileB = new File("F:" + File.separator + "demo" + File.separator + "data.txt");
        InputStream inA = new FileInputStream(fileA);
        InputStream inB = new FileInputStream(fileB);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int temp = 0 ;
        while((temp = inA.read()) != -1) { //读取A数据
            output.write(temp);
        }
        while((temp = inB.read()) != -1) { //读取B数据
            output.write(temp);
        }
        // 读取A,B文件结束后,将内存中的所有字节数据转为字节数组
        byte [] data = output.toByteArray();
        inA.close();
        inB.close();
        output.close();
        System.out.println(new String(data));
    }
}

实例

public class TestDemo {
    public static void main(String [] args) throws IOException {
        String str = "Hello,World!";
        InputStream in = new ByteArrayInputStream(str.getBytes());
        // 将所有要读取的数据设置大内存输入流中
        OutputStream out = new ByteArrayOutputStream();
        // 内存输出流
        int temp = 0 ;// 读取到的每一个字节数据
        while ((temp = in.read()) != -1) { // 每次读取一个字节数据
            out.write(Character.toUpperCase(temp));//字节输出流
            //   temp数据转大写并输出到内存输出流当中
        }
        System.out.println(out);
        in.close();
        out.close();
    }
}

字符内存流:

public class CharArrayReader
extends Reader
public class CharArrayWriter
extends Writer

打印流

接触打印流

如果使用OutputStream,输出String字符串数据,就需要将String变为字节数组输出getBytes(),同理boolean也需要变为Byte数据输出……

package helloworld;

import Java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

class PrintUtil {
    private OutputStream out; // 输出依靠 OutputSteam类
    public PrintUtil(OutputStream out) {
        this.out = out ; //确定OutputStream的对象是File……或者ByteArray……
    }
    public void print(int x) throws IOException {
        this.print(String.valueOf(x));//将x转为String型
    }
    public void print(String x) throws IOException {
        this.out.write(x.getBytes());
    }
    public void print(double x) throws IOException {
        this.print(String.valueOf(x));//将x转为String型
    }
    public void println(int x) throws IOException {
        this.println(String.valueOf(x));
    }
    public void println(String x) throws IOException {
        this.print(x.concat("\r\n"));
        //在String字符串结尾添加字符[concat()]
    }
    public void println(double x) throws IOException {
        this.println(String.valueOf(x));
    }
    public void close() throws IOException {//关闭输出流
        this.out.close();
    }
}

public class TestDemo {
    public static void main(String [] args) throws IOException {
        // 调用PrintUtil类的构造方法,实例化对象
        PrintUtil pu = new PrintUtil(
                new FileOutputStream(
                        new File("F:" 
                                    + File.separator + "demo" 
                                    + File.separator + "demo.txt")));
        pu.print("Hello,");
        pu.println("World!");
        pu.println(1+1);
        pu.println(1.1+1.1);
        pu.close();
    }
}

PrintUtil类,则是为了方便打印而设计的一个工具类,在类中,我们通过调用print方法,可以将当前的数据转为String后在转为Byte型数据,可以方便我们的数据输出;避免我们在代码编写过程中浪费时间来设计数据类型转换为Byte字节输出。

打印流

  • 为了解决上述的数据输出时的功能不足问题,java.io包提供了一套专门用于输出数据的类:PrintStream(打印字节流)PrintWriter(打印字符流)

PrintStream:字节打印流

  • 继承结构
java.lang.Object
    java.io.OutputStream
        java.io.FileOutputStream
            java.io.PrintStream
  • 构造方法
PrintStream(OutputStream out)

在PrintStream类中提供了一系列和上述 PrintUtil 类相似的print()、println()方法;这些方法都可支持各种数据类型的输出,同理:使用了PrintStream,那么就可以不用去使用write()字节流输出方法了。

PrintStream类的实现本质上是基于OutputStream类实现的;这类的设计在Java中称为 装饰设计模式 相当于将一个功能不足的操作类,通过某些类的包装实现成功能健全的更好的操作类。

  • 实现 PrintStream
public class TestDemo {
    public static void main(String [] args) throws IOException {
        // 调用PrintStream类的构造方法,实例化对象
        PrintStream pu = new PrintStream(
                new FileOutputStream(
                        new File("F:" 
                                    + File.separator + "demo" 
                                    + File.separator + "demo.txt")));
        pu.print("Hello,");
        pu.println("World!");
        pu.println(1+1);
        pu.println(1.1+1.1);
        pu.close();
    }
}

将PrintUtil类删除,使用PrintStream类代替,可以看出操作方法如出一辙。

PrintWrite:字符打印流

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2019-07/159323.htm

linux
相关资讯       IO内存流  IO打印流 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款