Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修改类以将异常写入文本文件_Java_Exception_Printwriter - Fatal编程技术网

Java 修改类以将异常写入文本文件

Java 修改类以将异常写入文本文件,java,exception,printwriter,Java,Exception,Printwriter,我真的需要一个指向正确方向的点。我不明白如何达到它的要求 修改ProductMainApp类,使其在ProductTextFile类中的addProduct和deleteProduct mwthod返回flase值时做出适当响应 修改ProductTextFile类,使其将异常写入tex文件名errorLog.txt,而不是将其打印到控制台。为此,添加一个名为printToLogFile的方法,该方法接受IOException作为参数。此方法应将两条记录附加到日志文件中:一条指示异常发生的日期和

我真的需要一个指向正确方向的点。我不明白如何达到它的要求

修改ProductMainApp类,使其在ProductTextFile类中的addProduct和deleteProduct mwthod返回flase值时做出适当响应

修改ProductTextFile类,使其将异常写入tex文件名errorLog.txt,而不是将其打印到控制台。为此,添加一个名为printToLogFile的方法,该方法接受IOException作为参数。此方法应将两条记录附加到日志文件中:一条指示异常发生的日期和时间,另一条包含有关异常的信息

修改getProducts和saveProducts方法,以便在发生错误时调用printToLogFile方法

以下是PrintTextFile:

import java.util.*;
import java.io.*;
import java.nio.file.*;

public final class ProductTextFile implements ProductDAO
{
private ArrayList<Product> products = null;
private Path productsPath = null;
private File productsFile = null;

private final String FIELD_SEP = "\t";

public ProductTextFile()
{
productsPath = Paths.get("products.txt");
productsFile = productsPath.toFile();
products = this.getProducts();
}

public ArrayList<Product> getProducts()
{
// if the products file has already been read, don't read it again
if (products != null)
return products;

products = new ArrayList<>();

if (Files.exists(productsPath)) // prevent the FileNotFoundException
{
try
{
if (true)
{
// throw new IOException();
}
// open the input stream
 BufferedReader in =
 new BufferedReader(
 new FileReader(productsFile));

 // read all products stored in the file
 // into the array list
 String line = in.readLine();
while(line != null)
{
 String[] columns = line.split(FIELD_SEP);
 String code = columns[0]; 
 String description = columns[1];
 String price = columns[2];

Product p = new Product(
code, description, Double.parseDouble(price));

products.add(p);

line = in.readLine();
}

// close the input stream
 in.close();
 }
 catch(IOException e)
 {
 //System.out.println(e);

return null;
}
}
return products;
}

public Product getProduct(String code)
{
for (Product p : products)
{
if (p.getCode().equals(code))
return p;
}
return null;
}

public boolean addProduct(Product p)
{
products.add(p);
return this.saveProducts();
}

public boolean deleteProduct(Product p)
{
products.remove(p);
return this.saveProducts();
}

public boolean updateProduct(Product newProduct)
{
// get the old product and remove it
Product oldProduct = this.getProduct(newProduct.getCode());
int i = products.indexOf(oldProduct);
products.remove(i);

// add the updated product
products.add(i, newProduct);

return this.saveProducts();
}

private boolean saveProducts()
{
   try
{
// open the output stream
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(productsFile)));

// write all products in the array list
// to the file
for (Product p : products)
{
out.print(p.getCode() + FIELD_SEP);
out.print(p.getDescription() + FIELD_SEP);
out.println(p.getPrice());
}

// close the output stream
out.close();
}
catch(IOException e)
{
System.out.println(e);
return false;
}

return true;
}

}
import java.util.*;
导入java.io.*;
导入java.nio.file.*;
公共最终类ProductTextFile实现ProductDAO
{
私有ArrayList产品=null;
私有路径productsPath=null;
私有文件productsFile=null;
私有最终字符串字段_SEP=“\t”;
公共产品文本文件()
{
productsPath=path.get(“products.txt”);
productsFile=productsPath.toFile();
products=this.getProducts();
}
公共阵列列表getProducts()
{
//如果产品文件已被读取,请不要再次读取
if(产品!=null)
退货产品;
products=新的ArrayList();
if(Files.exists(productsPath))//防止FileNotFoundException
{
尝试
{
如果(真)
{
//抛出新IOException();
}
//打开输入流
缓冲读取器=
新的缓冲读取器(
新文件读取器(productsFile));
//读取文件中存储的所有产品
//进入数组列表
String line=in.readLine();
while(行!=null)
{
String[]columns=line.split(FIELD_SEP);
字符串代码=列[0];
字符串说明=列[1];
字符串价格=列[2];
产品p=新产品(
代码,描述,Double.parseDouble(price));
产品.加入(p);;
line=in.readLine();
}
//关闭输入流
in.close();
}
捕获(IOE异常)
{
//系统输出打印ln(e);
返回null;
}
}
退货产品;
}
公共产品getProduct(字符串代码)
{
对于(产品p:产品)
{
if(p.getCode().equals(code))
返回p;
}
返回null;
}
公共布尔addProduct(产品p)
{
产品.加入(p);;
返回此.saveProducts();
}
公共布尔删除产品(产品p)
{
产品。去除(p);
返回此.saveProducts();
}
公共布尔更新产品(产品新产品)
{
//获取旧产品并将其移除
Product oldProduct=this.getProduct(newProduct.getCode());
int i=products.indexOf(旧产品);
产品。移除(i);
//添加更新的产品
产品。添加(i,新产品);
返回此.saveProducts();
}
私有布尔存储产品()
{
尝试
{
//打开输出流
PrintWriter out=新的PrintWriter(
新缓冲写入程序(
新文件编写器(productsFile));
//写入阵列列表中的所有产品
//归档
对于(产品p:产品)
{
打印(p.getCode()+字段_SEP);
打印(p.getDescription()+字段_SEP);
out.println(p.getPrice());
}
//关闭输出流
out.close();
}
捕获(IOE异常)
{
系统输出打印ln(e);
返回false;
}
返回true;
}
}
以下是ProductMainApp:

import java.util.Scanner;
import java.util.ArrayList;

public class ProductMaintApp implements ProductConstants
{
// declare two class variables
private static ProductDAO productDAO = null;
private static Scanner sc = null;

public static void main(String args[])
{
// display a welcome message
System.out.println("Welcome to the Product Maintenance application\n");

// set the class variables
productDAO = DAOFactory.getProductDAO();
sc = new Scanner(System.in);

// display the command menu
displayMenu();

// perform 1 or more actions
String action = "";
while (!action.equalsIgnoreCase("exit"))
{
// get the input from the user
action = Validator.getString(sc,
"Enter a command: ");
System.out.println();

if (action.equalsIgnoreCase("list"))
displayAllProducts();
else if (action.equalsIgnoreCase("add"))
{
addProduct();

}

else if (action.equalsIgnoreCase("del") ||        action.equalsIgnoreCase("delete"))
deleteProduct();
else if (action.equalsIgnoreCase("help") || action.equalsIgnoreCase("menu"))
displayMenu();
else if (action.equalsIgnoreCase("exit") || action.equalsIgnoreCase("quit"))
System.out.println("Bye.\n");
else
System.out.println("Error! Not a valid command.\n");
}
}

public static void displayMenu()
{
System.out.println("COMMAND MENU");
System.out.println("list - List all products");
System.out.println("add - Add a product");
System.out.println("del - Delete a product");
System.out.println("help - Show this menu");
System.out.println("exit - Exit this application\n");
}

public static void displayAllProducts()
{
System.out.println("PRODUCT LIST");

ArrayList<Product> products = productDAO.getProducts();
Product p = null;
StringBuilder sb = new StringBuilder();

if (productDAO.getProducts().equals(null))
{
System.out.println("Value Null");
System.exit(0);
}   

for (int i = 0; i < products.size(); i++)
{
p = products.get(i);

sb.append(StringUtils.padWithSpaces(
p.getCode(), CODE_SIZE + 4));
sb.append(StringUtils.padWithSpaces(
p.getDescription(), DESCRIPTION_SIZE + 4));
sb.append(
p.getFormattedPrice());
sb.append("\n");
}
System.out.println(sb.toString());
}

public static void addProduct()
{
String code = Validator.getString(
sc, "Enter product code: ");
String description = Validator.getLine(
sc, "Enter product description: ");
double price = Validator.getDouble(
sc, "Enter price: ");

Product product = new Product();
product.setCode(code);
product.setDescription(description);
product.setPrice(price);
productDAO.addProduct(product);


System.out.println();
System.out.println(description
+ " has been added.\n");
}

public static void deleteProduct()
{
String code = Validator.getString(sc,
"Enter product code to delete: ");

Product p = productDAO.getProduct(code);

System.out.println();
if (p != null)
{
productDAO.deleteProduct(p);
System.out.println(p.getDescription()
+ " has been deleted.\n");
}
else
{
System.out.println("No product matches that code.\n");
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
公共类ProductMaintApp实现ProductConstants
{
//声明两个类变量
私有静态ProductDAO ProductDAO=null;
专用静态扫描仪sc=null;
公共静态void main(字符串参数[])
{
//显示欢迎信息
System.out.println(“欢迎使用产品维护应用程序”\n);
//设置类变量
productDAO=DAOFactory.getProductDAO();
sc=新扫描仪(系统英寸);
//显示命令菜单
显示菜单();
//执行一个或多个操作
字符串动作=”;
而(!action.equalsIgnoreCase(“退出”))
{
//从用户那里获取输入
action=Validator.getString(sc,
“输入命令:”;
System.out.println();
if(action.equalsIgnoreCase(“列表”))
显示所有产品();
else if(action.equalsIgnoreCase(“add”))
{
addProduct();
}
else if(action.equalsIgnoreCase(“del”)| | action.equalsIgnoreCase(“delete”))
删除产品();
else if(action.equalsIgnoreCase(“帮助”)| | action.equalsIgnoreCase(“菜单”))
显示菜单();
else if(action.equalsIgnoreCase(“退出”)| | action.equalsIgnoreCase(“退出”))
System.out.println(“Bye.\n”);
其他的
System.out.println(“错误!不是有效的命令。\n”);
}
}
公共静态无效显示菜单()
{
System.out.println(“命令菜单”);
System.out.println(“列表-列出所有产品”);
System.out.println(“添加-添加产品”);
System.out.println(“del-删除产品”);
System.out.println(“帮助-显示此菜单”);
System.out.println(“退出-退出此应用程序\n”);
}
公共静态void displayAllProducts()
{
System.out.println(“产品列表”);
ArrayList products=productDAO.getProducts();
乘积p=null;
StringBuilder sb=新的StringBuilder();
if(productDAO.getProducts().equals(null))
{
System.out.println(“值空”);
系统出口(0);
}   
对于(int i=0;i