50,549
社区成员
发帖
与我相关
我的任务
分享
package com.dragon.util;
public class WriteLogThread implements Runnable {
private String fileName;
private int index;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public void run() {
for (int i = 0; i < 100; i ++) {
try {
WriteLogTool tool = WriteLogTool.instance;
tool.writeToFileB(fileName, "[id" + this.index + "][name" + this.index + "][" + "value" + this.index + "]" +
"[id" + this.index + "][name" + this.index + "][" + "value" + this.index + "]\n" );
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.dragon.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
public enum WriteLogTool {
instance();
public void writeToFileA(String fileName, String record) throws IOException, InterruptedException {
//RandomAccessFile randomAccessFile = new RandomAccessFile(new File(fileName), "rws");
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(fileName), "rw");
long fileLength = randomAccessFile.length();
randomAccessFile.seek(fileLength);
randomAccessFile.writeBytes(record + "\n");
randomAccessFile.close();
}
public void writeToFileB(String fileName, String record) throws IOException {
FileWriter writer = new FileWriter(fileName, true);
writer.write(record);
writer.close();
}
public static String logToString(Object object) {
String result = null;
return result;
}
public static void main(String[] args) {
String fileName = "D:\\1.txt";
File file = new File(fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 100; i++) {
WriteLogThread thread = new WriteLogThread();
thread.setFileName(fileName);
thread.setIndex(i);
thread.run();
}
}
}