一、字符编码
实际的开发过程中的常用编码:
- GBK/GB2312:国标编码,可以描述中文信息。
- ISO8859-1:国际通用编码,可以用其描述所有的字母信息。
- UNICODE编码:采用16进制的方式存储。
- UTF编码:象形文字部分采用16进制编码,而普通字符采用ISO8859-1,它的优势在于可以快速传输,节约带宽,也成为了我们开发中首选的编码,主要使用‘UTF-8’编码。
范例:列出本机属性
public class Main { public static void main(String[] args) { System.getProperties().list(System.out); } }
默认编码为:UTF-8
项目中出现乱码问题 就是编码和解码标准不统一,而最好的解决乱码的方式,就是都使用utf-8,保证编码统一。
二、内存操作流
在之前使用的全部都是文件操作流,文件操作流的特点,程序利用InputStream读取文件内容,而后程序利用OutputStream向文件输出内容,所有的操作都是以文件为终端的。
假设说现在需要实现IO操作,但是不希望产生文件就可以用内存作为终端来处理。
在Java里面提供了两类的额内存操作流:
- 字节内存操作流:ByteArrayOutputStream、ByteArrayInputStream;
- 字符内存操作流:CharArrayWriter、CharArrayReader
ByteArrayInputStream继承关系:
CharArrayWriter继承关系:
在ByteArrayOutputStream类里面有一个重要的方法,这个方法可以获取全部保存在内存流中的数据信息,该方法为:
- 获取数据:public byte[] toByteArray();
- 使用字符串的形式来获取:public String toString();
范例:内存输入流
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class Main { public static void main(String[] args) throws Exception{ String str = "www.baidu.com"; InputStream inputStream = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int data = 0; while ((data = inputStream.read()) != -1){ outputStream.write(Character.toUpperCase(data)); } byte result[] = outputStream.toByteArray(); System.out.println(new String(result)); inputStream.close(); outputStream.close(); } }
三、管道流
主要功能是实现两个线程之间的IO处理操作。
对于管道流也是分为两类:
- 字节管道流:PipedOutputStream、PipedInputStream
连接处理:public void connect(PipedOutputStream src) throws IOException
- 字符管道流:PipedWriter、PipedReader
连接处理:public void connect(PipedReader snk) throws IOException
范例:管道流操作
import java.io.*; class SendThread implements Runnable{ private PipedOutputStream output; public SendThread(){ this.output = new PipedOutputStream(); } public void run(){ for(int i = 0; i< 10 ; i++) { try { this.output.write(("【第"+(i+1)+"个消息发送" + Thread.currentThread().getName() + "】\n").getBytes()); } catch (IOException e) { e.printStackTrace(); } } try { this.output.close(); } catch (IOException e) { e.printStackTrace(); } } public PipedOutputStream getOutputStreamStream(){ return output; } } class ReceiveThread implements Runnable{ private PipedInputStream input; public ReceiveThread(){ this.input = new PipedInputStream(); } public void run(){ byte data[] = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream();//所有的数据保存在内存输出流 try { while((len = this.input.read(data)) != -1){ bos.write(data,0,len); } System.out.println("{"+Thread.currentThread().getName()+"接受消息}\n"+new String(bos.toByteArray())); bos.close(); } catch (IOException e) { e.printStackTrace(); } try { this.input.close(); } catch (IOException e) { e.printStackTrace(); } } public PipedInputStream getInput(){ return input; } } public class Main { public static void main(String[] args) throws Exception{ SendThread send = new SendThread(); ReceiveThread receive = new ReceiveThread(); send.getOutputStreamStream().connect(receive.getInput()); new Thread(send,"消息发送线程").start(); new Thread(receive,"消息接收线程").start(); } }
注:初学者,写的不好请见谅,如有相关问题记得私信我