标签:
readwrite.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@page import="java.io.*,java.lang.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘readwrite.jsp‘ starting page</title> </head> <body> <!-- 因为path系统已经定义过了,所以不需要加String --> <% //这里应该包含test.txt所在的路径,因为FileWriter和FileReader里面的参数不能带路径 path=request.getRealPath("")+"\\file"; FileWriter fw=new FileWriter(path+"\\test.txt"); fw.write("您好,世界"+"<br>"); fw.write("我来了"); fw.close(); FileReader fr=new FileReader(path+"\\test.txt"); int c=fr.read(); while(c!=-1){ out.print((char)c); c=fr.read(); if(c==13){ out.print("<br>"); fr.skip(1); fr.read(); } } fr.close(); %> </body> </html>
快速在特定目录下创建文件的方法:
path=request.getRealPath("");
File file=new File(path,"sdf.txt");
if(file.exists()){
out.print("该文件已经存在");
}
else{
file.createNewFile();
out.print("该文件创建成功");
}
标签:
原文地址:http://www.cnblogs.com/tianhengblogs/p/5325301.html