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("该文件创建成功");
}
运行可执行文件:Runtime(好像只能执行exe,txt就不行了)
File file=new File("D:\\酷狗\\KuGou2012\\","KuGou.exe");
try{
Runtime ce=Runtime.getRuntime();
ce.exec(file.getPath());
out.print("执行文件成功");
}
catch(Exception e){
out.print(e);
}