来源:互联网 发布:编程实现快速排序算法 编辑:程序博客网 时间:2024/05/17 03:27
流的分类: 


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


  例1:创建一个文本文档


      File a = new File("e:\\123.txt");
      try {
        a.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();
    }
0 0
原创粉丝点击