1. 程式人生 > >在Action中獲取檔案的路徑,讀寫檔案

在Action中獲取檔案的路徑,讀寫檔案

ActionContext ac = ActionContext.getContext(); 

ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT); 

String x = sc.getRealPath("/");

接著進入專案的工作空間,workspace1\bams\bin\dagl\WEB-INF\config\下找到了我們剛剛在eclipse環境中新增的

default.key.

in = new FileInputStream(x+"WEB-INF/config/default.key");

br = new BufferedReader(new InputStreamReader(in));

String code = br.readLine();

2)java專案向某一個檔案中寫一個字串

File keyFile = new File("default.key");
if (!keyFile.exists()) {
keyFile.createNewFile();
}
FileWriter fw = new FileWriter(keyFile);
fw.write(finalcode);
fw.close();