Java blackberry中的读/写文件
我正在使用此代码写入一个文件Java blackberry中的读/写文件,java,file,blackberry,Java,File,Blackberry,我正在使用此代码写入一个文件 protected void writeFile(String text) { DataOutputStream os = null; FileConnection fconn = null; try { fconn = (FileConnection) Connector.open("file:///store/home/user
protected void writeFile(String text) {
DataOutputStream os = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open("file:///store/home/user/documents/file.txt", Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();
os = fconn.openDataOutputStream();
os.write(text.getBytes());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != os)
os.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}}
代码运行良好
我的问题是,如果我第一次写“Banglore”,当我读到它时,我会得到“Banglore”。
但是,当我第二次写“印度”的时候,当我读到它的时候,我得到了“Indialore”。
所以,基本上,它的内容并没有随着文本的变化而改变,我给出。
请告诉我如何修复此问题。在文件中写入不会删除内容,但只会替换内容,因此在“班加罗尔”上写入“印度”将用“印度”替换“班加”,其余内容将保持不变。如果您想用新内容完全删除旧内容,您需要
新数据结束的文件
truncate(text.getBytes().length)
在文件中写入不会删除内容,但只会替换内容,因此在“Bangalore”上写入“india”将用“india”替换“Banga”,其余内容将保持不变。如果您想用新内容完全删除旧内容,您需要
新数据结束的文件<代码>截断(text.getBytes().length)