好的,我有一個我編寫的Web應用程序,我可以從我的源代碼包中包含的文件中讀取一個名爲「text」的新文件夾。我正在嘗試寫入相同的文件,但它不起作用。它從不寫入文件。這裏是我的兩個方法的代碼:從文件中讀取並寫入同一個文件的Java Web應用程序
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
我沒有寫入數據庫的選項。沒有辦法寫入webapp中包含的文本文件? – ssgtob1 2013-04-20 20:17:20
簡答題:不。你會發現一個.exe文件自己寫東西是合乎邏輯的嗎?戰爭文件一樣。 – 2013-04-20 20:18:43