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

  • 相关阅读:
    Luogu 4206 [NOI2005]聪聪与可可
    【Luogu】P3708Koishi的数字游戏(数论)
    【Luogu】P1850换教室(期望DP)
    【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
    【Luogu】P3865ST表模板(ST表)
    【Luogu】P3376网络最大流模板(Dinic)
    【Luogu】P1005矩阵取数游戏(高精度+DP)
    【Luogu】P2324骑士精神(IDA*)
    【Luogu】P3052摩天大楼里的奶牛(遗传算法乱搞)
    洛森地图半成品
  • 原文地址:https://www.cnblogs.com/lexus/p/2391373.html
Copyright © 2011-2022 走看看