java学习之IO流2

读取键盘输入

示例1:

下图中第3行建立字节读取流对象,获取键盘读取流System.in。第4行使用字节读取流的read()方法来读键盘数据。第5行进行输出。

	private static void readKey1() throws IOException 
	{
		InputStream in = System.in;
		int ch = in.read();
		System.out.println(ch);
	}
示例2:
下图中使用循环读取键盘输入的数据。当键入数据时按下回车键,此时回车键也会被读取进去,并打印出对于的ACSII码。如果输入的是汉字,则会分成两个字节读取。

	public static void readKey2() throws IOException 
	{
		InputStream in = System.in;
		int ch = 0;
		while((ch = in.read()) != -1)
		{
			System.out.println(ch);
		}
	}

获取用户键盘录入的数据,并将数据变成大写显示在控制台上,如果输入over,结束键盘录入。

	public static void readKey3() throws IOException 
	{
		//1.创建容器
		StringBuilder sb = new StringBuilder();
		//2.获取键盘读取流
		InputStream in = System.in;
		//3.定义变量,记录读取到的字节,并循环获取。
		int ch = 0;
		while((ch = in.read()) != -1)
		{
			//在存储之前需要判断是否是换行标记,因为换行标记不存储。
			if(ch == '\r')
			{
				continue;
			}
			if(ch == '\n')
			{
				String temp = sb.toString();
				if("over".equals(temp))
				{
					break;
				}
				System.out.println(temp.toUpperCase());
				sb.delete(0, sb.length());
			}
			else
			{
				//将读取到的字节存储到StringBuilder中
				sb.append((char)ch);
			}
		}
	}

转换流:
InputStreamReader:字节到字符的桥梁。解码。
OutputStreamWriter:字符到字节的桥梁。编码。

下图中第4行定义了一个转换流对象,构造函数中传递的是一个字节流,就把字节流转换成字符流了。然后在用字符缓冲区进行从键盘读取数据的操作。第8行定义了一个转换流对象,把字节流转换成为字符流。其实就是上个例子的另一个实现方式。

	public static void demo1() throws IOException
	{
		InputStream in = System.in;
		InputStreamReader isr = new InputStreamReader(in);
		BufferedReader bufr = new BufferedReader(isr);
		
		OutputStream out = System.out;
		OutputStreamWriter osw = new OutputStreamWriter(out);
		BufferedWriter bufw = new BufferedWriter(osw);
		
		String line = null;
		while((line = bufr.readLine()) != null)
		{
			if("over".equals(line))
			{
				break;
			}
			bufw.write(line.toUpperCase());
			bufw.newLine();
			bufw.flush();
		}
	}


转换流的编码和解码:
下图中writeText_1()方法使用字符流操作文件,将“你好”写到gbk.txt中。使用的码表是默认码表GBK,写入后文件是4个字节,即每个汉字是2个字节。writeText_2()方法使用转换流,使用码表是指定码表UTF-8,将“你好”写入文件后是6个字节,即每个汉字是3个字节。用转换流对文件进行读写才可以指定编码表。

	public static void writeText_1() throws IOException
	{
		FileWriter fw = new FileWriter("gbk.txt");
		fw.write("你好");
		fw.close();
	}

	public static void writeText_2() throws IOException 
	{
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"), "UTF-8");
		osw.write("你好");
		osw.close();
	}


下图中readText_1()方法使用默认的编码表GBK读取文件gbk.txt,因为上图中是用相同编码表把字符串写到文件中去。readText_2()方法使用了指定的编码表"UTF-8"来读取文件u8_1.txt,因为上图中使用相同的编码表把字符串写到文件中去。如果使用不同的编码表进行编码解码,那可能读出的东西不是你想要的结果。

	public static void readText_1() throws IOException 
	{
		FileReader fr = new FileReader("gbk.txt");
		char[] buf = new char[10];
		int len = fr.read(buf);
		String str = new String(buf, 0, len);
		System.out.println(str);
		fr.close();
	}

	public static void readText_2() throws IOException
	{
		InputStreamReader isr = new InputStreamReader(new FileInputStream("u8_1.txt"), "UTF-8");
		char[] buf = new char[10];
		int len = isr.read(buf);
		String str = new String(buf, 0, len);
		System.out.println(str);
		isr.close();
	}

流的操作规律:
之所以要弄清楚这个规律,是因为流对象太多,开发时不知道用哪个对象合适。

想要知道开发时用到哪些对象,只要通过四个明确即可。
1、明确源和目的(汇)
       源:InputStream Reader
       目的:OutputStream Writer
2、明确数据是否是纯文本数据。
      源:
           是纯文本: Reader
           不是纯文本: InputStream
       目的:
           是纯文本: Writer        
           不是纯文本: OutputStream
3、明确具体的设备。
       源设备:
               硬盘:File
               键盘:System.in
               内存:数组
               网络: Socket流   
       目的设备:
               硬盘:File
               控制台:System.out
               内存:数组
               网络:Socket流
4、是否需要其他额外功能
    (1)是否需要高效(缓冲区)
                          是,就加上buffer
    (2)转换






发布了53 篇原创文章 · 获赞 18 · 访问量 24万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章