现在的位置: 首页 > 综合 > 正文

IO之转换流

2013年10月28日 ⁄ 综合 ⁄ 共 1282字 ⁄ 字号 评论关闭

转换流属于处理流的一种。。。很重要。。。很常用。。。

 

小例子:

package heng.java.IO3;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/**
 * 转换流
 * @author haley
 *
 */
public class TestTransForm {
	public static void main(String[] args) {
		OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(
					new FileOutputStream("e:\\Order\\TCPTwoTalkClient.java"));
			osw.write("直接输入字符串,其实是将字节转换成字符了");
			System.out.println(osw.getEncoding());//打印osw的字节编码
			osw.close();
			//true是让将要写的东西接着文件的尾部。如果不写true,则会覆盖掉源文件
			osw = new OutputStreamWriter(//latin-1是汉字的编码
					new FileOutputStream("e:\\Order\\TCPTwoTalkClient.java",true),"ISO8859_1");//最后的参数是输入的编码
			osw.write("ghjggilugiu");
			System.out.println(osw.getEncoding());
			osw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

================================================================

 

package heng.java.IO3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestTransForm1 {
	public static void main(String[] args) {
		InputStreamReader isr = null;
		
		String s = null;
		try {
			isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);
			s = br.readLine();
			while(s != null){
				if(s.equalsIgnoreCase("exit")) break;
				System.out.println(s.toUpperCase());
				s = br.readLine();
			}
			br.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

抱歉!评论已关闭.