0330+0331

每种输入、输出流又可以分为字节流和字符流。
按处理数据来分:
      字节流:适应性广,功能别叫强大                        InputStream,OutputStream    (这四个都是接口,要用的话得用子类
      字符流:主要用于处理文本文件.txt 比较少。        Reader , Writer                           前面加上File,例如FileInputerStream)
角色来分:
      节点流:直接以物理IO节点作为构造器参数。
      包装流(处理流):流的构造器参数不是一个物理节点,而是已经存在的流,就一定是处理流。使用处理流来包装节点流。
import java.io.*;
public class StringNodeTest
{
	public static void main(String[] args)
	{
		String src = "一串字符串,用\n换行\n";
		char[] buffer = new char[32];
		int hasRead = 0;
		try(
			StringReader sr = new StringReader(src))
		{
			// 采用循环读取的访问读取字符串
			while((hasRead = sr.read(buffer)) > 0)
			{
				System.out.print(new String(buffer ,0 , hasRead));
			}
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
		try(
			// 创建StringWriter时,实际上以一个StringBuffer作为输出节点
			// 下面指定的20就是StringBuffer的初始长度
			StringWriter sw = new StringWriter())
		{
			// 调用StringWriter的方法执行输出
			sw.write("写入string到StringWriter用\n换行");
			System.out.println("----下面是sw的字符串节点里的内容----");
			// 使用toString()方法返回StringWriter的字符串节点的内容
			System.out.println(sw.toString());
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}


import java.io.*;
public class KeyinTest
{
	public static void main(String[] args)
	{
		try(
			// 将Sytem.in对象转换成Reader对象
			InputStreamReader reader = new InputStreamReader(System.in);
			// 将普通Reader包装成BufferedReader
			BufferedReader br = new BufferedReader(reader)
		     )
		{
			String line = null;
			// 采用循环方式来一行一行的读取
			while ((line = br.readLine()) != null)
			{
				// 如果读取的字符串为"exit",程序退出
				if (line.equals("exit"))
				{
					System.exit(1);
				}
				// 打印读取的内容
				System.out.println("输入内容为:" + line);
			}
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}


import java.io.*;
public class PushbackTest
{
	public static void main(String[] args)
	{
		try(
			// 创建一个PushbackReader对象,指定推回缓冲区的长度为64
			PushbackReader pr = new PushbackReader(new FileReader("PushbackTest.java") , 64))
		{
			char[] buf = new char[32];
			// 用以保存上次读取的字符串内容
			String lastContent = "";
			int hasRead = 0;
			// 循环读取文件内容
			while ((hasRead = pr.read(buf)) > 0)
			{
				// 将读取的内容转换成字符串
				String content = new String(buf , 0 , hasRead);
				int targetIndex = 0;
				// 将上次读取的字符串和本次读取的字符串拼起来,
				// 查看是否包含目标字符串, 如果包含目标字符串
				if ((targetIndex = (lastContent + content).indexOf("new PushbackReader")) > 0)
				{
					// 将本次内容和上次内容一起推回缓冲区
					pr.unread((lastContent + content).toCharArray());
					// 重新定义一个长度为targetIndex的char数组
					if(targetIndex > 32)
					{
						buf = new char[targetIndex];
					}
					// 再次读取指定长度的内容(就是目标字符串之前的内容)
					pr.read(buf , 0 , targetIndex);
					// 打印读取的内容
					System.out.print(new String(buf , 0 ,targetIndex));
					System.exit(0);
				}
				else
				{
					// 打印上次读取的内容
					System.out.print(lastContent);
					// 将本次内容设为上次读取的内容
					lastContent = content;
				}
			}
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}



import java.io.*;
public class RedirectOut
{
	public static void main(String[] args)
	{
		try(
			// 一次性创建PrintStream输出流
			PrintStream ps = new PrintStream(new FileOutputStream("out.txt")))
		{
			// 将标准输出重定向到ps输出流
			System.setOut(ps);
			// 向标准输出输出一个字符串
			System.out.println("普通字符串");
			// 向标准输出输出一个对象
			System.out.println(new RedirectOut());
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}


import java.util.*;
import java.io.*;
public class RedirectIn
{
	public static void main(String[] args)
	{
		try(
			FileInputStream fis = new FileInputStream("RedirectIn.java"))
		{
			// 将标准输入重定向到fis输入流
			System.setIn(fis);//不重定向的话就是读取System.in  现在是读取上面的文件。
			// 使用System.in创建Scanner对象,用于获取标准输入
			Scanner sc = new Scanner(System.in);
			// 增加下面一行将只把回车作为分隔符
			sc.useDelimiter("\n");//否则空格的地方也被当一行了。
			// 判断是否还有下一个输入项
			while(sc.hasNext())
			{
				// 输出输入项
				System.out.println("键盘输入的内容是:" + sc.next());
			}
			sc.close();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}

猜你喜欢

转载自huadianfanxing.iteye.com/blog/2366931