字节内存操作类/字符内存操作类(Byte memory operation class / character memory operation class)

package demo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MemoryIODemo {

	public static void main(String[] args) throws Exception {
		//System.getProperties().list(System.out);		//列出当前系统的属性
		File f=new File("D:"+File.separator+"abc.txt");
		OutputStream output1=new FileOutputStream(f);
		//ISO不适用中文,会出现乱码
		output1.write("中华人民共和国".getBytes("ISO8859-1") );
		
		//以内存为终端做IO操作,input先写内存,output读内存
		//字节内存操作类:ByteArrayOutputStream , ByteArrayInputStream
		//字符内存操作类:CharArrayWriter , CharArrayReader
		String str="abCd";
		//写入内存
		InputStream input2=new ByteArrayInputStream(str.getBytes());
		//从内存读出
		ByteArrayOutputStream output2=new ByteArrayOutputStream();
		int data=0;
		while( (data=input2.read())!=-1 ) {		//每次读一个字节,并且转为大写
			output2.write(Character.toUpperCase(data));
		}
		byte[] res=output2.toByteArray();
		System.out.println(new String(res));
		output2.close();
		input2.close();
	}

}

————————
package demo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MemoryIODemo {

	public static void main(String[] args) throws Exception {
		//System.getProperties().list(System.out);		//列出当前系统的属性
		File f=new File("D:"+File.separator+"abc.txt");
		OutputStream output1=new FileOutputStream(f);
		//ISO不适用中文,会出现乱码
		output1.write("中华人民共和国".getBytes("ISO8859-1") );
		
		//以内存为终端做IO操作,input先写内存,output读内存
		//字节内存操作类:ByteArrayOutputStream , ByteArrayInputStream
		//字符内存操作类:CharArrayWriter , CharArrayReader
		String str="abCd";
		//写入内存
		InputStream input2=new ByteArrayInputStream(str.getBytes());
		//从内存读出
		ByteArrayOutputStream output2=new ByteArrayOutputStream();
		int data=0;
		while( (data=input2.read())!=-1 ) {		//每次读一个字节,并且转为大写
			output2.write(Character.toUpperCase(data));
		}
		byte[] res=output2.toByteArray();
		System.out.println(new String(res));
		output2.close();
		input2.close();
	}

}