love fish大鵬一曰同風(fēng)起,扶搖直上九萬里

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          friends

          link

          最新評論

          關(guān)于JSP文件路徑問題 (轉(zhuǎn))

          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>文件的建立、檢查與刪除</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("");
          //out.println(path);
          File f=new File(path,"File.txt");
          //out.println(f);
          //out.println(f.exists());

          if(f.exists()){//檢查File.txt是否存在
          f.delete();//刪除File.txt文件
          out.println(path + "\\File.txt 存在,已刪除。");
          }else{
          f.createNewFile();//在當(dāng)前目錄下建立一個(gè)名為File.txt的文件
          out.println(path + "\\File.txt 不存在,已建立。");//輸出目前所在的目錄路徑
          }
          %>

          目錄的建立/檢查與刪除
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>目錄的建立/檢查與刪除</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("");
          path=path + "\\Sub";//將要建立的目錄路徑
          File d=new File(path);//建立代表Sub目錄的File對象,并得到它的一個(gè)引用
          if(d.exists()){//檢查Sub目錄是否存在
          d.delete();
          out.println("Sub目錄存在,已刪除");
          }else{
          d.mkdir();//建立Sub目錄
          out.println("Sub目錄不存在,已建立");
          }
          %>
          </body>
          </html>


          如何在JSP中處理虛擬目錄
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>JSP中如何處理虛擬目錄</title>
          </head>
          <body>
          取得虛擬目錄對應(yīng)的磁盤路徑<br>
          Web站點(diǎn)主目錄的位置為<font color=#ff0000><%=request.getRealPath("/")%></font><br>
          JSP網(wǎng)頁所在的目錄位置<font color=#ff0000><%=request.getRealPath("./")%></font><br>
          JSP網(wǎng)頁所在目錄上一層目錄的位置<font color=#ff0000><%=request.getRealPath("../")%></font><br>
          </body>
          </html>


          文件屬性的取得
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.util.Date,java.io.*"%>
          <html>
          <head>
          <title>文件屬性的取得</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("/");
          File f=new File(path,"ReadData.txt");
          if(f.exists()){
          %>
          <%=f.getName()%>的屬性如下:<br><br>
          文件長度為:<%=f.length()%>
          <%=f.isFile()?"是文件":"不是文件"%><br>
          <%=f.isDirectory()?"是目錄":"不是目錄"%><br>
          <%=f.canRead()?"可讀取":"不可讀取"%><br>
          <%=f.canWrite()?"可寫入":"不可寫入"%><br>
          <%=f.isHidden()?"是隱藏文件":"不是隱藏文件"%><br>
          文件的最后修改日期為:<%=new Date(f.lastModified())%><br>
          <%
          }else{
          f.createNewFile();//在當(dāng)前目錄下建立一個(gè)名為ReaData.txt的文件
          %>
          <%=f.getName()%>的屬性如下:<br><br>
          文件長度為:<%=f.length()%>
          <%=f.isFile()?"是文件":"不是文件"%><br>
          <%=f.isDirectory()?"是目錄":"不是目錄"%><br>
          <%=f.canRead()?"可讀取":"不可讀取"%><br>
          <%=f.canWrite()?"可寫入":"不可寫入"%><br>
          <%=f.isHidden()?"是隱藏文件":"不是隱藏文件"%><br>
          文件的最后修改日期為:<%=new Date(f.lastModified())%><br>
          <%
          }
          %>
          </body>
          </html>


          取出目錄中文件的方法
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>取出目錄中文件的方法--列出目錄中的文件</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("/");
          File d=new File(path);//建立當(dāng)前目錄中文件的File對象
          File list[]=d.listFiles();//取得代表目錄中所有文件的File對象數(shù)組
          out.println("<font color=#ff0000>" + path + "目錄下的文件:</font><br>");
          for(int i=0;i<list.length;i++){
          if(list<I>.isFile()){
          out.println(list<I>.getName() + "<br>");
          }
          }
          out.println("<br><font color=#ff0000>" + path + "目錄下的目錄:</font><br>");
          for(int i=0;i<list.length;i++){
          if(list<I>.isDirectory()){
          out.println(list<I>.getName() + "<br>");
          }
          }
          %>
          </body>
          </html>


          判斷是否為空白文件
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>判斷是否為空白文件</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("/");
          out.println(path);
          FileReader fr=new FileReader(path + "\\AtEnd.txt");//建立FileReader對象,并實(shí)例化為fr
          //對FileReader類生成的對象使用read()方法,可以從字符流中讀取下一個(gè)字符。
          if(fr.read()==-1)//判斷是否已讀到文件的結(jié)尾
          {
          out.print("AtEnd.txt文件中沒有數(shù)據(jù)<br>");
          }else{
          out.println("AtEnd.txt文件中有數(shù)據(jù)");
          }
          fr.close();
          %>
          </body>
          </html>


          讀取所有的文件數(shù)據(jù)
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*,java.lang.*"%>
          <html>
          <head>
          <title>讀取所有的文件數(shù)據(jù)</title>
          </head>
          <body>
          <%
          String path=request.getRealPath(".");
          FileReader fr=new FileReader(path + "\\ReadData.txt");
          //關(guān)鍵在于讀取過程中,要判斷所讀取的字符是否已經(jīng)到了文件的末尾,并且這個(gè)字符是不是文件中的斷行符,即判斷該字符值是否為13。
          int c=fr.read();//從文件中讀取一個(gè)字符
          //判斷是否已讀到文件結(jié)尾
          while(c!=-1){
          out.print((char)c);//輸出讀到的數(shù)據(jù)
          c=fr.read();//從文件中繼續(xù)讀取數(shù)據(jù)
          if(c==13){//判斷是否為斷行字符
          out.print("<br>");//輸出分行標(biāo)簽
          fr.skip(1);//略過一個(gè)字符
          //c=fr.read();//讀取一個(gè)字符
          }
          }
          fr.close();
          %>
          </body>
          </html> 一行一行讀取數(shù)據(jù)
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>文件讀取</title>
          </head>
          <body>
          <%
          String path=request.getRealPath("");//取得當(dāng)前目錄的路徑
          FileReader fr=new FileReader(path + "\\file\\inc\\t.txt");//建立FileReader對象,并實(shí)例化為fr
          BufferedReader br=new BufferedReader(fr);//建立BufferedReader對象,并實(shí)例化為br
          String Line=br.readLine();//從文件讀取一行字符串
          //判斷讀取到的字符串是否不為空
          while(Line!=null){
          out.println(Line + "<br>");//輸出從文件中讀取的數(shù)據(jù)
          Line=br.readLine();//從文件中繼續(xù)讀取一行數(shù)據(jù)
          }
          br.close();//關(guān)閉BufferedReader對象
          fr.close();//關(guān)閉文件
          %>
          </body>
          </html>


          略過文件中的字符不讀取
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>略過字節(jié)不讀取</title>
          </head>
          <body>
          <%
          String path=request.getRealPath(".");
          FileReader fr=new FileReader(path + "\\ReadData.txt");
          fr.skip(2);//跳過2個(gè)字節(jié)
          int c=fr.read();//讀取一個(gè)字節(jié)
          while(c!=-1){
          out.print((char)c);
          c=fr.read();
          }
          fr.close();
          %>
          </body>
          </html>


          將數(shù)據(jù)寫入文件
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>將數(shù)據(jù)寫入文件</title>
          </head>
          <body>
          <%
          String path=request.getRealPath(".");
          FileWriter fw=new FileWriter(path + "\\WriteData.txt");//建立FileWriter對象,并實(shí)例化fw
          //將字符串寫入文件
          fw.write("大家好!");
          fw.write("本書是《JSP編程技巧》");
          fw.write("請多多指教!");
          fw.write("email:stride@sina.com");
          fw.close();

          FileReader fr=new FileReader(path + "\\WriteData.txt");
          BufferedReader br=new BufferedReader(fr);//建立BufferedReader對象,并實(shí)例化為br
          String Line=br.readLine();
          //讀取一行數(shù)據(jù)
          out.println(Line + "<br>");
          br.close();//關(guān)閉BufferedReader對象
          fr.close();
          %>
          </body>
          </html>


          將寫入文件的數(shù)據(jù)分行
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>將寫入文件的數(shù)據(jù)分行</title>
          </head>
          <body>
          <%
          String path=request.getRealPath(".");
          FileWriter fw=new FileWriter(path + "\\WriteData.txt");
          BufferedWriter bw=new BufferedWriter(fw);
          bw.write("大家好!");
          bw.write("本書是《JSP編程技巧》。");
          bw.newLine();//斷行
          bw.write("請多多指教!");
          bw.newLine();//斷行
          bw.write("email: stride@sina.com");
          bw.flush();//將數(shù)據(jù)更新至文件
          fw.close();//關(guān)閉文件流
          out.println("寫入文件內(nèi)容為:<br>");
          FileReader fr=new FileReader(path + "\\WriteData.txt");
          BufferedReader br=new BufferedReader(fr);
          String Line=br.readLine();//讀取一行數(shù)據(jù)
          while(Line!=null){
          out.println(Line + "<br>");
          Line=br.readLine();
          }
          fr.close();
          %>
          </body>
          </html>
          如何將數(shù)據(jù)追加寫入到文件
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.io.*"%>
          <html>
          <head>
          <title>將寫入文件的數(shù)據(jù)分行</title>
          </head>
          <body>
          <%
          String path=request.getRealPath(".");
          RandomAccessFile rf=new RandomAccessFile(path + "\\WriteData.txt","rw");//定義一個(gè)類RandomAccessFile的對象,并實(shí)例化
          rf.seek(rf.length());//將指針移動到文件末尾
          rf.writeBytes("\nAppend a line to the file!");
          rf.close();//關(guān)閉文件流
          out.println("寫入文件內(nèi)容為:<br>");
          FileReader fr=new FileReader(path + "\\WriteData.txt");
          BufferedReader br=new BufferedReader(fr);//讀取文件的BufferedRead對象
          String Line=br.readLine();
          while(Line!=null){
          out.println(Line + "<br>");
          Line=br.readLine();
          }
          fr.close();//關(guān)閉文件
          %>
          </body>
          </html></I></I></I></I>

          posted on 2006-06-16 14:45 liaojiyong 閱讀(1981) 評論(0)  編輯  收藏 所屬分類: JSP

          主站蜘蛛池模板: 高州市| 巫山县| 南召县| 乌拉特前旗| 孝感市| 义乌市| 九寨沟县| 兰西县| 永定县| 肇源县| 湖北省| 武强县| 卢氏县| 凭祥市| 中卫市| 教育| 浮山县| 微博| 合阳县| 仁寿县| 日土县| 福贡县| 麻江县| 荆州市| 栖霞市| 深泽县| 中宁县| 鹤山市| 肃北| 阆中市| 通化县| 丰顺县| 营口市| 贵阳市| 襄汾县| 信丰县| 湘潭县| 阿巴嘎旗| 昌乐县| 崇左市| 新竹县|