如何生成TXT文件?

XmanYZ 2003-08-18 03:09:39
在我的页面上,设置个按钮,点击按钮能生成txt文件?
...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuzhan820 2003-09-06
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "zsc104.swf".toString();
//读到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
fuzhan820 2003-09-06
  • 打赏
  • 举报
回复
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
int bytesum=0;
int byteread=0;
//打开数据库
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得数据库中的数据
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();

将数据库中的数据读到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");

byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
stonewang 2003-08-19
  • 打赏
  • 举报
回复
XmanYZ(司徒游龙) :
1、byte[] b = str.getBytes();--得到传入的str字符串,作为文件的内容(如:"i'm a boy")
2、url--------------------------创建文件的全路径(如:"d:/temp/aFile.txt")
allan1031 2003-08-19
  • 打赏
  • 举报
回复
生成在本地?关注~~~~
ticlej 2003-08-18
  • 打赏
  • 举报
回复
你把文本传回服务器,然后用 yuanz62提供的方法写道文件就可以了.
如果是写到客户端,建议你还是直接从服务器上下载比较好.否则涉及到一堆安全之类的问题.
public 中返回的是什么样的东西?
你指得是public static void main(String[] args) throws IOException这个方法吗?
他不返回任何信息.要不生成文件成功,要么throw exception
XmanYZ 2003-08-18
  • 打赏
  • 举报
回复
我是菜鸟

byte[] b = str.getBytes();

这句不懂

还有就是存盘路径是不是就是url?
stonewang 2003-08-18
  • 打赏
  • 举报
回复
1、生成txt
public static void testWrite(String str,String url)
{
FileOutputStream out;
try {
out = new FileOutputStream(url);
byte[] b = str.getBytes();
out.write(b);
}
catch (Exception e) {
e.printStackTrace();
}
}
(jdk1.4通过)
2、如果是在跑ie的applet,请加数字签名(不清楚可以问我)
XmanYZ 2003-08-18
  • 打赏
  • 举报
回复
文本内容不是定义好的

是从页面上传到后台的

通过按钮来调用javabean

public 中返回的是什么样的东西?
Leemaasn 2003-08-18
  • 打赏
  • 举报
回复
可以使用服务器生成,通过下载到本地
XmanYZ 2003-08-18
  • 打赏
  • 举报
回复
在本地生成TXT呢?
luo_wen_jie 2003-08-18
  • 打赏
  • 举报
回复
但txt文件是在服务器端生成的
yuanz62 2003-08-18
  • 打赏
  • 举报
回复
public static void main(String[] args) throws IOException
{
File outputFile=new File("d:\\zhyuan\\Dst.txt");
FileOutputStream out=new FileOutputStream(outputFile);
String c="abcdefg....";
out.write(c);
out.close();
}

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧