thieumao

Xử lý file đơn giản

May 8th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package xulyfile;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12.  
  13. /**
  14.  *
  15.  * @author Thieu Mao
  16.  */
  17. public class XuLyFile {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) {
  23.         XuLyFile xlf = new XuLyFile();
  24.     }
  25.  
  26.     public XuLyFile() {
  27.         String strLeft, strRight, strCenter;
  28.         strLeft = "<string1>";
  29.         strRight = "<string2>";
  30.         String[] strData = new String[100];
  31.         int i = 0;
  32.         File f = new File("vanban.txt");
  33.         try {
  34.             FileReader fr = new FileReader(f);
  35.             BufferedReader br = new BufferedReader(fr);
  36.             while ((strCenter = br.readLine()) != null) {
  37.                 strCenter = strLeft + " " + strCenter + " " + strRight;
  38.                 strData[i] = strCenter;
  39.                 i++;
  40.             }
  41.             br.close();
  42.             fr.close();
  43.         } catch (Exception ex) {
  44.             System.out.println("Loi doc file!");
  45.         }
  46.  
  47.         f = new File("vanban.txt");
  48.         try {
  49.             FileWriter fw = new FileWriter(f);
  50.             BufferedWriter bw = new BufferedWriter(fw);
  51.             for (int j = 0; j < i; j++) {
  52.                 System.out.println(strData[j]);
  53.                 bw.write(strData[j] + "\n");
  54.             }
  55.             bw.close();
  56.             fw.close();
  57.         } catch (Exception e) {
  58.             System.out.println("Loi ghi file!");
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment