For requirement 4 I'm getting the following recommendation message from the mentor:
CodeGym security exception. You are performing a forbidden or potentially dangerous operation.
When I run the task on IntelliJ the end result is as expected. Where does this security exception happens?
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();;
File file = new File(fileName);
try {
if(!file.exists()){
throw new FileNotFoundException();
}
}catch (FileNotFoundException e){
System.out.println("Product file does not exists: " + fileName);
}
if (args.length > 0 && args[0].equals("-c")){
Product prod = new Product();
prod.setProdName(args[1]);
prod.setPrice(args[2]);
prod.setQuantity(args[3]);
if(file.exists()) {
Product.refreshProdList(file, prod);
}
}
else if(args.length == 0){
}
}
}
package com.codegym.task.task18.task1827;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Product {
private String prodId;
private String prodName;
private String price;
private String quantity;
public String getProdId() {
return prodId;
}
public void setProdId(String prodId) {
prodId = String.valueOf(Integer.parseInt(prodId) + 1);
prodId = setPadding(8, prodId);
this.prodId = prodId;
}
public String getProdName() {
return prodName;
}
public void setProdName(String prodName) {
prodName = setPadding(30, prodName);
this.prodName = prodName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
price = setPadding(8, price);
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
quantity = setPadding(4, quantity);
this.quantity = quantity;
}
public static void refreshProdList(File file, Product prod){
List<String> prodList = new ArrayList<>();
String record;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((record = reader.readLine()) != null){
prodList.add(record);
}
reader.close();
if(prodList.size() > 0){
rewrite(file, prodList);
}
if(prod != null){
setMaxId(prodList, prod);
addProd2List(file, prod);
}
}catch (IOException e){
}
}
private static void rewrite(File file, List<String> prodList) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(String record : prodList){
writer.write(record);
writer.newLine();
}
writer.close();
}catch (IOException e){
e.printStackTrace();
}
}
private static void addProd2List(File file, Product prod){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
String prodRecord = prod.getProdId() + prod.getProdName() + prod.getPrice() + prod.getQuantity();
writer.write(prodRecord);
writer.close();
}catch (IOException e){
e.printStackTrace();
}
}
private static void setMaxId(List<String> prodList, Product prod) {
String maxId = "123456789";
if (prodList.size() != 0) {
maxId = "";
String lastRecord = prodList.get(prodList.size() - 1);
for(int i = 0;Character.isDigit(lastRecord.charAt(i)); i++){
maxId += lastRecord.charAt(i);
}
}
prod.setProdId(maxId);
}
private static String setPadding(int len, String field){
int counter = field.length();
while(counter < len) {
field += " ";
counter++;
}
if(counter > len){
field = field.substring(0, len);
}
return field;
}
}