ホーム > java > Strutsでファイル( FormFile )を書いたり読んだり

Strutsでファイル( FormFile )を書いたり読んだり

2009 年 7 月 17 日 tdtsh
このエントリーをはてなブックマークに追加
はてなブックマーク - Strutsでファイル( FormFile )を書いたり読んだり
Share on Facebook
Share on FriendFeed
Share on GREE
Share on reddit
Share on LinkedIn

FormFile ( org.apache.struts.upload.FormFile ) をファイルに書き出す

private void _saveFormFile(String filePath, FormFile upfile) throws Exception
{
	InputStream is = upfile.getInputStream();
	BufferedInputStream bis = new BufferedInputStream(is);
	FileOutputStream fos = new FileOutputStream(filePath);
	BufferedOutputStream bos = new BufferedOutputStream(fos);
	int read = 0;
	while ((read = bis.read()) != -1) {
		bos.write(read);
	}
	bos.flush();
	bis.close();
	bos.close();
}

書き出したファイルをArrayListに格納する

private ArrayList _readFile(String filePath) throws Exception
{
	File file = new File(filePath);
	FileInputStream fis = new FileInputStream(filePath);
	InputStreamReader isr = new InputStreamReader(fis, "utf-8"));
	BufferedReader br = new BufferedReader(isr);
	ArrayList lines = new ArrayList();
	String line = br.readLine();
	while(line != null){
		lines.add(line);
		line = br.readLine();
	}
	br.close();
	return lines;
}

Listに格納されたテキストをファイルに書き出す

private void _saveFile(String filePath, List lines) throws Exception
{
	File file = new File(filePath);
	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath),"utf-8"));
	for (Iterator i = lines.iterator(); i.hasNext();) {
		bw.write((String)i.next());
		bw.newLine();
		bw.flush();
	}
	bw.close();
	return;
}

参考サイト
http://www.ingrid.org/jajakarta/struts/struts1.0/ja/target/documentation/api/index.html
http://www.javaroad.jp/opensource/js_struts20.htm

カテゴリー: java タグ:
コメントは受け付けていません。