• buffer writer


    Write to file using a BufferedWriter - A Java Code Example

        import java.io.BufferedWriter;
        import java.io.FileNotFoundException;
        import java.io.FileWriter;
        import java.io.IOException;

        /**
         *
         * @author javadb.com
         */
        public class Main {
            
            /**
             * Prints some data to a file using a BufferedWriter
             */
            public void writeToFile(String filename) {
                
                BufferedWriter bufferedWriter = null;
                
                try {
                    
                    //Construct the BufferedWriter object
                    bufferedWriter = new BufferedWriter(new FileWriter(filename));
                    
                    //Start writing to the output stream
                    bufferedWriter.write("Writing line one to file");
                    bufferedWriter.newLine();
                    bufferedWriter.write("Writing line two to file");
                    
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    //Close the BufferedWriter
                    try {
                        if (bufferedWriter != null) {
                            bufferedWriter.flush();
                            bufferedWriter.close();
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                new Main().writeToFile("myFile.txt");
            }
        }

            



        The output to the file will look like this when you run the code:

        Writing line one to file
        Writing line two to file

  • 相关阅读:
    html2pdf后逐页固定位置盖公章
    c#Stream学习笔记
    Flume -- 开源分布式日志收集系统
    Sqoop -- 用于Hadoop与关系数据库间数据导入导出工作的工具
    Hive -- 基于Hadoop的数据仓库分析工具
    HBase -- 基于HDFS的开源分布式NoSQL数据库
    ZooKeeper -- 分布式开源协调服务
    Hadoop学习(4)-- MapReduce
    Hadoop学习(3)-- 安装1.x版本
    Hadoop学习(2)-- HDFS
  • 原文地址:https://www.cnblogs.com/lexus/p/2391373.html
Copyright © 2020-2023  润新知