Java 更新eclipse插件中的IFile内容
我有一个文件,我想根据一些用户菜单选择更新。 我的代码得到了IFile 如果它不存在,则创建它(使用用户的内容),如果它存在,则应更新它。 我目前的代码是:Java 更新eclipse插件中的IFile内容,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,我有一个文件,我想根据一些用户菜单选择更新。 我的代码得到了IFile 如果它不存在,则创建它(使用用户的内容),如果它存在,则应更新它。 我目前的代码是: String userString= "original String"; //This will be set by the user byte[] bytes = userString.getBytes(); InputStream source = new ByteArrayInputStream(bytes)
String userString= "original String"; //This will be set by the user
byte[] bytes = userString.getBytes();
InputStream source = new ByteArrayInputStream(bytes);
try {
if( !file.exists()){
file.create(source, IResource.NONE, null);
}
else{
InputStream content = file.getContents();
//TODO augment content
file.setContents(content, 1, null);
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
IDE.openEditor(page, file);
我的问题是,即使我获得了原始内容并设置了文件的内容,我在更新时也会得到一个空文件,即,整个内容被删除
我做错了什么?您评论中的这个版本的代码适合我:
InputStream inputStream = file.getContents();
StringWriter writer = new StringWriter();
// Copy to string, use the file's encoding
IOUtils.copy(inputStream, writer, file.getCharset());
// Done with input
inputStream.close();
String theString = writer.toString();
theString = theString + " added";
// Get bytes using the file's encoding
byte[] bytes = theString.getBytes(file.getCharset());
InputStream source = new ByteArrayInputStream(bytes);
file.setContents(source, IResource.FORCE, null);
请注意原始输入流的关闭,以及使用
file.getCharset()
来使用正确的编码。您的注释中的此版本的代码适用于我:
InputStream inputStream = file.getContents();
StringWriter writer = new StringWriter();
// Copy to string, use the file's encoding
IOUtils.copy(inputStream, writer, file.getCharset());
// Done with input
inputStream.close();
String theString = writer.toString();
theString = theString + " added";
// Get bytes using the file's encoding
byte[] bytes = theString.getBytes(file.getCharset());
InputStream source = new ByteArrayInputStream(bytes);
file.setContents(source, IResource.FORCE, null);
请注意原始输入流的关闭以及使用
file.getCharset()
来使用正确的编码。如果文件已经存在,代码只会将文件内容设置为现有内容。但是,我想这很可能会截断文件,因为您同时读取和写入文件。如果文件已经存在,你到底想做什么?嘿。抱歉,如果我的帖子不清楚,但我有一个//TODO扩充内容的想法,即基于用户输入,我将获取当前文件内容+将用户输入添加到当前现有内容,并使用新内容设置文件。既然我想避免任何同步问题,那么最好的办法就是获取内容、更改内容并重新设置。但是,当我执行setContents时,我得到的是一个空文件。读完后,您是否调用了content.close()
?您需要向我们显示您用于获取内容的实际代码,因为您当前向我们显示的内容毫无意义,也没有显示问题所在。@greg-499,说我想添加“added”文件的当前内容不是“”InputStream content=file.getContents();StringWriter编写器=新的StringWriter();复制(输入流、写入器、编码);字符串theString=writer.toString();字符串=字符串+“添加”字节[]字节=字符串.getBytes();InputStream源=新的ByteArrayInputStream(字节);file.setContents(source,1,null);“”如果文件已经存在,该代码只是将文件内容设置为现有内容。但是,我想这很可能会截断文件,因为您同时读取和写入文件。如果文件已经存在,你到底想做什么?嘿。抱歉,如果我的帖子不清楚,但我有一个//TODO扩充内容的想法,即基于用户输入,我将获取当前文件内容+将用户输入添加到当前现有内容,并使用新内容设置文件。既然我想避免任何同步问题,那么最好的办法就是获取内容、更改内容并重新设置。但是,当我执行setContents时,我得到的是一个空文件。读完后,您是否调用了content.close()
?您需要向我们显示您用于获取内容的实际代码,因为您当前向我们显示的内容毫无意义,也没有显示问题所在。@greg-499,说我想添加“added”文件的当前内容不是“”InputStream content=file.getContents();StringWriter编写器=新的StringWriter();复制(输入流、写入器、编码);字符串theString=writer.toString();字符串=字符串+“添加”字节[]字节=字符串.getBytes();InputStream源=新的ByteArrayInputStream(字节);file.setContents(source,1,null);“”我缺少inputStream.close();我缺少inputStream.close();