zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    弱监督学习框架下的图像语义分割调研
    LeetCode(115):不同的子序列
    LeetCode(114): 二叉树展开为链表
    LeetCode(113):路径总和 II
    项目实战10.1—企业级自动化运维工具应用实战-ansible
    快收藏!高手Linux运维管理必备工具大全,你会吗?
    项目实战12.2—企业级监控工具应用实战-zabbix操作进阶
    项目实战12.1—企业级监控工具应用实战-zabbix安装与基础操作
    项目实战13—企业级虚拟化Virtualization
    计算机专用英语词汇1695个词汇表
  • 原文地址:https://www.cnblogs.com/lexus/p/2391373.html
Copyright © 2011-2022 走看看