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

  • 相关阅读:
    Sudoku POJ 2676 [dfs]
    a>b和(*a).b
    lowbit()操作
    Anniversary party HDU 1520树形dp
    Lifting the Stone HDU 1115 求多边形的重心
    Boolean Expressions POJ 2106 【递归】
    Shaolin HDU 4585 STL map||Treap树
    取石子游戏 HDU 1527 威佐夫游戏
    A Simple Problem with Integers POJ 3468 区间修改线段树lazy—tag大法
    社会性网络软件SNS 帮助你认识比尔盖茨 java程序员
  • 原文地址:https://www.cnblogs.com/lexus/p/2391373.html
Copyright © 2011-2022 走看看