站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题如何用jsp打开文件,并在文件里写内容

如何用jsp打开文件,并在文件里写内容

摘要: 如何用jsp打开文件,并在文件里写内容


如何用jsp打开一个名为ww.txt的文件,并在文件内写入“你好,欢迎来如jsp”内容,小弟初学,在现等


java程序里怎么写jsp里就怎么写,完全一样的

最好的方法是写个后台的读取文件的类,jsp里import一下直接用,


java得FilterOutputStream!


小弟初学,请给出代码


我给你写代码 你参考一下 别忘了鼓励哦!!
if ( order.getContract_attached_file_URL() != null )
order_attached_file_url = get_Url ;
else
order_attached_file_url = "" ;

if ( order_attached_file_url != null && ! order_attached_file_url.trim().equals( "" ) ) {
int pos = order_attached_file_url.lastIndexOf(".") ;
if ( pos > -1 )
tmp_ext = order_attached_file_url.substring( pos ) ;
else
tmp_ext = ".txt" ;
}

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar now = Calendar.getInstance(); //get CurrDate
tmp_fn = "Order" + order_id + "_" + formatter.format(now.getTime() ) ;
tmp_fn = tmp_fn + tmp_ext ;
tmpLink = "/ecatic/temp/" + tmp_fn ;
tmpRealPathFile = request.getRealPath( tmpLink ) ;
resultAttachFile = ordersheet.get_orderHeader().getOrderAttach( conn, tmpRealPathFile ,get_Url ,order_id) ;

//In the server, file name is : tmpRealPathFile
// open the temp file, and put data to out
File file = new File( tmpRealPathFile );
response.setContentType("application/octet-stream");
//response.setContentType("application/vnd.ms-excel") ;

response.setHeader("Content-disposition" , "attachment; filename=" + file.getName() );
//response.setHeader("content-disposition","attachment;filename=/""+ FileName +".xls/"");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;

bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
bos.flush();
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
bos.close() ;
bis.close() ;

file.delete() ;


不是把大哥,我只是要求写入几行简单的子,你给我闹这么乱起东东,在线再等 呜呜


难道没有人嘛? 再顶一下


晕倒
你还嫌代码长啊~~~~~~~~


//给你一个现成的吧

private static void write2File(String content, String fileName)
{
if (content == null || fileName == null || fileName.trim().length()==0)
{
throw new IllegalArgumentException("- [error]: 文件内容和文件名一个都不能少!");
}

BufferedReader reader = null;
BufferedWriter writer = null;

try
{
String filePath = "c:/" + fileName;
System.out.println(filePath);
File file = new File(filePath);

if (!file.exists())
{
file.createNewFile();
}

reader = new BufferedReader(new StringReader(content));
writer = new BufferedWriter(new FileWriter(file));
String tempStr = null;
while((tempStr=reader.readLine()) != null )
{
writer.write(tempStr);
writer.flush();
writer.newLine();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{

try
{
if (reader != null)
{
reader.close();
}

if (writer != null)
{
writer.close();
}
}

catch (IOException e)
{
e.printStackTrace();
}
}
}


<%@ page language="java" pageEncoding="gb2312"%>
<%@ page import ="java.io.*"%>

<head>

文件操作
</head>
<body>

jsp 中对文件的操作,其实跟java 一样




你先建立一个文件,放在%TOMCAT_HOME%/webapps/ROOT文件夹下。比如

test.txt,输入如下内容:

测试一下读取文件操作



<%
String filename="test.txt";
String tempPath=request.getRealPath("/");
String file=tempPath+"//"+filename;
File testFile=new File(file);
BufferedReader fin=new BufferedReader(new FileReader(testFile));
String str=null;
out.println("文件内容如下:
");

while((str=fin.readLine())!=null)
{
out.println(str+"
");
}
out.flush();
fin.close();
%>



mark


↑返回目录
前一篇: 用jsp往文件里写内容时出现乱码?
后一篇: 如何用jsp调用编译好的c语言程序?