java⼤⽂件⾸⾏追加,java中实现,在⼤⽂件的第⼀⾏添加内容需求描述:
最近在写⼀个定时任务,发送多次请求到接⼝,接⼝返回报⽂写⼊到⼀个⽂件中,⽂件要求格式第⼀⾏为总数。这个总数只能在最后的时候才能知道,
这就相当于提出了⼀个要求:在⽂件的第⼀⾏添加内容,以前的内容整体向下偏移⼀⾏。
遇到的问题:
我开始做的⽅法是,在代码中 new StringBuffer ,等全部报⽂返回后,把返回的报⽂先全部保存到buffer中,⽤PrintWriter写⼊总数到⽂件,再将返回报⽂写⼊。可在实际代码运⾏中,由于每次返回的报⽂都存⼊到buffer中,返回的报⽂对象被buffer对象引⽤,没有释放掉,所以占⽤内存很⼤。折腾了半天不可⾏。只能换⼀种思路了。这个⽂件⼤⼩是50多M,内存⼏百兆没了。
那既然⼀次性保存到buffer中不⾏,那就返回⼀次报⽂就保存到本地临时⽂件中tmpFile,最后在另⼀个⽂件dinsFile中先写⼊总数,从tmpFile没读取⼀⾏,就写⼊到dinsFile。⼀开始没想到⽤Scammer这个类,从⽹上看其他⽹友的博⽂才发现正适合,还是很菜,多多学习。
这样占⽤内存的问题就解决了。下⾯上代码:
public class QueryBondBalTask extends BaseService{
private String url;
private Logger logger = Logger(QueryBondBalTask.class);
public void setUrl(String url) {
this.url = url;
}
public void getBondBalToText() {
ICstpCustodianConfigDao cstpCustodianConfigDao = (ICstpCustodianConfigDao)
List list = cstpCustodianConfigDao.selectCustodianAccount();
// 创建输出临时⽂件
String date = CurDate();
String name = "tmp_bond_subject_csbs_" + date + "_.txt";
String tmpPath = FppTradeConfig.ReadStringFromConfig("pPath");
File tmpFile = new File(tmpPath + name);
//中债登的接⼝包
DClient dClient = new DClient(url);
SAXReader saxReader = new SAXReader();
long sum = 0;
Reader reader = null;
PrintWriter pw = null;
try {
pw = new PrintWriter(tmpFile);
for (String bondId : list) {
// 模拟请求报⽂
String requestXml = createXml(bondId);
// 调⽤接⼝查询
String backMsg = dClient.sendMsg(requestXml);
if (backMsg == null || "" == backMsg) {
<("返回报⽂异常", new Exception());
}
//解析返回backMsg,⽣成临时⽂件
InputStream input1 = new Bytes()); reader = new InputStreamReader(input1);
Document document = ad(reader);
Element root = RootElement();
// 获取债券账号
Node accountNode = root.selectSingleNode("//Id");
String account = Text();
// 余额条数
List acctLdgList = root.selectNodes("//AcctLdg");
int size = acctLdgList.size();
// 获取所有标签
List bdList = root.selectNodes("//Bd");
for (Element bond : bdList) {
Node bdidNode = bond.selectSingleNode("BdId");
String bdid = Text();
// 从当前节点下获取
List acctLdglList = bond.selectNodes("SummryStmt/AcctLdg");
for (Element acctLdg : acctLdglList) {
Node ldgTpnNode = acctLdg.selectSingleNode("LdgTp");
String ldgTp = Text();
Node balNode = acctLdg.selectSingleNode("Bal");
String bal = Text();
String record = account + "|" + bdid + "|" + ldgTp + "|" + bal;
pw.println(record);
}
}
pw.flush();
sum = sum + size;
}
//将总数写⼊⽂件
WriteSum(sum,tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
<("中债登接⼝调⽤异常", e);
} catch (DocumentException e) {
e.printStackTrace();
<("", e);
} catch (Exception e) {
e.printStackTrace();
<("返回报⽂异常", e);
} finally{
if (pw != null) {
pw.close();
}
tmpFile.delete();
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String createXml(String bondId) {
String appPath = APP_HOME(); String path=appPath + "/l";
File file = new File(path);
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = ad(file);
} catch (DocumentException e) {
e.printStackTrace();
<("", e);
}
Element root = RootElement();
Node txFlowIdNode = root.selectSingleNode("//TxFlowId");
String txFlowId = ReconTxFlowID();
txFlowIdNode.setText(txFlowId);
Node creDtNode = root.selectSingleNode("//CreDtTm");
String CreDtTmDate = CurFormatTime();
creDtNode.setText(CreDtTmDate);
Node startNode = root.selectSingleNode("//StartDt");
String startDate = CurFormatDate();
startNode.setText(startDate);
Node endNode = root.selectSingleNode("//EndDt");
String endDate = CurFormatDate();
endNode.setText(endDate);
Node idNode = root.selectSingleNode("//Id");
idNode.setText(bondId);
String requestXml = document.asXML();
大文件发送
return requestXml;
}
//读取⽂件,将总数写⼊
private void WriteSum(long sum, File tmpFile) {
// 最终⽂件
String date = CurDate();
String name = "bond_subject_csbs_" + date + ".txt";
String destPath = FppTradeConfig.ReadStringFromConfig("QueryBondBalTask.destPath"); File file = new File(destPath + name);
PrintWriter pw = null;
Scanner scanner = null;
InputStream input = null;
try {
//输出到最终⽂件
pw = new PrintWriter(file);
pw.println(sum);
//读取临时⽂件
input = new FileInputStream(tmpFile); scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = Line();
pw.println(line);
}
pw.print("END");
pw.flush();
} catch (FileNotFoundException e) { ("",e);
} finally{
if (scanner != null) {
scanner.close();
}
if (pw != null) {
pw.close();
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}