1 importjava.io.BufferedWriter;2 importjava.io.File;3 importjava.io.FileWriter;4 importjava.io.IOException;5 importjava.text.SimpleDateFormat;6 importjava.util.Date;7 importjava.util.concurrent.atomic.AtomicReference;8
9
10
11 /**
12 *13 *@authorzhangshuang14 *15 */
16 public classSyncFile {17 private static String FILENAME ="";18
19 private static String FILEDIR="";20
21 private static final AtomicReference instance = new AtomicReference();22
23 private String currentDate =SyncFile.getCurrentDate();24
25 private File file = null;26
27 private FileWriter fw = null;28
29 private BufferedWriter bw = null;30
31 privateSyncFile(String dir, String name) {32 SyncFile.FILEDIR =dir;33 SyncFile.FILENAME =name ;34 while(dir.endsWith("/")) {35 dir = dir.substring(0, dir.length() - 1);36 }37 mkDir(dir);38 String filePath = dir + "/" + name + "." +currentDate ;39 file = newFile(filePath);40 try{41 if(!file . exists()) {42 file.createNewFile();43 }44 fw = new FileWriter(file, true);45 bw = newBufferedWriter(fw);46 } catch(Exception e) {47 e.printStackTrace();48 }49
50 }51
52 public staticSyncFile getInstance(String dir, String name) {53 if( instance .get() == null) {54 instance .compareAndSet(null, newSyncFile(dir, name));55 }56 returninstance.get();57 }58
59 public staticSyncFile getInstance() {60 if( instance .get() == null) {61 instance.compareAndSet(null, newSyncFile(FILEDIR, FILENAME));62 }63 returninstance.get();64 }65
66 public synchronized voidwrite (String line) {67 openBuffer() ;68
69 line = line.endsWith("\n") ? line : line + "\n";70 try{71 if(line != null && line.trim() != ""){72 bw.write(line);73 bw.flush();74 }75 } catch(IOException e) {76 e.printStackTrace();77 }78 }79
80 private static voidmkDir( String dir) {81 File file = newFile(dir);82 mkDir(file);83 }84
85 /**
86 *
递归创建不存在的目录87 *@paramfile88 */
89 private static voidmkDir(File file){90 if(file.getParentFile().exists()){91 file.mkdir();92 }else{93 mkDir(file.getParentFile());94 file.mkdir();95 }96 }97
98 public staticString getCurrentDate() {99 return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(newDate());100 }101
102 private voidopenBuffer() {103
104 if(!currentDate.equals(SyncFile.getCurrentDate()) || fw == null ||bw == null) {105 if (!currentDate.equals(SyncFile.getCurrentDate())){106 currentDate =SyncFile.getCurrentDate();107 file = new File(FILEDIR + "/" + FILENAME + "." +currentDate);108 }109 close ();110 try{111 fw = new FileWriter(file , true);112 bw = newBufferedWriter(fw);113 } catch(IOException e) {114 e.printStackTrace();115 }116 }117
118 }119
120 public voidclose() {121 try{122 if (bw != null) {123 bw.close();124 bw = null;125 }126
127 if (fw != null) {128 fw.close();129 fw = null;130 }131 } catch(IOException e) {132 e.printStackTrace();133 }134 }135
136 public static voidmain(String[] args) {137 //String a = "/data/logs/";138 //while (a.endsWith("/")) {139 //a = a.substring(0 , a.length()-1);140 //System.out.println(a);141 //}142 //System.out.println("ok" + a);
143 mkDir("c:/2");144 }145
146
147 }