我想要有一个方法,它的目的是:
fileDescriptor openFile (filename):将打开一个具有特定名称的文件(如果该文件已经存在,它将删除该文件的内容),并在第一行放入“return of Process”;返回文件描述符
public FileDescriptor openFile(String fileName){
}有人能帮我解决这个问题吗?
发布于 2012-08-09 18:30:21
下面是如何做的:
private FileDescriptor openFile(String path)
throws FileNotFoundException, IOException {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
// remember th 'fos' reference somewhere for later closing it
fos.write((new Date() + " Beginning of process...").getBytes());
return fos.getFD();
}但是,最好返回FileOutputStream实例(您也可以从该实例获取FileDescriptor ),因为您可以向文件中添加更多内容并适当地关闭它
如果您的方法签名不允许出现异常,则尝试捕获并抛出RuntimeException或try-catch异常并返回null (在这种情况下,添加javadoc来解释返回的null意味着什么)
https://stackoverflow.com/questions/11880747
复制相似问题