欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件写入

程序员文章站 2022-07-15 08:47:41
...
/**
* 保存文件
* @param filePath
* @param reader
* @throws DefaultException
*/
public static void saveFile(String filePath,InputStream reader) throws DefaultException{
OutputStream writer = null;
File file = new File(filePath);
File parent = file.getParentFile();
if (!parent.exists())
parent.mkdirs();
try {
writer = new FileOutputStream(file);
byte[] b = new byte[1024];
int bytesRead = 0;
while((bytesRead=reader.read(b, 0, 1024)) != -1){
writer.write(b,0,bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new DefaultException(e);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new DefaultException(ioe);
} finally {
try{
reader.close();
writer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
相关标签: 文件写入