一、文件读写
涉及文本文件读写+二进制文件读写
读写流抽象基类(不能new操作)
1)字节流(二进制文件或文本文件)InputStream、OutputStream
实现文件读写基类FileInputStream、FileOutputStream
2)字符流(文本文件)Reader(InputStreamReader)、Writer(OutputStreamReader)
实现文件读写基类InputStreamReader、OutputStreamReader(常用FileReader、FileWriter)
1、java文件操作类图
2、读文件
a、分隔符方式
File inFile = new File(fileAbsolutePath);
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(inFile));
String str;
while((str = br.readLine()) != null){
String strArr[] = str.split("\\@\\#\\$", -1);
tempDO.setSubAcctNo(strArr[0]);
tempDO.setSubAcctName(strArr[1]);
}
}catch (IOException e) {
throw new BaseException("2000000", e, "文件io异常");
}catch (Exception e) {
throw new BaseException("2000000", e, "处理发生未知异常");
}finally {
inFile = null;
if(br != null){
br.close();
br = null;
}
}
b、定长方式
3、写文件
//写文件
File file = new File(fileAbsolutePath);
TrcLog.info(logFile, "写文件:" + file);
BufferedWriter bw = null;
final String MARK_SPLIT = "&#$"; // 标题、数据分隔符
DecimalFormat df = new DecimalFormat("0.00");
try {
// 创建文件
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
for(BookDO bookDO:bookDOList)
{
//文件内容:
bw.write(MARK_SPLIT);
bw.write(bookDO.getPlatDate());//平台日期
bw.write(MARK_SPLIT);
bw.write(bookDO.getPlatSeq());//平台流水
bw.newLine();
}
} catch (IOException ex) {
throw 异常;
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ex) {
throw 异常;
}
}