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

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

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

举例说明1:

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. DataOutputStreamdos=null;
  4. try{
  5. dos=newDataOutputStream(newFileOutputStream("F:/price.txt"));
  6. dos.writeDouble(243.21);
  7. }catch(IOExceptione){
  8. e.printStackTrace();
  9. }finally{
  10. try{
  11. dos.close();
  12. }catch(IOExceptione2){
  13. e2.printStackTrace();
  14. }
  15. }
  16. }
  17. }

举例说明2:

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. DataInputStreamdis=null;
  4. try{
  5. dis=newDataInputStream(newFileInputStream("F:/price.txt"));
  6. System.out.println(dis.readDouble());
  7. }catch(IOExceptione){
  8. e.printStackTrace();
  9. }finally{
  10. try{
  11. dis.close();
  12. }catch(IOExceptione2){
  13. e2.printStackTrace();
  14. }
  15. }
  16. }
  17. }

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

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. BufferedReaderbr=null;
  4. try{
  5. br=newBufferedReader(newInputStreamReader(System.in));
  6. Stringline=null;
  7. while((line=br.readLine())!=null){
  8. System.out.println(line);
  9. }
  10. }catch(IOExceptione){
  11. e.printStackTrace();
  12. }
  13. }
  14. }

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

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. Processprocess=null;
  4. BufferedReaderbr=null;
  5. try{
  6. process=Runtime.getRuntime().exec("javac.exe");
  7. br=newBufferedReader(newInputStreamReader(
  8. process.getErrorStream()));
  9. Stringline=null;
  10. System.out.println("编译出错,错误信息如下~~~~~");
  11. while((line=br.readLine())!=null){
  12. System.out.println(line);
  13. }
  14. }catch(IOExceptione){
  15. e.printStackTrace();
  16. }finally{
  17. try{
  18. br.close();
  19. }catch(IOExceptione2){
  20. e2.printStackTrace();
  21. }
  22. }
  23. }
  24. }

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

Java代码 收藏代码
  1. publicclassTest{
  2. publicstaticvoidmain(String[]args)throwsIOException{
  3. RandomAccessFileraf=newRandomAccessFile("f:/1.txt","rw");
  4. byte[]buff=newbyte[1024];
  5. inthasRead=-1;
  6. while((hasRead=raf.read(buff))>0){
  7. System.out.println(newString(buff,0,hasRead));
  8. }
  9. raf.close();
  10. }
  11. }


举例说明2:

Java代码 收藏代码
  1. /**
  2. *@authorlhy
  3. *@description向文件中的指定位置插入内容
  4. */
  5. publicclassTest{
  6. publicstaticvoidmain(String[]args){
  7. RandomAccessFileraf=null;
  8. try{
  9. raf=newRandomAccessFile("f:/1.txt","rw");
  10. insertContent(raf,100,"Java面向对象之I/O流之RandomAccessFile的使用");
  11. }catch(IOExceptione){
  12. e.printStackTrace();
  13. }finally{
  14. try{
  15. raf.close();
  16. }catch(IOExceptione){
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. publicstaticvoidinsertContent(RandomAccessFileraf,intpos,
  22. Stringcontent){
  23. ByteArrayOutputStreambos=null;
  24. try{
  25. bos=newByteArrayOutputStream();
  26. raf.seek(pos);//把记录指针移到要插入的地方
  27. byte[]buff=newbyte[1024];
  28. inthasRead=-1;
  29. //将raf从pos位置开始、直到结尾所有的内容
  30. while((hasRead=raf.read(buff))>0){
  31. bos.write(buff,0,hasRead);//将读取的数据(从pos位置开始)放入bos中
  32. }
  33. raf.seek(pos);//再次将记录指针移到要插入的地方
  34. raf.write(content.getBytes());//写入要插入的内容
  35. raf.write(bos.toByteArray());//写入之前保存的内容
  36. }catch(IOExceptione){
  37. e.printStackTrace();
  38. }finally{
  39. try{
  40. bos.close();
  41. }catch(IOExceptione){
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }

结束语

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

你可能感兴趣的:(Java学习)