Can someone please help me out that how to delete the file, below is the code using to convert the JSON file to Excel file, but after conversion am unable to delete the JSON file in the respective path.Getting exception saying that "java.nio.file.FileSystemException: .\Json_files\db-AAPL.json: The process cannot access the file because it is being used by another process "
public class DB_Search_API {
String stockname;
public static XSSFWorkbook wb;
public static XSSFSheet s;
public static FileInputStream fi;
public static FileOutputStream fo;
public static Row r;
public static Row r1;
public static Cell c;
@Test
public void FinBlock() throws Exception {
int lastRow = dataXl.getRowCount("Sheet1");
////
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattedDate = date.format(dateFormat);
DateTimeFormatter dateformat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH-mm");
String formatedate = date.format(dateformat);
Path dir = Paths.get(".//Output_Files//DB_Excelfiles-" formattedDate);
Files.createDirectories(dir);
wb = new XSSFWorkbook();
//////
for (int i = 2; i <= lastRow; i ) {
stockname = dataXl.getCellData("Sheet1", "Stock name", i);
String USERNAME = CONFIG.getProperty("Username");
String Password = CONFIG.getProperty("password");
String URI = CONFIG.getProperty("API");
SessionFilter session = new SessionFilter();
RestAssured.baseURI = URI;
String Response = given().auth().preemptive().basic(USERNAME, Password).filter(session).when()
.get(RestAssured.baseURI).then().extract().response().getCookie("RAY_SESSION_ID");
Response resp = given().cookie("RAY_SESSION_ID", Response).filter(session).when()
.get("******"
stockname
"*****")
.then().log().all().extract().response();
PrintWriter out = new PrintWriter(new FileWriter(".//Json_files//db-" stockname ".json", true), true);
out.write(resp.asString());
out.close();
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(".//Json_files//db-" stockname ".json"));
JSONObject jsonObj = (JSONObject) obj;
JSONObject response = (JSONObject) jsonObj.get("response");
JSONObject arraybody = (JSONObject) response.get("data");
JSONArray arrayindex = (JSONArray) arraybody.get("arrayRowData");
int n = arrayindex.size();
s = wb.createSheet(stockname);
for (int j = 0; j < n; j ) {
JSONObject jsonObject = new JSONObject((Map) arrayindex.get(j));
JSONObject jsonObject1 = (JSONObject) jsonObject.get("header");
String value = (String) jsonObject1.get("displayName");
s.createRow(j 1).createCell(0).setCellValue(value);
JSONArray arrayindex1 = (JSONArray) jsonObject.get("cellData");
int m = arrayindex1.size();
int x = m - 1;
r = s.getRow(j 1);
r1 = s.createRow(0);
for (int k = 0; k <= x; k ) {
JSONObject jsonObject12 = new JSONObject((Map) arrayindex1.get(k));
String newdate = (String) jsonObject12.get("fiscalDate");
r1.createCell(k 1).setCellValue(newdate);
if (jsonObject12.get("value") == null) {
r.createCell(k 1).setCellValue("-");
} else {
r.createCell(k 1).setCellValue((double) jsonObject12.get("value"));
}
}
}
fo = new FileOutputStream(dir "//DB_Search" "-" formatedate ".xlsx");
wb.write(fo);
fo.flush();
fo.close();
try {
Files.deleteIfExists(Paths.get(".//Json_files//db-" stockname ".json"));
} catch (NoSuchFileException e) {
System.out.println("No such file/directory exists");
} catch (DirectoryNotEmptyException e) {
System.out.println("Directory is not empty.");
}
}
}
}
CodePudding user response:
You do not close the FileReader. So instead of just creating a FileReader directly inside the method call parser.parse(..)., you should use a "try wih resources" (or at least close the reeader after reading the content).
So instead of:
Object obj = parser.parse(new FileReader(".//Json_files//db-" stockname ".json"));
You have code like:
Object obj;
try (FileReader reader = new FileReader(".//Json_files//db-" stockname ".json")) {
obj = parser.parse(reader);
}
That way the reader is closed and the file is no longer in use.
CodePudding user response:
Your variable names are not descriptive, so that's not good.
Try to flush()
the object that is reading the JSON and then close()
it, that should do it.