好的,我有一个我编写的 Web 应用程序,我可以从一个文件中读取,该文件包含在我的源包中的一个名为“text”的新文件夹中。我正在尝试写入同一个文件,但它不起作用。它从不写入文件。这是我的两种方法的代码:
public void fillItems() throws IOException{
String path = "/OBrien_PROJ2/text/catalog.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(path)));
String text = null;
while ((text=br.readLine())!=null){
String[] itemArray = text.split(",");
// you might want to check array size
items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));
}
br.close();
}
public void createNewItem(String iD, String name, String description, String price, String quantity) throws IOException{
String path = "/OBrien_PROJ2/text/catalog.txt";
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
bw.write(iD + "," + name + "," + description + "," + price + "," + quantity);
items.add(new ItemBean (iD, name, description, price, quantity));
bw.flush();
bw.close();
}
如果重要的话,我正在使用 NetBeans