码迷,mamicode.com
首页 > 编程语言 > 详细

Java 流

时间:2017-04-16 14:55:13      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:leo   读取文件   read   写入内容   except   file   print   stack   文件   

  ⑴流的分类: 

    ①输入流和输出流都是以程序作为主导;
    ②程序读取文件中的内容叫输入流;
    ③程序往文件中写入内容叫输出。

  例1:创建一个文本文档

      File f = new File("e:\\123.txt");
      try {
        f.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }

  例2:把内容输入到文本文档中

    OutputStream ou = null;
    try {
      ou = new FileOutputStream("e:\\123.txt");
      String aa = "哈哈....彩票中奖了";
      ou.write(aa.getBytes());
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }

  例3:查看文本文档中的内容

    try {
      InputStream iS = new FileInputStream("e:\\123.txt");
      byte [] ff =new byte[1024];
      int len = 0;
      while((len = iS.read(ff)) >=0){
      String str = new String(ff, 0, len);
      System.out.println(str);
    }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

  例4:创建文件夹

    File f = new File("e:\\hh");
    f.mkdir();

 

  例5:把文本文档复制到文件夹中
    InputStream iss = null;
    OutputStream ops = null;
    try {
    iss = new FileInputStream("e:\\123.txt");
    ops = new FileOutputStream("e:\\hh\\123.txt");
    byte [] hh = new byte [1024];
    int len = 0;
    while((len = iss.read(hh)) >=0){
        ops.write(hh);
    }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

 

Java 流

标签:leo   读取文件   read   写入内容   except   file   print   stack   文件   

原文地址:http://www.cnblogs.com/zxb555/p/6718745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!