文件写入
程序员文章站
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();
}
}
}
下一篇: Unity浏览本地文件