我有一个文本编辑器,它使用的代码与下面的代码几乎完全相同,用于读取和写入。当我打开小文件进行编辑时,当我尝试再次保存它们时,总会遇到此错误。但真正有趣的是,我的文本编辑器对于大约600字节或更多的文件(似乎每次都会有所不同)都能正常工作,这是我在这里无法复制的行为。FileNotFoundException在使用java读取的同一文件上打开FileOutputStream时出现错误
我目前的解决方法是检查1024字节的文件大小<,如果是这种情况,请使用java.io。
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class Test {
public static void main(String[] args) {
String text = null;
try {
FileInputStream fis = new FileInputStream("test.txt");
FileChannel ifc = fis.getChannel();
CharBuffer cb = Charset.forName("ISO-8859-15").newDecoder().
decode(ifc.map(FileChannel.MapMode.READ_ONLY, 0,
(int) ifc.size()));
text = cb.toString();
ifc.close();
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
FileOutputStream fos = new FileOutputStream("test.txt");
OutputStreamWriter osw = new OutputStreamWriter(
fos, "ISO-8859-15");
osw.write(text);
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对于上面的代码,我得到这个错误test.txt的各种大小从4个字节到10KB:
java.io.FileNotFoundException: test.txt (The requested operation cannot be perfo
rmed on a file with a user-mapped section open)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at Test.main(Test.java:23)
任何人都可以解释我做错了,我怎么能得到这工作,而仍然阅读与尼奥?