我想要一个类,来自不同线程的实例将从同一个文件中写入或读取。下面几乎是写操作,但我得到一个java.nio.file.FileSystemException
. 我正在使用 2 个实例作为微不足道的多线程访问,但我无法使其工作
try {
fileChannel = AsynchronousFileChannel.open(Paths.get("Filename.txt"),
StandardOpenOption.READ,
StandardOpenOption.WRITE);
} catch (IOException e) {
e.printStackTrace();
}
Future<Integer> writeFuture =
fileChannel.write(ByteBuffer.wrap(obj.toString().getBytes()), position);
try {
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
编辑:
堆栈跟踪:
java.nio.file.FileSystemException: C:\Documents and Settings\Administrator\workspace\TileMap\FileMap.txt:该进程无法访问该文件,因为它正被另一个进程使用。
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:90)
at sun.nio.fs.WindowsChannelFactory.newAsynchronousFileChannel(WindowsChannelFactory.java:199)
at sun.nio.fs.WindowsFileSystemProvider.newAsynchronousFileChannel(WindowsFileSystemProvider.java:138)
at java.nio.channels.AsynchronousFileChannel.open(AsynchronousFileChannel.java:248)
at java.nio.channels.AsynchronousFileChannel.open(AsynchronousFileChannel.java:300)
at slick.FileMap.updateFiguredMap(FileMap.java:84)
at agents.PlayerMap.seeFiguredMap(PlayerMap.java:196)
at agents.TickerExplorerRandomMapFile.seeFiguredMap(TickerExplorerRandomMapFile.java:206)
at agents.TickerExplorerRandomMapFile$1.onTick(TickerExplorerRandomMapFile.java:236)
at jade.core.behaviours.TickerBehaviour.action(TickerBehaviour.java:72)
at jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344)
at jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1532)
at jade.core.Agent.run(Agent.java:1471)
at java.lang.Thread.run(Thread.java:722)