java(JSP)讀寫檔案操作
JAVA讀寫指定檔案內容,工作中用到的,放在這裡給大家分享一下.
以下為讀寫檔案類:
package com.hexun.wap.zgpack;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* @author Administrator
*/
public class BasicFile
{
public void WriteFile(String path,String content) // 寫檔案類,引數: 檔案路徑,寫入內容
{
try
{
FileWriter fw = new FileWriter(path,true);
fw.write("/n"+content);
fw.flush();
fw.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public ArrayList ReadFile(String path) // 讀檔案 引數: 檔案路徑.
{
ArrayList list = new ArrayList();
try
{
String file = path;
BufferedReader br = new BufferedReader(new FileReader(file));
String str = "";
String free = "";
while((str = br.readLine()) != null)
{
list.add(str); //將檔案放入 list ,以便JSP頁面呼叫
}
br.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
}
以下為JSP內容:
<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import = "java.io.*,java.util.*,com.hexun.wap.zgpack.*;" %>
<html>
<head><title>Basic News Page</title><style type="text/css"><!--td {font-size:12px}--></style></head>
<body>
<table width=80% border=0 bgcolor=000000 cellspacing=1 cellpadding=1 align=center><tr><td bgcolor=FF7Dff>Title</td><td bgcolor=FF7Dff>Time / Author</td><td bgcolor=FF7Dff>Content</td></tr>
<%
String t = request.getParameter("t")!=null?request.getParameter("t"):"";
String s = request.getParameter("s")!=null?request.getParameter("s"):"";
String d = request.getParameter("detail")!=null?request.getParameter("detail"):"";
String flag = request.getParameter("flag")!=null?request.getParameter("flag"):"false";
if(flag.equals("true"))
{
/*
byte[] b = t.getBytes("ISO8859_1");
String str = new String(b,"GBK");
byte[] b2 = s.getBytes("ISO8859_1");
String str2 = new String(b2,"GBK");
byte[] b3 = d.getBytes("ISO8859_1");
String str3 = new String(b3,"GBK");
*/
new BasicFile().WriteFile("/usr/local/stockdata/basictitle.txt",t);
//new BasicFile().WriteFile("/usr/local/stockdata/btit.txt",t);
new BasicFile().WriteFile("/usr/local/stockdata/basictime.txt",s);
//new BasicFile().WriteFile("/usr/local/stockdata/btim.txt",s);
new BasicFile().WriteFile("/usr/local/stockdata/basicinfo.txt",d);
//new BasicFile().WriteFile("/usr/local/stockdata/bi.txt",d);
}
ArrayList al = new BasicFile().ReadFile("/usr/local/stockdata/basictitle.txt");
//ArrayList al = new BasicFile().ReadFile("/usr/local/stockdata/btit.txt");
ArrayList time = new BasicFile().ReadFile("/usr/local/stockdata/basictime.txt");
//ArrayList time = new BasicFile().ReadFile("/usr/local/stockdata/btim.txt");
ArrayList info = new BasicFile().ReadFile("/usr/local/stockdata/basicinfo.txt");
//ArrayList info = new BasicFile().ReadFile("/usr/local/stockdata/bi.txt");
int a_size = al.size();
for(int i=0; i<a_size;i++)
{
out.println("<tr><td width=10% height=30 align=left bgcolor=FF7D00>"+al.get(i)+"</td>");
out.println("<td width=10% align=left bgcolor=FF7D00>"+ time.get(i)+"</td>");
out.println("<td width=60% align=left bgcolor=FF7D00>"+info.get(i)+"</td>");
out.println("</tr>");
}
%>
</table>
<br/>
<br/>
<table width=80% border=0 bgcolor=#FFE4C4 cellspacing=1 cellpadding=1 align=center>
<tr><td colspan=2>Add Basic News :</td></tr>
<tr><form action="OperBasic.jsp">
<td>Title:</td><td><input type="text" name="t" value="<%=t%>" maxlength="60" format="*N" style="border-width:1; border-color:black; border-style:solid;"/></td></tr>
<tr><td>Title:</td><td><input type="text" name="s" value="<%=s%>" maxlength="20" format="*N" style="border-width:1; border-color:black; border-style:solid;"/></td></tr>
<tr><td>Content:</td><td><textarea name="detail" rows="10" cols="67" style="border-width:1; border-color:black; border-style:solid;"></textarea></td>
<tr><td colspan=2 align=center><input type="submit" name="submit" value="Add"/></td>
<input type="hidden" name="flag" value="true" maxlength="20" format="*N"/>
</form>
</tr>
</table>
</body>
</html>