windows下一个进程写文件ok,命令行下同时运行两个进程,总会丢失几条!!
代码如下:import java.io.BufferedWriter;
import java.io.PrintWriter;import java.io.FileWriter;public class WriteFile {
public static void main(String[] args) {
long times = 3000;
for (int i = 0; i < times; i++) {
//write1(Integer.toString(i+1));
write2(Integer.toString(i+1));
}
System.out.println("finished!");
} private static void write1(String content) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d:\\1.txt", true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.flush();
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
private static synchronized void write2(String content) {
FileWriter fw = null;
PrintWriter pw = null;
try {
fw = new FileWriter("d:\\2.txt", true);
pw = new PrintWriter(fw);
pw.println(content);
pw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
代码如下:import java.io.BufferedWriter;
import java.io.PrintWriter;import java.io.FileWriter;public class WriteFile {
public static void main(String[] args) {
long times = 3000;
for (int i = 0; i < times; i++) {
//write1(Integer.toString(i+1));
write2(Integer.toString(i+1));
}
System.out.println("finished!");
} private static void write1(String content) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d:\\1.txt", true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.flush();
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
private static synchronized void write2(String content) {
FileWriter fw = null;
PrintWriter pw = null;
try {
fw = new FileWriter("d:\\2.txt", true);
pw = new PrintWriter(fw);
pw.println(content);
pw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
解决方案 »
- 生成了一个.jar文件,现在要写一个批处理文件xx.bat使得在任何电脑里点击xx.bat便可以运行此jar文件,怎么写好?
- 哪位帮解释一下java中的volatile关键字
- soap消息中的命名空间问题
- 如果保证输入法的待选字窗口总是显示?(散分,200)
- 能运行就给分
- 本机上用socket(tcp/ip)做了个两个客户端和服务器端控制台程序,客户和服务器都有收发线程,我的这两个程序一运行cpu就站了100%,各位高手
- 一个简单的问题
- ASCII为0的是什么字符?
- 为什么用javamail连接邮箱出现NoSuchProviderException?怎么没有provider呢?求救
- 传递基本数据类型参数
- 关于守护线程的问题(来源于scjp)
- 帮忙看下,这段代码的错误在哪里?
这里不存在多线程问题,同步不起作用。两个不同的进程是独立的,各自运行于一个独立的jvm中。两个进程同时写一个文件,结果应该依赖于操作系统的文件处理方式。
java下面好像没有对进程同步化的控制.
要么你对文件进行加锁,每个线程写的时候加锁,写完解锁.
通过文件通道和文件锁方式解决,代码如下。谢谢各位!import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;import java.io.FileWriter;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;public class WriteFile {
public static void main(String[] args) throws IOException {
long times = 3000;
for (int i = 0; i < times; i++) {
// write1(Integer.toString(i+1));
// write2(Integer.toString(i + 1));
write3(Integer.toString(i + 1));
}
System.out.println("finished!");
} private static void write1(String content) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d:\\1.txt", true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.flush();
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
} private static synchronized void write2(String content) {
FileWriter fw = null;
PrintWriter pw = null;
try {
fw = new FileWriter("d:\\2.txt", true);
pw = new PrintWriter(fw);
pw.println(content);
pw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
} private static void write3(String content) throws IOException {
String fileName = "d:\\3.txt";
RandomAccessFile raFile = null;
FileChannel channel = null;
FileLock fileLock = null;
try {
raFile = new RandomAccessFile(fileName, "rw");
channel = raFile.getChannel();
fileLock = channel.lock();
raFile.seek(raFile.length());
raFile.write((content + System.getProperty("line.separator"))
.getBytes());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != fileLock) {
fileLock.release();
}
if (null != channel) {
channel.close();
}
}
}
}