文本文件的jsp创建、写入、读出....高手请进!!

arnoldyu 2002-04-15 01:43:52
文本文件的jsp创建、写入、读出....高手请进!!
...全文
84 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hotenM 2002-04-15
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import ="java.sql.*" %>
<%@ page import ="java.util.*,java.io.*" %>
<%@include file="/public/getConn.jsp"%>
<%@include file="/public/checksession.jsp"%>
<html>
<head>
<title>抽奖报表</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../public/size.css" type="text/css">
</head>
<%
String type = "CJ";
Statement stmt = conn.createStatement();
ResultSet rs = null;
String strSql ="";
String code = request.getParameter("code2");
String time ="";
String fileName = "";
String allFileName = "";
%>
<%//生成一个文件名,写入文件并提供下载
java.util.Calendar date = java.util.Calendar.getInstance();
int intMSecond = date.get(Calendar.MILLISECOND);
String month,day,hour,min,second;
if ((date.get(Calendar.MONTH) + 1) < 10)
month = "0" + String.valueOf(date.get(Calendar.MONTH) + 1);
else
month = String.valueOf(date.get(Calendar.MONTH) + 1);
if (date.get(Calendar.DATE) < 10)
day = "0" + String.valueOf(date.get(Calendar.DATE));
else
day = String.valueOf(date.get(Calendar.DATE));
if (date.get(Calendar.HOUR) < 10)
hour = "0" + String.valueOf(date.get(Calendar.HOUR));
else
hour = String.valueOf(date.get(Calendar.HOUR));
if (date.get(Calendar.MINUTE) < 10)
min = "0" + String.valueOf(date.get(Calendar.MINUTE));
else
min = String.valueOf(date.get(Calendar.MINUTE));
if (date.get(Calendar.SECOND) < 10)
second = "0" + String.valueOf(date.get(Calendar.SECOND));
else
second = String.valueOf(date.get(Calendar.SECOND));
time = String.valueOf(date.get(Calendar.YEAR)) + month + day + hour + min + second;
fileName = code+time+".bb";
allFileName = "defaultroot\\cj\\"+fileName;
PrintWriter bb = new PrintWriter(new FileWriter(allFileName, true), true);
bb.println("\t\t\t\t说明:奖项0表示是特等奖,奖项9表示纪念奖。\n");
bb.println("奖项\t\t中奖人数");
strSql ="select grade,count(*) from sms_userinfo where code ='"+code+"' and grade <> 'U' group by grade";
rs = stmt.executeQuery(strSql);
String strLine;
while(rs.next()){
strLine = rs.getString(1)+"\t\t"+rs.getString(2);
bb.println(strLine);
}
bb.close();

%>
<body>
<div align="center"><a href="<%=fileName%>">下载报表(<font size="2">请右键点击并另存为</font>)</a></div>
</body>
</html>
给你个例子
回复
Times2001 2002-04-15
import java.io.*;
import java.util.*;
public class CreateFile
{
public static void main(String[] args)
{
String str="hello";
try{
FileWriter fileOut = new FileWriter("outfile.txt");
fileOut.write(str);
fileOut.close();
System.out.println("Create File is ok");
} catch(Exception e) {
e.printStackTrace();
}
}
}

创建
回复
Andrawu 2002-04-15
import java.io.*;
import java.util.*;
import java.text.*;

public class WriteToFile
{
public void writeToFile()
{
StringBuffer allContent = new StringBuffer();
String fSrcName="c:\\srcfile.txt";
String fObjName="c:\\newfile.txt";
try
{
FileReader fr = new FileReader(fSrcName) ;
BufferedReader br = new BufferedReader(fr) ;
String sLine = br.readLine() ;
while (sLine!=null){
allContent.append(sLien+"\n") ;
}
sLine = br.readLine() ;
}
PrintWriter pw = new PrintWriter(new FileWriter(fObjName, false), true);
pw.println(allContent.toString());
pw.flush() ;
pw.close() ;
fr.close() ;
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}

你自己改为jsp试试吧。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告