<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<html>
<body>
<% //字符流FileWriter类写入
// request.setCharacterEncoding("gb2312");
////该句相当重要,该语句的作用是告诉jsp引擎,页面参数采用的是中文编码,由jsp引擎自动完成字符集转换。
////contentType="text/html;charset=gb2312"头中c不能大写,否则也是乱码,能解决写入文件时乱码的问题
////只能这样写才能解决页面显示和写入文件时乱码的问题
// String str1=request.getParameter("text1");
// int len=str1.length();
// char te[]=new char[len]; //text1.getChars(0,text1.length(),te[],0);
// for(int i=0;i<len;i++){te[i]=str1.charAt(i);}//将字符串存入字符数组中
// try
// {
// File file1=new File("d:/","temp.txt");
// if(file1.exists()) file1.delete();//可删除,这里为调试方便
// if(file1.exists())
// { out.print("文件夹已存在!"); }
// else
// { file1.createNewFile();
// out.print("文件创建成功!");
// }
// out.print("<br>"+"你的输入为:"+str1);
// FileWriter fw_1=new FileWriter(file1);
// fw_1.write(te,0,len);//fw_1.write(te);也可以
// fw_1.close();
// }
// catch(Exception e)
// { out.print(e); }
%>
<% //带BufferWriter类写入
//该句相当重要,该语句的作用是告诉jsp引擎,页面参数采用的是中文编码,由jsp引擎自动完成字符集转换。
//contentType="text/html;charset=gb2312"头中c不能大写,否则也是乱码,能解决写入文件时乱码的问题
//只能这样写才能解决页面显示和写入文件时乱码的问题
request.setCharacterEncoding("gb2312");
String str1=request.getParameter("text1");
try
{ File file1=new File("d:/","temp.txt");
if(file1.exists()) file1.delete();
if(file1.exists())
{ out.print("文件夹已存在!"); }
else
{ file1.createNewFile();//创建文件
out.print("文件创建成功!");
}
out.print("<br>"+"你的输入为:"+str1);
FileWriter fw1=new FileWriter(file1);
BufferedWriter bufw1=new BufferedWriter(fw1);
bufw1.write(str1); //使用BufferedWriter来写入,这样可以写入字符串;
bufw1.flush();//刷新流,强制输出缓冲区中所有字符;
bufw1.close();
fw1.close();
}
catch(IOException e)
{ out.print(e); }
%>
<%
// try
// { File file2=new File("d:/","temp.txt");
// if(file2.exists())
// { FileReader fr1=new FileReader(file2);
// BufferedReader bufr1=new BufferedReader(fr1);
// String str2=bufr1.readLine();
// out.print("<br>"+"文本内容为:"+str2);
// bufr1.close();
// fr1.close();
// }
// else
// { out.print("文件不存在"); }
// }
// catch(IOException e)
// { out.print(e); }
%>
<% //字节流FileInputStream示例,FileOutputStream类似,只可以英文
try
{ File file2=new File("d:/","temp.txt");
if(file2.exists())
{ FileInputStream fr1=new FileInputStream(file2);
int aa=fr1.available();//返回从文件中读取的字节数目,只有FileInputStream有此方法
byte temp2[]=new byte[aa];
fr1.read(temp2,0,aa);
String str2=new String(temp2,0);//将字符数组存入字符串
out.print("<br>"+"文本内容为:"+str2);
fr1.close();
}
else{ out.print("文件不存在"); }
}
catch(IOException e)
{ out.print(e);}
%>
</body>
</html>
没有合适的资源?快使用搜索试试~ 我知道了~
jsp简单例子-文件操作
共2个文件
jsp:2个
需积分: 10 18 下载量 2 浏览量
2008-11-21
13:45:35
上传
评论
收藏 2KB RAR 举报
温馨提示
jsp的FileWriter和FileReader示例,使用多个方法示例,对文件读写,可以解决中文乱码问题
资源推荐
资源详情
资源评论
收起资源包目录
file.rar (2个子文件)
outin1.jsp 591B
outin2.jsp 3KB
共 2 条
- 1
资源评论
xiekingjr
- 粉丝: 3
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功