在Java Spring MVC中使用cronjob转换包含jsonstring(jsonarray)的巨大.txt文件。获得超ememoryerror:java堆空间
小文件已转换,但是对于大文件,我得到以下例外,
Exception in thread "C3P0PooledConnectionPoolManager[identityToken->2sw75dao1v61ja2kwuaoa|292bfbfb]-AdminTaskTimer" java.lang.OutOfMemoryError: Java heap space
2022-05-24 00:20:37 WARN ThreadPoolAsynchronousRunner$PoolThread:run 749 - An Error forced the closing of Thread[C3P0PooledConnectionPoolManager[identityToken->2sw75dao1v61ja2kwuaoa|292bfbfb]-HelperThread-#2,5,main]. Will attempt to reconstruct, but this might mean that something bad is happening.
java.lang.OutOfMemoryError: Java heap space
我有一个240MB .TXT文件,我试图使用Cronjob中的以下代码将其转换为Excel
软件包samplepackage.cron;
导入java.io.bufferedReader; 导入java.io.file; 导入java.io.io.fileoutputstream; 导入java.io..filereader; 导入java.io.ioexception; 导入java.math.bigdecimal; 导入java.text.simpledateformat; 导入java.util.arraylist; 导入java.util.date; 导入java.util.list; 导入org.apache.poi.ss.s.usermodel.row; 导入org.apache.poi.xssf.streaming.sxssfsheet; 导入org.apache.poi.xssf.streaming.sxssfworkbook; 导入org.json.jsonarray; 导入org.json.jsonobject; 导入org.springframework.beans.factory.annotation.autowired; 导入org.springframework.context.annotation.configuration; 导入org.springframework.scheduling.annotation.enablescheduling; 导入org.springframework.scheduling.annotation.scheduled; 导入org.springframework.stereotype.component; 导入com.ril.asp.gstasp.utils.gstrutils; 导入com.ril.asp.gstasp.wrapper.logwrapper; @成分 @配置 @enablescheduling 公共类Cronjobgstr2B { @Autowired 私人gstrutil gstrutil; @scheduled(cron =“ */120 * * * * * *”) public void scheduletaskingscronexpression()抛出ioexception { 现在= system.currenttimemillis() / 1000; logwrapper.info(cronjobgstr2b.class,“使用cron作业的计划任务 - ” +现在); 文件文件夹=新文件(gstrutils.getPropertyValue(“ gstr2b_files”)); file [] listOffiles = folder.listfiles(); if(listoffiles.length!= 0){ for(文件文件:listOffiles){ if(file.isfile()){ logwrapper.info(cronjobgstr2b.class,file.getName()); 字符串fileName = file.getName(); if(file.getName().tolowercase()。endswith(“。txt”)){ if(filename.contains(“ - done”)){ logwrapper.info(cronjobgstr2b.class,“ Excel已经创建”); } 别的 { string [] arrofstr = filename.split(“ _”,6); 字符串GSTIN = ARROFSTR [0]; 字符串userId = arrofstr [1]; 字符串周期= arrofstr [2]; 字符串部分= arrofstr [3]; 字符串RecordScount = Arrofstr [4]; BufferedReader Reader =新的BufferedReader( new FileReader(gstrutils.getPropertyValue(“ gstr2b_files”)+ fileName)); 字符串CurrentLine = Reader.Readline(); reader.close(); jsonarray jsonarray = new JSONARRAY(CurrentLine); jsonarray.length(); 字符串[]标头= null; 对象[] objdata = null; 列表< object []> listData = new ArrayList<>(); 字符串时间戳=新的SimpleDateFormat(“ yyyymmddhhmmss”)。格式(new Date()); 字符串XLSXFILENAME =“”; if(equart.equalsignorecase(“ b2b”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商的GSTR1/5提交日期”,“供应商的GSTR1/5的提交期”, “发票号码”,“发票类型”,“发票值”,“供应地点”, “供应吸引反向电荷”,“ ITC可用性”, “ ITC不可用的原因”,“适用于税率的%”,“项目编号”, “税率”,“总应税价值”,“根据发票”,“ IGST金额”, “根据发票的CGST金额”,“根据发票的SGST金额”, “根据发票的限额量”}; XlsxfiLename = gstin +“ _” +“ gstr2b b2b” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalsignorecase(“ cdnr”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商的GSTR1/5提交日期”,“供应商的GSTR1/5的提交期”, “注意号码”,“ Note Type”,“ Note供应类型”,“ Note Date(DD-MM-YYYY)”, “笔记值”,“供应地点”,“供应吸引反向费用”, “ ITC可用性”,“ ITC不可用的原因”,“适用于税率的%”, “项目编号”,“税率”,“总应税价值”,“综合税额(卢比)”, “中央税额(卢比)”,“州税/ UT金额(卢比)”, “ cess量(rs。)”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B CDN” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalsignorecase(“ b2ba”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商的GSTR1/5提交日期”,“供应商的GSTR1/5的提交期”, “ Orignal Invoice Number”,“ Orignal Invoice Date(DD-MM-YYYY)”, “发票编号”,“发票类型”,“发票日期”,“发票值(rs。)”, “供应地点(州法典)”,“供应吸引了反向费用”, “ ITC可用性”,“ ITC不可用的原因”,“适用于税率的%”, “项目编号”,“税率”,“总应税价值”,“综合税额(卢比)”, “中央税额(卢比)”,“州税/工会领土税额(卢比)”, “ cess量(rs。)”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B B2BA数据” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalsignorecase(“ cdnra”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商的GSTR1/5提交日期”,“供应商的GSTR1/5的提交期”, “原始音符号”,“原始注日日期”,“原始笔记类型”, “注意号码”,“ Note Type”,“ Note供应类型”,“ Note Date”,“ Note Value”, “ S fupply的地方”,“供应吸引反向充电”,“ ITC可用性”, “ ITC不可用的原因”,“适用于税率的%”,“项目编号”, “税价”,“总应税价值”,“综合税额(卢比)”, “中央税额(卢比)”,“州税/ UT金额(卢比)”, “ cess量(rs。)”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B cDNA数据” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalalsignorecase(“ isd”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商申请GSTR6的日期”,“供应商GSTR6的提交期”, “ ISD文档类型”,“ ISD文档号”,“ ISD文档日期”, “原始发票号码”,“原始发票日期”, “综合税额(卢比)”,“中央税额(卢比)”, “州税/ UT金额(Rs。)”,“ CESS金额(Rs。)”,“符合ITC”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B ISD数据” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalalsignorecase(“ isda”)){ 标题= new String [] {“供应商的GSTIN”,“供应商的贸易/法定名称”, “供应商申请GSTR6的日期”,“供应商GSTR6的提交期”, “原始ISD文档类型”,“原始ISD文档编号”, “原始ISD文档日期”,“ ISD文档类型”,“ ISD文档编号”, “ ISD文档日期”,“原始发票号”,“原始发票日期”, “综合税额(卢比)”,“中央税额(卢比)”, “州税/ UT金额(Rs。)”,“ CESS金额(Rs。)”,“符合ITC”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B ISDA数据” +“ _” + ofere + ofere + ofere +“ _” + timestamp; } if(equart.equalsignorecase(“ impg”)){ 标题= new String [] {“ Icegate参考日期”,“在GST系统中接收到的日期”, “ port代码”,“条目清单”,“入境单日期”,“已修订”, “总应税价值”,“总IGST金额”,“总CESS金额”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B IMPG数据” +“ _” + ofere + ofere +“ _” + timestamp; } if(equart.equalalsignorecase(“ impgsez”)){ 标题= new String [] {“ Icegate参考日期”,“在GST系统中接收到的日期”, “ port代码”,“条目清单”,“入境单日期”,“已修订”, “总应税价值”,“总IGST金额”,“总CESS金额”}; XLSXFILENAME = GSTIN +“ _” +“ GSTR2B IMPGSEZ数据” +“ _” + ofere + ofere +“ _” + timestamp; } if(jsonarray实例jsonarray){ for(int x1 = 0; x1< jsonarray.length(); x1 ++){ jsonObject jsonobj1 = jsonarray.getJsonObject(x1); if(jsonobj1.has(“ error_desc”)|| jsonobj1.has(“ error_grp”) || jsonobj1.has(“ error_code”)){ } 别的 { if(jsonobj1.has(“ gstn”)){ if(jsonobj1.get(“ gstn”)jsonObject的实例){ jsonObject jsonobjdocdata = jsonobj1.getJsonObject(“ gstn”); // logwrapper.info(gstr2prereconserviceimpl.class,“ |||||||||在这里访问code
Small files are converted but for huge files I am getting the following exception
Exception in thread "C3P0PooledConnectionPoolManager[identityToken->2sw75dao1v61ja2kwuaoa|292bfbfb]-AdminTaskTimer" java.lang.OutOfMemoryError: Java heap space
2022-05-24 00:20:37 WARN ThreadPoolAsynchronousRunner$PoolThread:run 749 - An Error forced the closing of Thread[C3P0PooledConnectionPoolManager[identityToken->2sw75dao1v61ja2kwuaoa|292bfbfb]-HelperThread-#2,5,main]. Will attempt to reconstruct, but this might mean that something bad is happening.
java.lang.OutOfMemoryError: Java heap space
I have a 240MB .txt file which I am trying to convert to excel using the following code in CronJob
package samplepackage.cron;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import com.ril.asp.gstasp.utils.GSTRUtils;
import com.ril.asp.gstasp.wrapper.LogWrapper;@Component
@Configuration
@EnableScheduling
public class CronJobGstr2b {@Autowired
private GSTRUtil gstrUtil;@Scheduled(cron = "*/120 * * * * *")
public void scheduleTaskUsingCronExpression() throws IOException {long now = System.currentTimeMillis() / 1000;
LogWrapper.info(CronJobGstr2b.class, "schedule tasks using cron jobs - " + now);File folder = new File(gstrUtils.getPropertyValue("GSTR2B_FILES"));
File[] listOfFiles = folder.listFiles();if (listOfFiles.length != 0) {
for (File file : listOfFiles) {
if (file.isFile()) {
LogWrapper.info(CronJobGstr2b.class, file.getName());
String fileName = file.getName();if (file.getName().toLowerCase().endsWith(".txt")) {
if (fileName.contains("-done")) {
LogWrapper.info(CronJobGstr2b.class, "Excel already created");
} else {String[] arrOfStr = fileName.split("_", 6);
String gstin = arrOfStr[0];
String userId = arrOfStr[1];
String period = arrOfStr[2];
String section = arrOfStr[3];
String recordsCount = arrOfStr[4];BufferedReader reader = new BufferedReader(
new FileReader(gstrUtils.getPropertyValue("GSTR2B_FILES")+ fileName));
String currentLine = reader.readLine();
reader.close();
JSONArray jsonArray = new JSONArray(currentLine);
jsonArray.length();String[] headers = null;
Object[] ObjData = null;
List<Object[]> listData = new ArrayList<>();
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String xlsxFileName = "";
if (section.equalsIgnoreCase("B2B")) {
headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR1/5 of supplier", "Filing period of GSTR1/5 of supplier",
"Invoice Number", "Invoice Type", "Invoice Value", "Place of supply",
"Supply attracts reverse charge", "ITC Availability",
"Reason for ITC Unavailability", "Applicable % of tax rate", "Item Number",
"Tax Rate", "Total Taxable value", "IGST Amount as per invoice",
"CGST Amount as per invoice", "SGST Amount as per invoice",
"CESS Amount as per invoice" };xlsxFileName = gstin + "_" + "GSTR2B B2B" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("CDNR")) {
headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR1/5 of supplier", "Filing period of GSTR1/5 of supplier",
"Note Number", "Note Type", "Note Supply Type", "Note Date(dd-mm-yyyy)",
"Note Value", "Place of supply", "Supply Attracts Reverse Charge",
"ITC Availability", "Reason for ITC Unavailability", "Applicable % of tax rate",
"Item Number", "Tax Rate", "Total Taxable Value", "Integrated Tax Amount (Rs.)",
"Central Tax Amount (Rs.)", "State Tax/ UT Amount (Rs.)",
"Cess Amount (Rs.)" };xlsxFileName = gstin + "_" + "GSTR2B CDN" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("B2BA")) {
headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR1/5 of supplier", "Filing period of GSTR1/5 of supplier",
"Orignal Invoice Number ", "Orignal Invoice Date (dd-mm-yyyy) ",
"Invoice Number", "Invoice Type", "Invoice Date", "Invoice Value(Rs.) ",
"Place of Supply (State Code) ", "Supply attracts reverse charge",
"ITC Availability", "Reason for ITC Unavailability", "Applicable % of tax rate",
"Item Number", "Tax Rate", "Total Taxable value", "Integrated Tax Amount (Rs.)",
"Central Tax Amount (Rs.)", "State Tax/Union Territory Tax Amount (Rs.)",
"CESS Amount (Rs.)" };xlsxFileName = gstin + "_" + "GSTR2B B2BA Data" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("CDNRA")) {
headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR1/5 of supplier", "Filing period of GSTR1/5 of supplier",
"Original Note Number", "Original Note Date", "Original Note Type",
"Note Number", "Note Type", "Note Supply Type", "Note Date", "Note Value",
"Place of s upply", "Supply attracts Reverse Charge", "ITC Availability",
"Reason for ITC Unavailability", "Applicable % of tax rate", "Item Number",
"Tax Value", "Total Taxable value", "Integrated Tax Amount (Rs.)",
"Central Tax Amount (Rs.)", "State Tax/ UT Amount (Rs.)",
"Cess Amount (Rs.)" };xlsxFileName = gstin + "_" + "GSTR2B CDNA Data" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("ISD")) {
headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR6 of supplier", "Filing period of GSTR6 of supplier",
"ISD Document Type", "ISD Document Number", "ISD Document Date",
"Original Invoice Number", "Original Invoice date",
"Integrated Tax Amount (Rs.)", "Central Tax Amount (Rs.)",
"State Tax/ UT Amount (Rs.)", "Cess Amount (Rs.)", "Eligible for ITC" };xlsxFileName = gstin + "_" + "GSTR2B ISD Data" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("ISDA")) {headers = new String[] { "GSTIN of the Supplier", "Trade/Legal name of the supplier",
"Date of filing GSTR6 of supplier", "Filing period of GSTR6 of supplier",
"Original ISD Document Type", "Original ISD Document Number",
"Original ISD Document Date", "ISD Document Type", "ISD Document Number",
"ISD Document Date", "Original Invoice Number", "Original Invoice date",
"Integrated Tax Amount (Rs.)", "Central Tax Amount (Rs.)",
"State Tax/ UT Amount (Rs.)", "Cess Amount (Rs.)", "Eligible for ITC" };xlsxFileName = gstin + "_" + "GSTR2B ISDA Data" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("IMPG")) {
headers = new String[] { "ICEGATE Reference Date", "Received Date in the GST System",
"Port Code", "Bill of Entry Number", "Bill of Entry Date", "Is Amended",
"Total Taxable value", "Total IGST amount", "Total Cess Amount" };xlsxFileName = gstin + "_" + "GSTR2B IMPG Data" + "_" + period + "_" + timeStamp;
}
if (section.equalsIgnoreCase("IMPGSEZ")) {
headers = new String[] { "ICEGATE Reference Date", "Received Date in the GST System",
"Port Code", "Bill of Entry Number", "Bill of Entry Date", "Is Amended",
"Total Taxable value", "Total IGST amount", "Total Cess Amount" };xlsxFileName = gstin + "_" + "GSTR2B IMPGSEZ Data" + "_" + period + "_" + timeStamp;
}
if (jsonArray instanceof JSONArray) {
for (int x1 = 0; x1 < jsonArray.length(); x1++) {
JSONObject jsonObj1 = jsonArray.getJSONObject(x1);
if (jsonObj1.has("error_desc") || jsonObj1.has("error_grp")
|| jsonObj1.has("error_code")) {
} else {
if (jsonObj1.has("gstn")) {if (jsonObj1.get("gstn") instanceof JSONObject) {
JSONObject jsonObjDocData = jsonObj1.getJSONObject("gstn");
// LogWrapper.info(GSTR2PreReconServiceImpl.class, "||||||||reached here in code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
");
if (jsonObjDocData.has("b2b")) {
if (jsonObjDocData.has("b2b")
&& jsonObjDocData.get("b2b") instanceof JSONObject) {
JSONObject jobj = jsonObjDocData.getJSONObject("b2b");
double cess;
double txval;
double cgst;
double igst;
double sgst;
double rt;
double num;
String ctin = "";
String trdnm = "";
String supfildt = "";
String supprd = "";
if (jobj.has("ctin")) {
ctin = jobj.getString("ctin");
}
if (jobj.has("trdnm")) {
trdnm = jobj.getString("trdnm");
}
if (jobj.has("supfildt")) {
supfildt = jobj.getString("supfildt");
}
if (jobj.has("supprd")) {
supprd = jobj.getString("supprd");
}
if (jobj.has("inv") && jobj.get("inv") instanceof JSONObject) {
JSONObject jitemobj = jobj.getJSONObject("inv");
double invValue = 0.0;
String invoiceNum = "";
String invType = "";
String InvDate = "";
String pos = "";
String rev = "";
String itcavl = "";
String rsn = "";
double diffprcnt = 0.00;
if (jitemobj.has("inum")) {
invoiceNum = jitemobj.getString("inum");
}
if (jitemobj.has("typ")) {
invType = jitemobj.getString("typ");
}
if (jitemobj.has("val")) {
invValue = invValue + jitemobj.getDouble("val");
}
if (jitemobj.has("pos")) {
pos = jitemobj.getString("pos");
} else {
pos = "";
}
if (jitemobj.has("rev")) {
rev = jitemobj.getString("rev");
}
if (jitemobj.has("itcavl")) {
itcavl = jitemobj.getString("itcavl");
}
if (jitemobj.has("rsn")) {
rsn = jitemobj.getString("rsn");
}
if (jitemobj.has("diffprcnt")) {
diffprcnt = diffprcnt + jitemobj.getDouble("diffprcnt");
}
if (jitemobj.has("items")
&& jitemobj.get("items") instanceof JSONArray) {
JSONArray jitemdataarray = jitemobj
.getJSONArray("items");
for (int j = 0; j < jitemdataarray.length(); j++) {
cess = 0.00;
txval = 0.00;
cgst = 0.00;
igst = 0.00;
sgst = 0.00;
rt = 0.00;
num = 0.00;
JSONObject jitemdataobjitemobj = jitemdataarray
.getJSONObject(j);
// JSONObject jitemdataobjitemobj =
// jitemdataobj.getJSONObject("itm_det");
// csamt =
// jitemdataobjitemobj.getDouble("csamt");
// txval =
// jitemdataobjitemobj.getDouble("txval");
// camt =
// jitemdataobjitemobj.getDouble("camt");
// iamt =
// jitemdataobjitemobj.getDouble("iamt");
// samt =
// jitemdataobjitemobj.getDouble("samt");
// rt =
// jitemdataobjitemobj.getInt("rt");
if (jitemdataobjitemobj.has("num")) {
num = jitemdataobjitemobj.getDouble("num");
}
if (jitemdataobjitemobj.has("rt")) {
rt = jitemdataobjitemobj.getDouble("rt");
}
if (jitemdataobjitemobj.has("txval")) {
txval = jitemdataobjitemobj.getDouble("txval");
}
if (jitemdataobjitemobj.has("igst")) {
igst = jitemdataobjitemobj.getDouble("igst");
}
if (jitemdataobjitemobj.has("cgst")) {
cgst = jitemdataobjitemobj.getDouble("cgst");
}
if (jitemdataobjitemobj.has("sgst")) {
sgst = jitemdataobjitemobj.getDouble("sgst");
}
if (jitemdataobjitemobj.has("cess")) {
cess = jitemdataobjitemobj.getDouble("cess");
}
ObjData = new Object[] { ctin, trdnm, supfildt,
supprd, invoiceNum, invType, invValue, pos,
rev, itcavl, rsn, diffprcnt, num, rt, txval,
igst, cgst, sgst, cess };
listData.add(ObjData);
}
}
}
}
}
}
}
}
}
}
SXSSFWorkbook workBook = new SXSSFWorkbook();
if (listData != null && listData.size() > 0) {
SXSSFSheet sheet = (SXSSFSheet) workBook
.createSheet(section.toUpperCase() + "Report");
Row r1 = sheet.createRow(0);
int rowCount = 1;
for (int rn = 0; rn < headers.length; rn++) {
r1.createCell(rn).setCellValue(headers[rn]);
}
for (Object[] data : listData) {
Row row = sheet.createRow(rowCount);
for (int rn = 0; rn < data.length; rn++) {
if (data[rn] instanceof String) {
row.createCell(rn).setCellValue((String) data[rn]);
} else if (data[rn] instanceof Integer) {
row.createCell(rn).setCellValue((Integer) data[rn]);
} else if (data[rn] instanceof Double) {
row.createCell(rn).setCellValue((Double) data[rn]);
} else if (data[rn] instanceof BigDecimal) {
row.createCell(rn).setCellValue(String.format("%.2f", data[rn]));
}
}
rowCount++;
}
try (FileOutputStream byteArrayOutputStream = new FileOutputStream(
gstrUtils.getPropertyValue("GSTR2B_FILES")+gstin + "_" + userId + "_" + period + "_" + section + "_" + recordsCount + "_GSTR2BFile.xlsx");) {
workBook.write(byteArrayOutputStream);
} finally {
workBook.close();
workBook.dispose();
// renaming a file Start
File oldfile = new File(gstrUtils.getPropertyValue("GSTR2B_FILES") + fileName);
String beforeExtension = fileName.substring(0, fileName.lastIndexOf('.'));
String fileExtension = fileName.substring(fileName.lastIndexOf('.'));
File newfile = new File(gstrUtils.getPropertyValue("GSTR2B_TEXT_FILES") + beforeExtension
+ "-done" + fileExtension);
if (oldfile.renameTo(newfile)) {
LogWrapper.info(CronJobGstr2b.class, "File renamed");
} else {
LogWrapper.info(CronJobGstr2b.class, "Sorry! the file can't be renamed");
}
// renaming a file End
}
}
}
}
} else {
LogWrapper.info(CronJobGstr2b.class, "The file is not a text (.txt) file hence skipping");
}
}
}
} else {
LogWrapper.info(CronJobGstr2b.class, "No files found in the folder cronJobTextFiles");
}
}
}
有没有一种方法可以通过行读取JSONSTRING的JSONTRAY或获取当前处于字符串格式的JSONARRAY的第一个对象,然后继续逐行附加到Excel -excel?
");
if (jsonObjDocData.has("b2b")) {
if (jsonObjDocData.has("b2b")
&& jsonObjDocData.get("b2b") instanceof JSONObject) {
JSONObject jobj = jsonObjDocData.getJSONObject("b2b");
double cess;
double txval;
double cgst;
double igst;
double sgst;
double rt;
double num;
String ctin = "";
String trdnm = "";
String supfildt = "";
String supprd = "";
if (jobj.has("ctin")) {
ctin = jobj.getString("ctin");
}
if (jobj.has("trdnm")) {
trdnm = jobj.getString("trdnm");
}
if (jobj.has("supfildt")) {
supfildt = jobj.getString("supfildt");
}
if (jobj.has("supprd")) {
supprd = jobj.getString("supprd");
}
if (jobj.has("inv") && jobj.get("inv") instanceof JSONObject) {
JSONObject jitemobj = jobj.getJSONObject("inv");
double invValue = 0.0;
String invoiceNum = "";
String invType = "";
String InvDate = "";
String pos = "";
String rev = "";
String itcavl = "";
String rsn = "";
double diffprcnt = 0.00;
if (jitemobj.has("inum")) {
invoiceNum = jitemobj.getString("inum");
}
if (jitemobj.has("typ")) {
invType = jitemobj.getString("typ");
}
if (jitemobj.has("val")) {
invValue = invValue + jitemobj.getDouble("val");
}
if (jitemobj.has("pos")) {
pos = jitemobj.getString("pos");
} else {
pos = "";
}
if (jitemobj.has("rev")) {
rev = jitemobj.getString("rev");
}
if (jitemobj.has("itcavl")) {
itcavl = jitemobj.getString("itcavl");
}
if (jitemobj.has("rsn")) {
rsn = jitemobj.getString("rsn");
}
if (jitemobj.has("diffprcnt")) {
diffprcnt = diffprcnt + jitemobj.getDouble("diffprcnt");
}
if (jitemobj.has("items")
&& jitemobj.get("items") instanceof JSONArray) {
JSONArray jitemdataarray = jitemobj
.getJSONArray("items");
for (int j = 0; j < jitemdataarray.length(); j++) {
cess = 0.00;
txval = 0.00;
cgst = 0.00;
igst = 0.00;
sgst = 0.00;
rt = 0.00;
num = 0.00;
JSONObject jitemdataobjitemobj = jitemdataarray
.getJSONObject(j);
// JSONObject jitemdataobjitemobj =
// jitemdataobj.getJSONObject("itm_det");
// csamt =
// jitemdataobjitemobj.getDouble("csamt");
// txval =
// jitemdataobjitemobj.getDouble("txval");
// camt =
// jitemdataobjitemobj.getDouble("camt");
// iamt =
// jitemdataobjitemobj.getDouble("iamt");
// samt =
// jitemdataobjitemobj.getDouble("samt");
// rt =
// jitemdataobjitemobj.getInt("rt");
if (jitemdataobjitemobj.has("num")) {
num = jitemdataobjitemobj.getDouble("num");
}
if (jitemdataobjitemobj.has("rt")) {
rt = jitemdataobjitemobj.getDouble("rt");
}
if (jitemdataobjitemobj.has("txval")) {
txval = jitemdataobjitemobj.getDouble("txval");
}
if (jitemdataobjitemobj.has("igst")) {
igst = jitemdataobjitemobj.getDouble("igst");
}
if (jitemdataobjitemobj.has("cgst")) {
cgst = jitemdataobjitemobj.getDouble("cgst");
}
if (jitemdataobjitemobj.has("sgst")) {
sgst = jitemdataobjitemobj.getDouble("sgst");
}
if (jitemdataobjitemobj.has("cess")) {
cess = jitemdataobjitemobj.getDouble("cess");
}
ObjData = new Object[] { ctin, trdnm, supfildt,
supprd, invoiceNum, invType, invValue, pos,
rev, itcavl, rsn, diffprcnt, num, rt, txval,
igst, cgst, sgst, cess };
listData.add(ObjData);
}
}
}
}
}
}
}
}
}
}
SXSSFWorkbook workBook = new SXSSFWorkbook();
if (listData != null && listData.size() > 0) {
SXSSFSheet sheet = (SXSSFSheet) workBook
.createSheet(section.toUpperCase() + "Report");
Row r1 = sheet.createRow(0);
int rowCount = 1;
for (int rn = 0; rn < headers.length; rn++) {
r1.createCell(rn).setCellValue(headers[rn]);
}
for (Object[] data : listData) {
Row row = sheet.createRow(rowCount);
for (int rn = 0; rn < data.length; rn++) {
if (data[rn] instanceof String) {
row.createCell(rn).setCellValue((String) data[rn]);
} else if (data[rn] instanceof Integer) {
row.createCell(rn).setCellValue((Integer) data[rn]);
} else if (data[rn] instanceof Double) {
row.createCell(rn).setCellValue((Double) data[rn]);
} else if (data[rn] instanceof BigDecimal) {
row.createCell(rn).setCellValue(String.format("%.2f", data[rn]));
}
}
rowCount++;
}
try (FileOutputStream byteArrayOutputStream = new FileOutputStream(
gstrUtils.getPropertyValue("GSTR2B_FILES")+gstin + "_" + userId + "_" + period + "_" + section + "_" + recordsCount + "_GSTR2BFile.xlsx");) {
workBook.write(byteArrayOutputStream);
} finally {
workBook.close();
workBook.dispose();
// renaming a file Start
File oldfile = new File(gstrUtils.getPropertyValue("GSTR2B_FILES") + fileName);
String beforeExtension = fileName.substring(0, fileName.lastIndexOf('.'));
String fileExtension = fileName.substring(fileName.lastIndexOf('.'));
File newfile = new File(gstrUtils.getPropertyValue("GSTR2B_TEXT_FILES") + beforeExtension
+ "-done" + fileExtension);
if (oldfile.renameTo(newfile)) {
LogWrapper.info(CronJobGstr2b.class, "File renamed");
} else {
LogWrapper.info(CronJobGstr2b.class, "Sorry! the file can't be renamed");
}
// renaming a file End
}
}
}
}
} else {
LogWrapper.info(CronJobGstr2b.class, "The file is not a text (.txt) file hence skipping");
}
}
}
} else {
LogWrapper.info(CronJobGstr2b.class, "No files found in the folder cronJobTextFiles");
}
}
}
Is there a way in which I can read the jsonString line by line or get first object of jsonArray which is currently in string format and then keep on appending to excel line by line?