【发布时间】:2016-07-03 18:48:29
【问题描述】:
我有一个 Java 类,它使用 FileOutputStream 和 BufferedOutputStream 编写文件。这工作正常,但我的意图是我想在 java 中编写任意数量的文件,而不仅仅是一个。这是我用 Java 编写的代码
public class FileToBeTaken{
public void fileBack(byte [] output) {
FileOutputStream fop = null;
BufferedOutputStream bos = null;
File file;
try {
file= new File("/Users/user/Desktop/newfile.txt");
fop = new FileOutputStream(file);
bos = new BufferedOutputStream(fop);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
bos.write(output);
bos.flush();
bos.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这里fileBack 方法在for 循环内从另一个类调用n 次。所以每次我需要在我的桌面上写一个新文件,因为我的代码是我只取最后一次迭代的文件。另外我应该提到,对于每个迭代,作为此类的参数,发送一个字节数组,该数组由“byte [] output
【问题讨论】:
-
你能提供一个运行示例吗?
-
显然您已经了解可以将参数传递给函数,因为您将其用于文件的内容。我不明白是什么阻止您也将文件名作为参数传递。
-
@njzk2 因为我想在上面写的这个类中创建这些文件名所以我真的不明白如何将它作为参数传递给我的其他类
标签: java list file fileoutputstream