IO 内存流和打印流

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(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4700331845c45ec07a839401b94beb2e.html