字节数组流,文件流,数据流,转换流

public class Demo011 {
	public static void main(String[] args) {
		Demo011 de = new Demo011();
		de.byteArrayInputStream();// 字节数组输入流
		de.fileInputStream();// 文件输入流
		de.fileInputStream2();// 文件输入流,读进数组
		de.fileOutputStream();// 文件输出流
		de.copy();// 文件复制
		de.dataOutputStream();// 数据输出流(用数据流写操作)
		de.dataInputStream();// 数据输入流(用数据流读操作)
		de.dataOutputStream2();// 数据输出流(用带缓冲区的输出流写操作)
		de.dataInputStream2();// 数据输入流(用带缓冲区的输入流读操作)
		de.printStream();

	}

	private void printStream() {
		// TODO Auto-generated method stub
		try {
			PrintStream ps = new PrintStream("c:\\hello.txt");
			ps.write("sdfa".getBytes());
			ps.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void dataOutputStream2() {
		// TODO Auto-generated method stub
		try {
			FileOutputStream fos = new FileOutputStream("c:\\hello.txt");
			BufferedOutputStream bs = new BufferedOutputStream(fos);
			bs.write("xiedu".getBytes());
			fos.close();
			bs.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void dataInputStream2() {
		// TODO Auto-generated method stub
		try {
			FileInputStream fis = new FileInputStream("c:\\hello.txt");
			StringBuffer str = new StringBuffer();
			BufferedInputStream bs = new BufferedInputStream(fis);// 带缓冲区的输入流
			InputStreamReader isr = new InputStreamReader(bs);// 转换为字符流
			int i = isr.read();
			while (i != -1) {
				str.append((char) i);// 转换为字符遍历输出
				i = isr.read();
			}
			isr.close();
			bs.close();
			fis.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void dataInputStream() {
		// TODO Auto-generated method stub
		try {
			FileInputStream fis = new FileInputStream("c:\\hello.txt");
			DataInputStream dos = new DataInputStream(fis);
			String str = dos.readUTF();
			System.out.println(str);
			fis.close();
			dos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void dataOutputStream() {
		// TODO Auto-generated method stub
		try {
			FileOutputStream fis = new FileOutputStream("c:\\hello.txt");
			DataOutputStream dis = new DataOutputStream(fis);
			dis.writeUTF("写入");
			dis.close();
			fis.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void copy() {
		try {
			FileInputStream fis = new FileInputStream("c:\\hello.txt");
			FileOutputStream fos = new FileOutputStream("c:\\hello1.txt");
			int i = fis.read();
			while (i != -1) {
				fos.write(i);
				i = fis.read();
			}
			fos.close();
			fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void fileOutputStream() {
		// TODO Auto-generated method stub
		String str = "写入内容";
		try {
			OutputStream os = new FileOutputStream("c:\\hello.txt");
			os.write(str.getBytes());// 转换为字节输入
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void fileInputStream2() {
		// TODO Auto-generated method stub
		try {
			InputStream is = new FileInputStream("c:\\hello.txt");
			byte[] by = new byte[1024];
			is.read(by);// 读进字节数组
			String str = new String(by);// 输出字节数组
			is.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void fileInputStream() {
		// TODO Auto-generated method stub
		try {
			InputStream is = new FileInputStream("c:\\hello.txt");
			InputStreamReader isr = new InputStreamReader(is, "GBK");// 转换流
			int i = isr.read();
			while (i != -1) {
				System.out.println((char) i);// 转换为字符
				i = isr.read();
			}
			isr.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void byteArrayInputStream() {
		// TODO Auto-generated method stub
		byte[] by = new byte[] { 5, 12, 1, 56, 47, 8, 96, 64 };
		ByteArrayInputStream bais = new ByteArrayInputStream(by);
		int i = bais.read();
		while (i != -1) {
			System.out.print(i);
			i = bais.read();
		}
		try {
			bais.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(Java)