[关闭]
@liruiyi962464 2017-03-23T03:53:28.000000Z 字数 2432 阅读 427

IO InputStream、OutputStream:字节流读写类

java

FileInputStream:字节输入流

  1. public static void main(String[] args) throws IOException {
  2. FileInputStream fis = new FileInputStream(new File("E:/a/a.txt"));
  3. /*
  4. * 1.使用前导入相应的包
  5. * 2.使用时要处理异常
  6. * 3.使用后要关闭流 fis.close()
  7. * */
  8. //读取文件内容
  9. //read()返回一个读到的字节
  10. System.out.println(fis.read());
  11. System.out.println(fis.read());
  12. //关闭流
  13. fis.close();
  14. }

FileOutputStream:字节输出流

  1. public static void main(String[] args) throws IOException {
  2. FileOutputStream fos =
  3. new FileOutputStream(new File("E:/a/a.txt"),true);//追加
  4. //输出文件内容
  5. fos.write(97);
  6. fos.write(98);
  7. fos.close();//关闭流
  8. }

字节输入输出

基础使用

  1. public static void main(String[] args) throws IOException {
  2. // 字节输入流
  3. FileInputStream fis = new FileInputStream("E:/a/a.txt");
  4. // 字节输出流
  5. FileOutputStream fos = new FileOutputStream("E:/a/b.txt");
  6. int a = -1;
  7. while ((a = fis.read()) != -1) {
  8. fos.write(a);
  9. }
  10. fis.close();
  11. fos.close();
  12. }

自定义缓冲区

  1. public static void main(String[] args) throws IOException {
  2. // 字节输入流
  3. FileInputStream fis = new FileInputStream("E:/JavaTaskIO/guiyangtu.mp3");
  4. // 字节输出流
  5. FileOutputStream fos = new FileOutputStream("E:/JavaTaskIO/贵阳图.mp3");
  6. byte[] b = new byte[1024 * 2];// 创建篮子
  7. int length = 0;
  8. long time1 = System.currentTimeMillis();
  9. while ((length = fis.read(b)) != -1) {
  10. fos.write(b,0,length);
  11. }
  12. long time2 = System.currentTimeMillis();
  13. System.out.println(time2-time1);
  14. fis.close();
  15. fos.close();
  16. }

系统缓冲区

  1. public class Test5_BufferdIOStream {
  2. public static void main(String[] args) throws Exception {
  3. //字节输入流
  4. FileInputStream fis = new FileInputStream("E:/JavaTaskIO/guiyangtu.mp3");
  5. //字节输出流
  6. FileOutputStream fos = new FileOutputStream("E:/JavaTaskIO/贵阳图.mp3");
  7. //创建一个带缓冲区的字节输入流
  8. BufferedInputStream bis = new BufferedInputStream(fis);
  9. //创建一个带缓冲区的字节输出流 修改缓存区大小
  10. BufferedOutputStream bos = new BufferedOutputStream(fos,1024*200);
  11. long time1 = System.currentTimeMillis();
  12. int length = -1;
  13. while((length = bis.read())!=-1){
  14. bos.write(length);
  15. }
  16. long time2 = System.currentTimeMillis();
  17. System.out.println(time2-time1);
  18. //关闭流
  19. fis.close();
  20. bos.close();
  21. bis.close();
  22. bos.close();
  23. }
  24. }

DataInputStream && DataOutputStream:数据输入输出流

  • 对对象的持久化存储,就是将自定的对象永久存储
  • 这个过程叫序列化,在读出来,叫反序列化
  1. public class Tset6_DataIOStream {
  2. public static void main(String[] args) throws Exception {
  3. FileOutputStream fos = new FileOutputStream("E:/JavaTaskIO/abc.txt");
  4. DataOutputStream dos = new DataOutputStream(fos);
  5. dos.writeInt(1234);
  6. dos.writeUTF("李四");
  7. dos.writeDouble(23.9);
  8. FileInputStream fis = new FileInputStream("E:/JavaTaskIO/abc.txt");
  9. DataInputStream dis = new DataInputStream(fis);
  10. //读的顺序跟写的顺序一样
  11. System.out.println(dis.readInt());
  12. System.out.println(dis.readUTF());
  13. System.out.println(dis.readDouble());
  14. fos.close();
  15. dos.close();
  16. fis.close();
  17. dis.close();
  18. }
  19. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注