日期:2014-05-16  浏览次数:20336 次

jsp 读写文件

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%??

??????? String secondFileName = "c:\\db.properties";
??????? String thirdFileName = "c:\\read.txt";
??????? File fileObject = new File(thirdFileName);
??????? FileInputStream inStream;
??????? FileOutputStream outStream;
??????? try
??????? {
??????????? fileObject.createNewFile();
??????????? inStream = new FileInputStream(secondFileName);
??????????? outStream = new FileOutputStream(thirdFileName);
??????????? copyContent(inStream, outStream);
??????? }catch(FileNotFoundException e)
??????? {
??????????? e.printStackTrace();
??????? }catch(IOException e)
??????? {
??????????? e.printStackTrace();
??????? }

?FileReader fr=new FileReader("c:\\read.txt");
?BufferedReader br=new BufferedReader(fr);
?
?String Line=br.readLine();
?
?while(Line!=null){
? out.println(Line + "<br>");
? Line=br.readLine();
?}
?br.close();
?fr.close();
???
%>
<%!
?? public? static void copyContent(FileInputStream inObj, FileOutputStream outObj)
?? {
??????? int copyLen;
??????? byte[] copyBuf = new byte[1024];
??????? try
??????? {
??????????? while ((copyLen = inObj.read(copyBuf, 0, 1024)) != -1)
??????????? {
??????????????? String copyStr = new String(copyBuf);
??????????????? System.out.println(copyStr);
??????????????? outObj.write(copyBuf, 0, copyLen);
??????????? }
??????????? System.out.println("ok");
??????? }catch(IOException e)
??????? {
??????????? System.out.println("error: " + e);
??????? }
??? }
%>