JAVA学习第五十课 — IO流(四)转换流

一、键盘录入

System.out:标准输出设备,控制台

System.in:标准输入设备,键盘

输入演示

输入字符串遇到回车,打印,输入over,输入结束

PS:键盘的录入只读取一个字节,先将字节拼一个字符串,所以需要一个容器,再参与over的判断

import java.io.*;

public class Main 
{
	public static void main(String[] args) throws IOException {
		
		InputStream in = System.in;
		StringBuilder sb =  new StringBuilder();
		int t = 0;
		while(true){
			t = in.read();Windows下回车是'\r\n'
			if(t== 13)continue;//'\r' = 13
			if(t==10){//'\n' = 10
				String str = sb.toString();
				if(str.equals("over"))
					break;
				System.out.println(str);
				sb.delete(0, sb.length());//注意情况,否则字符串会越来越长
			}
			else {
				sb.append((char)t);
			}
		}
		System.out.println(t);
	}
}

read():是阻塞式方法,没数据就一直等待

注意:默认的输入输出设备不要关,一旦关闭,以后再也无法创建流对象了

二、转换流

因为有readLine()方法,就不必要一个字符一个字符的读取,这就涉及到了字符流转换到字符流

在Reader类中有一个InputStreamReader()类,用来转换字节与字符

API文档解释:InputStreamReader是字节流通向字符流的桥梁:它使用指定的charset 读取字节并将其解码为字符。

而字符流的由来:字符流+编码表

字节流转字符流

import java.io.*;

public class Main 
{
	public static void main(String[] args) throws IOException {
		
		InDemo();
	}

	public static void InDemo() throws IOException{
		
		InputStream in  = System.in;//字节流
		
		InputStreamReader ins = new InputStreamReader(in);//转换字符流
		
		BufferedReader br = new BufferedReader(ins);//缓冲区,增强
		
		String str = null;
		while((str = br.readLine())!=null){
			if(str.equals("over"))break;
			
			System.out.println(str);
		}
	}
}

字符流转字节流

在Writer类下有OuputStreamWriter类

API文档解释:OutputStreamWriter是字符流通向字节流的桥梁:可使用指定的charset 将要写入流中的字符编码成字节。

import java.io.*;

public class Main 
{
	private static String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws IOException {
		
		InDemo();
	}

	public static void InDemo() throws IOException{
		
		/*InputStream in  = System.in;//字节流
		
		InputStreamReader ins = new InputStreamReader(in);//转换字符流
		
		BufferedReader br = new BufferedReader(ins);*/
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		/*OutputStream out = System.out;//标准输出流
		
		OutputStreamWriter osw = new OutputStreamWriter(out);
		
		BufferedWriter bw = new BufferedWriter(osw);*/
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		String str = null;
		
		while((str = br.readLine())!=null){
			if(str.equals("over"))break;
			//将字符数据用缓冲区对象写入缓冲区中,最终目的地osw->out->控制台
			bw.write(str+LINE_SEPARATOR);
			bw.flush();
		}
	}
}


三、转换流演示

将数据写到文本文件中

import java.io.*;

public class Main 
{
	private static String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws IOException {
		
		InDemo();
	}

	public static void InDemo() throws IOException{
	
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("acm.txt")));
		
		String str = null;
		
		while((str = br.readLine())!=null){
			if(str.equals("over"))break;
			bw.write(str+LINE_SEPARATOR);
			bw.flush();
		}
	}
}

将一个文本文件显示到控制台

import java.io.*;

public class Main 
{
	private static String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws IOException {
		
		InDemo();
	}

	public static void InDemo() throws IOException{
	
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("acm.txt")));
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		String str = null;
		
		while((str = br.readLine())!=null){
			if(str.equals("over"))break;
			bw.write(str+LINE_SEPARATOR);
			bw.flush();
		}
	}
}

将一个文件的内容复制到另一个文件中

import java.io.*;

public class Main 
{
	private static String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws IOException {
		
		InDemo();
	}
	public static void InDemo() throws IOException{
	
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("acm.txt")));
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("A.txt")));
		
		String str = null;
		
		while((str = br.readLine())!=null){
			if(str.equals("over"))break;
			bw.write(str+LINE_SEPARATOR);
			bw.flush();
		}
	}
}

上述三个例子,改变的只是流的源和目的地






郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。