我需要编写大量不同的小文件(每个文件大约 30kb)。下面是我的 java 代码:
for(every file){
File destFile = new File(fileName);
try {
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// System.out.println(dr.readLine());
bos.write(result.getBytes(
"UTF-8"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当然,对于每个文件,我都必须新建一个 File 对象。但是是否有必要为每个不同的文件新建 fileOutputStream 和 BufferdOutputStream 对象?有没有更有效的方法来编写大量小文件?