【java】java 读写文件

场景:JDK8  将上传的文件,保存到服务器

Java读写文件操作:

MultipartFile file
InputStream inputStream = file.getInputStream();
private void saveOldFile(String uid,String fileName,InputStream inputStream){
String filePath = getFilePath() + "/" + uid +fileName;
BufferedReader reader = null;
BufferedWriter writer = null;
try {
File file = new File(filePath);
reader = new BufferedReader(new InputStreamReader(inputStream));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); String lineText = null;
while ((lineText = reader.readLine()) != null){
writer.write(lineText+FILE_CONTENT_SPLIT_MARK); } } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} }
}
上一篇:大型分布式C++框架《三:序列化与反序列化》


下一篇:转:Java读写文件各种方法及性能比较