[javase]IO

一、流
java流分为两种,一种 字节流,另一种是 字符流,分别由四个抽象类表示:InputStream, OutputStream, Reader, Writer
注:在网络程序设计中到底是使用字符流还是字节流呢?
答案:有一个简单的判断方法:如果通讯的双方都是java语言编写,最好使用字符流或更高级的流类。如果通讯的另一端是未知的,最好使用字节流,并事先约定好通讯规则发送和接收数据

[javase]IO_第1张图片
1、输出流
[javase]IO_第2张图片
[javase]IO_第3张图片
[javase]IO_第4张图片
FileOutputStream使用的2种方法
//1.创建文件
		File file = new File("d:\\test.txt");
		FileOutputStream fos = new FileOutputStream(file);
//		FileOutputStream fos = new FileOutputStream("d:\\test.txt", true);
		
		//2.写入内容
		byte[] b = "xiaoming a".getBytes();
		fos.write(b);
		
		//3.销毁流
		fos.close();
ByteArrayOutputStream使用方法
public void byteArrayOutputStream() throws IOException {
		int a = 0;
		int b = 1;
		int c = 2;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		bout.write(a);
		bout.write(b);
		bout.write(c);
		byte[] buff = bout.toByteArray();
		for (int i = 0; i < buff.length; i++) {
			System.out.println(buff[i]);
		}
		
		System.out.println("***********************");
		
		ByteArrayInputStream bin = new ByteArrayInputStream(buff);
		while ((b = bin.read()) != -1) {
			System.out.println(b);
		}
	}

FilterOutputStream之BufferedOutputStream
@Test
	public void filterOutputTest() {
		
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			fos = new FileOutputStream("io.txt");
			//1 BufferedOutputStream
			//如果第二参数<=b.length+b1.length+b2.length或没有第二参数,缓存空间不够,会写入部分内容并bos.flush()
			//否则b,b1,b2内容会缓存再bos里,等待bos.close或bos.flush
			//如果bos.close()或者bos.flush(),会立即吸入io.txt
			bos = new BufferedOutputStream(fos,16);
			
			byte[] b = "小明".getBytes();
			byte[] b1 = "大红".getBytes();
			byte[] b2 = "小刚".getBytes();
			bos.write(b);
			bos.write(b1);
			bos.write(b2);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null) {
					bos.flush();
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

Writer之BufferedWriter
@Test
	public void bufferedWriterTest() throws IOException {
		
		Writer w = null;
		BufferedWriter bw = null;
		try {
			w = new FileWriter("io.txt");
			
			bw = new BufferedWriter(w);
			
			bw.write("sdsdafdasdfads");
			bw.newLine();
			bw.write("小明啊..............");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				bw.flush();
				bw.close();
			}
		}
		
	}

Writer之OutputStreamWriter
@Test
	public void outputStreamWriterTest() throws IOException {
		
		OutputStreamWriter osw = new OutputStreamWriter(System.err);
		BufferedWriter out
		   = new BufferedWriter(osw);
		
		out.append("hello world").append("! 你好").append(", world");
		
		System.out.println(osw.getEncoding());
		
		out.flush();
		
	}

1、输人流
[javase]IO_第5张图片
[javase]IO_第6张图片
[javase]IO_第7张图片
InputStreamReader 读/写文件
//读/写文件
	public void inputStreamReaderTest() throws Exception {
		
		InputStreamReader isr = new InputStreamReader(new FileInputStream("d://catalina.out.2016-04-12-20.06.out"));
		
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d://co.out"));
		
		int i;
		while ((i = isr.read()) != -1) {
			osw.write(i);
		}
		
		osw.close();
		isr.close();
	}


你可能感兴趣的:(java,IO)