自己写的在JSP页面得到文件的服务器路径,并对XML文件进行读写(以对Tomcat的conf目录中的context.xml文件为例)
http://blog.csdn.net/songylwq/article/details/5771893
//获取服务器根目录
String serPath = this.getClass().getResource("/").getPath();
int indTom = serPath.indexOf("Tomcat");
String be = serPath.substring(0,indTom);
//处理后得到context.xml文件的绝对路径
String conf = be + serPath.substring(serPath.indexOf("Tomcat"))
.substring(0,serPath.substring(serPath.indexOf("Tomcat")).indexOf("/"))
+ "/conf/context.xml";
//将路径中符号转变成空格,这样得到的conf就是文件的绝对路径
conf = conf.replaceAll("%20", " ");
如何用JSP读取文件夹里的全部文件名及大小类型
http://bbs.chinaunix.net/thread-651343-1-1.htmlString path = "./"; //路径
File f = new File(path);
if (!f.exists())
{
out.println(path+" not exists");
return;
}
File fa[] = f.listFiles();
for(int i=0;i<fa.length;i++)
{
File fs = fa[i];
if (fs.isDirectory())
{
out.println(fs.getName()+" [目录]<br />");
}else{
out.println(fs.getName()+"<br />");
}
}
JSP读取TXT文件两例
http://www.java3z.com/cwbwebhome/article/article5/5775.html
String path = request.getRealPath("/temp"); //取得目录在服务器端的实际位置
//out.println(path+"<br>");
FileWriter fw = new FileWriter(path + "\\File.txt"); //建立FileWrite对象,并设定由fw对象变量引用
//将字串写入文件
fw.write("大家好!");
fw.write("祝大家学习jsp愉快!");
fw.write("希望大家能够深入了解jsp技术!");
fw.close(); //关闭文件
FileReader fr = new FileReader(path + "\\File.txt"); //建立FileReader对象,并设定由fr对象变量引用
BufferedReader br = new BufferedReader(fr); //建立BufferedReader对象,并设定由br对象变量引
String Line = br.readLine(); //读取一行数据
out.println(Line + "<BR>"); //输出读取得的数据
br.close(); //关闭BufferedReader对象
fr.close(); //关闭文件