黑马程序员技术交流社区
标题:
黑马程序员-Java基础-转换流的使用
[打印本页]
作者:
QQheima
时间:
2015-6-12 21:28
标题:
黑马程序员-Java基础-转换流的使用
学到I/O流后,字节流、字符流的各个类很多,在开发过程中,判断使用哪个类是比较困难的。下面总结了选取流类型的方法,并提供了各种事例程序。在程序中也体现了字符流中的编码问题。
public class ReadIn {
public static void main(String[] args) throws IOException{
//testInputStream();
//testIO();
//readLine();
//write_1();
//write_2();
//read_1();
//read_2();
system_in_out();
}
public static void testInputStream() throws IOException{
InputStream in = System.in;
int by = in.read();
int by2 = in.read();
int by3 = in.read();
int by4 = in.read();
//回车符也会被读取到,/r/n
System.out.println(by);
System.out.println(by2);
System.out.println(by3);
System.out.println(by4);
}
public static void testIO() throws IOException{
//包装输入/输出流
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw =
new BufferedWriter(new OutputStreamWriter(System.out));
}
/*
* 需求:实现控制台readLine方法。
* 通过键盘录入数据
* 当录入一行数据后,就将该行数据进行打印
* 如果录入的数据是over,就结束程序
*
* */
public static void readLine() throws IOException{
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
int ch;
int i = 0;
while(true){
ch = in.read();
if(ch=='\r')
continue;
if(ch=='\n'){
if("over".equals(sb.toString())){
break;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());
}else{
sb.append((char)ch);
}
}
}
/*
* 需求:
* 从控制台(键盘)录入数据,保存到文本中(硬盘),使用系统默认GBK编码。
*
* 分析:
* 1、
* 从控制台录入数据:Reader/InputStream
* 录入文本内容:Reader
* 键盘设备:标准输入流System.in
* 使用系统默认GBK编码:InputStreamReader
* 需要提高读取效率:BufferedReader
*
* 2、
* 向硬盘写数据:Writer/OutputStream
* 写入的是文本文件:Writer
* 使用系统默认GBK编码:FileWriter
* 需要提高写入效率:BufferedWriter
* */
public static void write_1(){
BufferedReader bufr = null;
BufferedWriter bufw =null;
try{
bufr = new BufferedReader(new InputStreamReader(System.in,"GBK"));//不写"GBK"即为默认
bufw = new BufferedWriter(new FileWriter("TextFileFromSystemIn_GBK.txt"));//使用默认编码方式写入文件
String s;
while(!"over".equals(s=bufr.readLine())){
bufw.write(s);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
}catch(IOException e){
throw new RuntimeException("读取/写入失败!");
}finally{
try{
if(null!=bufr)
bufr.close();
}catch(IOException re){
throw new RuntimeException("关闭读取流失败!");
}
try{
if(null!=bufw)
bufr.close();
}catch(IOException we){
throw new RuntimeException("关闭写入流失败!");
}
}
}
/*
* 需求:
* 从控制台(键盘)录入数据,保存到文本中(硬盘),使用指定UTF-8编码。
*
* 分析:
* 1、
* 从控制台录入数据:Reader/InputStream
* 录入文本内容:Reader
* 键盘设备:标准输入流System.in
* 使用指定的*****UTF-8*****编码:InputStreamReader
* 需要提高读取效率:BufferedReader
*
* 2、
* 向硬盘写数据:Writer/OutputStream
* 写入的是文本文件:Writer
* 使用指定的*****UTF-8*****编码:FileOutputStream
* 需要提高写入效率:BufferedWriter
* */
public static void write_2(){
BufferedReader bufr = null;
BufferedWriter bufw =null;
try{
bufr = new BufferedReader(new InputStreamReader(System.in));
//将字节流(FileOutputStream)通过桥梁(OutputStreamWriter)转换成字符流(BufferedWriter);
bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("TextFileFromSystemIn_UTF-8.txt"), "UTF-8"));
//以下写法错误,OutputStreamWriter是字符流通向字节流的桥梁,不能使用字符流参数
//bufw = new BufferedWriter(new OutputStreamWriter(new FileWriter(""), "UTF-8"));
String s;
while(!"over".equals(s=bufr.readLine())){
bufw.write(s);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
}catch(IOException e){
throw new RuntimeException("读取/写入失败!");
}finally{
try{
if(null!=bufr)
bufr.close();
}catch(IOException re){
throw new RuntimeException("关闭读取流失败!");
}
try{
if(null!=bufw)
bufr.close();
}catch(IOException we){
throw new RuntimeException("关闭写入流失败!");
}
}
}
/*
* 需求:
* 从硬盘读取一个文本文件,将内容打印到控制台,使用系统默认GBK编码。
* 分析:
* 1、
* 读取数据:Reader/InputStream
* 读取文本:Reader
* 硬盘文件:FileReader
* 编码系统默认GBK:FileReader
* 需要优化效率:BufferedReader
*
* 2、
* 输出数据:Writer/OutputStream
* 文本:Writer
* 输出到控制台:标准输出设备System.out
* 编码系统默认GBK:System.out
* (需要转换编码时就用到OutputStreamWriter,)
* 需要提高效率:BufferedWriter
* (此处只是为了做演示,所以用BufferedWriter包装System.out,此时就用到了OutputStreamWriter)
*
* */
public static void read_1(){
BufferedReader bufr = null;
BufferedWriter bufw = null;
try{
//此处读取的是GBK编码文件,若读取UTF-8编码的文件会出现乱码
bufr = new BufferedReader(new FileReader("TextFileFromSystemIn_GBK.txt"));
//乱码
//bufr = new BufferedReader(new FileReader("TextFileFromSystemIn_UTF-8.txt"));
bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String s ;
while((s=bufr.readLine())!=null){
bufw.write(s);
bufw.newLine();
bufw.flush();
}
}catch(IOException e){
throw new RuntimeException("读取/写入失败!");
}finally{
try{
if(null!=bufr)
bufr.close();
}catch(IOException re){
throw new RuntimeException("关闭读取流失败!");
}
try{
if(null!=bufw)
bufr.close();
}catch(IOException we){
throw new RuntimeException("关闭写入流失败!");
}
}
}
/*
* 需求:
* 从硬盘读取一个文本文件,将内容打印到控制台,使用指定UTF-8编码。
*
* * 分析:
* 1、
* 读取数据:Reader/InputStream
* 读取文本:Reader
* 硬盘文件:FileReader
* 编码指定UTF-8:******InputStreamReader******
* 需要优化效率:BufferedReader
*
* 2、
* 输出数据:Writer/OutputStream
* 文本:Writer
* 输出到控制台:标准输出设备System.out
* 编码系统默认GBK:System.out
* (需要转换编码时就用到OutputStreamWriter,)
* 需要提高效率:BufferedWriter
* (此处只是为了做演示,所以用BufferedWriter包装System.out,此时就用到了OutputStreamWriter)
*
* */
public static void read_2(){
BufferedReader bufr = null;
BufferedWriter bufw = null;
try{
//此处读取的是UTF-8编码的文件,需要用字节流读取,然后指定UTF-8编码查表,再转换成字符流。
bufr = new BufferedReader(new InputStreamReader(new FileInputStream("TextFileFromSystemIn_UTF-8.txt"),"UTF-8"));
//标准输出设备接收的是GBK编码的字符流,不能指定为"UTF-8",否则乱码
bufw = new BufferedWriter(new OutputStreamWriter(System.out));
//乱码
//bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String s ;
while((s=bufr.readLine())!=null){
bufw.write(s);
bufw.newLine();
bufw.flush();
}
}catch(IOException e){
throw new RuntimeException("读取/写入失败!");
}finally{
try{
if(null!=bufr)
bufr.close();
}catch(IOException re){
throw new RuntimeException("关闭读取流失败!");
}
try{
if(null!=bufw)
bufr.close();
}catch(IOException we){
throw new RuntimeException("关闭写入流失败!");
}
}
}
/**
* 重定向标准输入/输出设备
* 将System.in和System.out的源和目的,改成指定的文件
* 实现文件的复制
* @throws IOException
*/
public static void system_in_out() throws IOException{
//指定数据源为一个文件
System.setIn(new FileInputStream("CopyTextFile.java"));
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//指定输出到另一个文件
System.setOut(new PrintStream("PrintStream.txt"));
String s;
//将文件内复制到另一个文件
while((s=bufr.readLine())!=null){
System.out.println(s);
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2