zoukankan      html  css  js  c++  java
  • 利用缓冲流读取跟写入

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    /**
     * 利用输入流从本地读入一个文件,写入到hello.tex中(输出流),利用缓冲流
     */
    public class IoClassDemo_3 {
     
     public static void main(String[] args) {
      File file = new File("d/nihao.txt");
      File saveFile = new File("hello.txt");
      getReadFileToRoot(file,saveFile);
     }
     
     /**
      * @date  2016-11-08
      * @param file
      * @desc  从本地读入一个文件到程序根目录下
      * 由于是文本文件,用字符流
      */
     public static void getReadFileToRoot(File file,File saveFile){
      //创建一个字符输出缓冲流(主要是加速文件的读取)
      BufferedReader br = null;
      //创建一个字符输出缓冲流(加速输出作用)
      BufferedWriter bw = null;
      try {
       //创建一个字符输入流
       FileReader fr = new FileReader(file);
       br = new BufferedReader(fr);
       //创建一个字符输出流
       FileWriter fw = new FileWriter(saveFile);
       bw = new BufferedWriter(fw);
       char[] ch = new char[1024];
       int len ;
       while ((len = br.read(ch))!=-1) {
        //bw.write(br.readLine()); 
        bw.write(ch, 0, len);
        bw.flush();
       }
      }catch (IOException e) {
       e.printStackTrace();
      }finally{
       //关闭流   因为在关闭中,缓冲流属于处理流,而字符流属于节点流,所以在关闭中只要关闭缓冲流,节点流会自动关闭
       if(bw !=null){
        try {
         bw.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
       if(br != null){
        try {
         br.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
     }
    }

  • 相关阅读:
    SQL关键字的执行顺序
    StructuredStreaming基础操作和窗口操作
    StructuredStreaming简单的例子(NewAPI)
    StructuredStreaming(New)
    StructuredStreaming编程模型
    SparkStreaming简单例子(oldAPI)
    SparkStreaming架构
    Storm与SparkStreaming对比
    SparkStreaming-DStream(Discretized Stream)
    史上最全的java随机数生成算法分享(转)
  • 原文地址:https://www.cnblogs.com/wanglu1991/p/6042513.html
Copyright © 2011-2022 走看看