thieumao

Ghi tiếp File

May 12th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13. public class GhiTiepFile {
  14.     public static void main(String[] args) {
  15.         String duonglink = "/home/thieumao/Desktop/GhiTiepFileDo.txt";
  16.         String themvao = "Noi dung can ghi";
  17.         BufferedReader br = null;
  18.         BufferedWriter bw = null;
  19.         try {
  20.             br = new BufferedReader(new InputStreamReader(new FileInputStream(
  21.                     duonglink)));
  22.             String line = "";
  23.             String newLine = "";
  24.             while ((line = br.readLine()) != null) {
  25.                 newLine = newLine + line + "\n";
  26.             }
  27.             bw = new BufferedWriter(new FileWriter(duonglink));
  28.             newLine = newLine + themvao;
  29.             bw.write(newLine);
  30.             bw.flush();
  31.         } catch (FileNotFoundException ex) {
  32.             JOptionPane.showMessageDialog(null, "Duong dan bi sai");
  33.             System.exit(0);
  34.         } catch (IOException ex) {
  35.             Logger.getLogger(GhiTiepFile.class.getName()).log(Level.SEVERE,
  36.                     null, ex);
  37.         } finally {
  38.             try {
  39.                 bw.close();
  40.                 br.close();
  41.             } catch (IOException ex) {
  42.                 Logger.getLogger(GhiTiepFile.class.getName()).log(Level.SEVERE,
  43.                         null, ex);
  44.             }
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment