Java学习系列(十一)Java面向对象之I/O流(下)
来源:互联网 发布:ubuntu和debian哪个强 编辑:程序博客网 时间:2024/05/16 09:07
今天接着昨天的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面向对象之序列化机制及版本。
0 0
- Java学习系列(十一)Java面向对象之I/O流(下)
- Java学习系列(十一)Java面向对象之I/O流(下)
- Java学习系列(十)Java面向对象之I/O流(上)
- Java学习系列(十)Java面向对象之I/O流(上)
- Java学习系列(二十一)Java面向对象之注解详解
- Java I/O 技术(十一)—— I/O 功能流对象(二)
- java学习之I/O流
- java学习之i/o
- Java学习系列(八)Java面向对象之集合框架详解(下)
- Java学习系列(十五)Java面向对象之细谈线程、线程通信(下)
- Java学习系列(八)Java面向对象之集合框架详解(下)
- Java学习系列(十五)Java面向对象之细谈线程、线程通信(下)
- 《Java面向对象编程》学习笔记16—Java I/O系统
- 《Java面向对象编程》学习笔记16—Java I/O系统
- 《Java面向对象编程》学习笔记16—Java I/O系统
- Java总结系列之输入输出I/O
- Java学习系列(十三)Java面向对象之界面编程
- Java学习系列(十七)Java面向对象之开发聊天工具
- Android /java获取系统广播地址、ip地址、子网掩码的方法
- android性能优化之数据库优化一
- jquery获取radio的值
- 2014-4-25-return的用法
- Android中Bitmap,byte[],Drawable相互转化
- Java学习系列(十一)Java面向对象之I/O流(下)
- HDOJ-1014 Uniform Generator
- 在jquery通过ajax发送异步请求
- 三十五、字符集到底是怎么回事呀
- android 线程
- Python3.x和Python2.x的区别
- MINA源码分析1-服务端启动和客户端调用
- IO缓冲,php中的ob缓冲机制,以及socket、curl、fopen这些函数的底层关系
- 多线程间的数据共享的几种实现方式比较