Java学习系列(十一)Java面向对象之I/O流(下)

今天接着昨天的IO流讲,内容可能会比较多。

DataInputStream与DataOutputStream
它们是建立在已有的IO的基础上的两个特殊的过滤流。规律:它们只是增加了一些特定的方法读取特定的数据。

举例说明1:

public class Test {
	public static void main(String[] args) {
		DataOutputStream dos = null;
		try {
			dos = new DataOutputStream(new FileOutputStream("F:/price.txt"));
			dos.writeDouble(243.21);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				dos.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}
}
举例说明2:
public class Test {
	public static void main(String[] args) {
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new FileInputStream("F:/price.txt"));
			System.out.println(dis.readDouble());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				dis.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}
}


节点流(System.in是读取键盘输入,可以换成new FileInputStream("f:/1.txt")读文件,也可以换成读网络(Socket)--以后会讲)包装成过滤流编程:

public class Test {
	public static void main(String[] args) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


Java虚拟机读取其他进程的数据
   Java如何启动其他进程:Runtime实例.exec(String command)
举例说明:

public class Test {
	public static void main(String[] args) {
		Process process = null;
		BufferedReader br = null;
		try {
			process = Runtime.getRuntime().exec("javac.exe");
			br = new BufferedReader(new InputStreamReader(
					process.getErrorStream()));
			String line = null;
			System.out.println("编译出错,错误信息如下~~~~~");
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}
}


RandomAccessFile ---随机(任意)访问文件。  --创建时需要指定r/w模式。
Random --想访问文件的哪个点,就访问文件的哪个点(任意)。
特征:既可读、又可写、还可追加,不会覆盖原有文件内容。但它只能访问文件。
举例说明1:

public class Test {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("f:/1.txt", "rw");
		byte[] buff = new byte[1024];
		int hasRead = -1;
		while ((hasRead = raf.read(buff)) > 0) {
			System.out.println(new String(buff, 0, hasRead));
		}
		raf.close();
	}
}

举例说明2:

/**
 * @author lhy
 * @description 向文件中的指定位置插入内容
 */
public class Test {
	public static void main(String[] args) {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile("f:/1.txt", "rw");
			insertContent(raf, 100, "Java面向对象之I/O流之RandomAccessFile的使用");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void insertContent(RandomAccessFile raf, int pos,
			String content) {
		ByteArrayOutputStream bos = null;
		try {
			bos = new ByteArrayOutputStream();
			raf.seek(pos);// 把记录指针移到要插入的地方
			byte[] buff = new byte[1024];
			int hasRead = -1;
			// 将raf从pos位置开始、直到结尾所有的内容
			while ((hasRead = raf.read(buff)) > 0) {
				bos.write(buff, 0, hasRead);// 将读取的数据(从pos位置开始)放入bos中
			}
			raf.seek(pos);// 再次将记录指针移到要插入的地方
			raf.write(content.getBytes()); // 写入要插入的内容
			raf.write(bos.toByteArray()); // 写入之前保存的内容

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

结束语

       有关Java中的IO流类比较多,而且方法大同小异,大家有空可以多查查API文档。今天就讲到这,明天开始讲Java面向对象之序列化机制及版本。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值