jsp如何在指定的地方创建一个.txt的文件,并向其中写内容和读内容?

simonqsm 2002-01-28 12:17:18
...全文
169 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzwrj 2002-01-29
  • 打赏
  • 举报
回复
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile


public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readFile
simonqsm 2002-01-28
  • 打赏
  • 举报
回复
没人知道吗?

81,092

社区成员

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

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