JAVA IO

字节流

1. FileInputStream  FileOutputStream通过字节流来读写文件

[java] view plaincopy

  1. public static void main(String[] args) throws Exception {  
  2. //将文件里写数据
  3.     File f = new File("d:\\dmeo.txt");  
  4.     FileOutputStream output = new FileOutputStream(f);  
  5.     String s = "I Am Learning Java , 我在学习Java";  
  6. byte[] b = s.getBytes(); //将String变为byte数组
  7. //output.write(b);write可以接收byte数组
  8. for (int i = 0; i < b.length; i++) {  
  9.         output.write(b[i]);  
  10.     }  
  11.     output.flush();  
  12.     output.close();  
  13. //读取文件里数据
  14.     FileInputStream input = new FileInputStream(f);  
  15. //开辟一个文件大小的数组空间
  16. byte[] in = new byte[(int) f.length()];  
  17. //将读取来的字节放入数组中
  18. for (int i = 0; i < in.length; i++) {  
  19.         in[i] = (byte) input.read();   
  20.     }  
  21.     System.out.print(new String(in));  
  22.     input.close();  

2. BufferedInputStream BufferedOutPutStream 带有缓存的读写字节流

[java] view plaincopy

  1. public static void main(String[] args) throws Exception {  
  2. //将文件里写数据
  3. File f = new File("d:\\dmeo.txt");  
  4. BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(f));  
  5. String s = "I Am Learning Java , 我在学习Java";  
  6. byte[] b = s.getBytes();  
  7. output.write(b); //默认会有512字节的缓存,当缓存存满时一次性向文件中写入
  8. output.flush();   
  9. output.close();  
  10. BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));  
  11. byte[] in = new byte[(int)f.length()];  
  12. for (int i = 0; i < in.length; i++) {  
  13.         in[i] = (byte)input.read();  
  14.     }  
  15. System.out.println(new String(in));  
  16. input.close();  

2. DataInputStream DataOutputStream 可以读写基本数据类型的字节流

[java] view plaincopy

  1. import java.io.*;  
  2. //一个简单的人员类,只有姓名和年龄
  3. public class Person {  
  4. private String name;  
  5. private int age;  
  6. public Person(String name ,int age){  
  7. this.name = name;  
  8. this.age = age;  
  9.     }  
  10. public String getName(){  
  11. return this.name;  
  12.     }  
  13. public int getAge() {  
  14. return this.age;  
  15.     }  
  16. public static void main(String[] args) {  
  17. //构造对象数组
  18.         Person[] p1 = {new Person("Ryan",20),new Person("Tom",30),new Person("Jerry",15)};  
  19.         File f = new File("d:\\demo\\demo.txt");  
  20. try {  
  21.             DataOutputStream output =  new DataOutputStream(new FileOutputStream(f));  
  22. //写入数据
  23. for (int i = 0; i < p1.length; i++) {  
  24.                 output.writeUTF(p1[i].getName()); //以UTF编码写入姓名
  25.                 output.writeInt(p1[i].getAge()); //以Int型写入年龄
  26.             }  
  27.             output.flush();  
  28.             output.close();  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }     
  32.         Person[] p2 = new Person[p1.length];  
  33. try {  
  34. //读出数据
  35.             DataInputStream input = new DataInputStream(new FileInputStream(f));  
  36. for (int i = 0; i < p1.length; i++) {  
  37.                 String name =  input.readUTF(); //读出姓名
  38. int age  = input.readInt(); //读出年龄
  39. //重新构造person对象
  40.                 p2[i] = new Person(name,age);             
  41.             }  
  42.                 input.close();  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.         }  
  46. //检查是否还原正确
  47. for (int i = 0; i < p2.length; i++) {  
  48.             System.out.println("姓名:" + p2[i].getName() + " ;年龄:" + p2[i].getAge());  
  49.         }  
  50.     }  
  51. }<strong>  
  52. </strong> 

2. PrintStream  将内存中的数据经过相应的转换后再输出

[java] view plaincopy

  1. public static void main(String[] args){  
  2.     File f = new File("d:\\dmeo.txt");  
  3.     String studentName = "TOM";  
  4. int age = 20;  
  5. double totalScore = 600.0f;  
  6.     PrintStream ps = null;  
  7. try {  
  8.         ps = new PrintStream(new FileOutputStream(f));  
  9.     } catch (FileNotFoundException e) {  
  10.         e.printStackTrace();  
  11.     }  
  12. //格式化后输出
  13.     ps.printf("姓名:%s , 年龄:%d , 总分:%3.1f",studentName,age,totalScore);  
posted @ 2014-06-04 15:30  Liping的个人博客  阅读(140)  评论(0编辑  收藏  举报