love fish大鵬一曰同風起,扶搖直上九萬里

          常用鏈接

          統(tǒng)計

          積分與排名

          friends

          link

          最新評論

          Java文件操作大全(轉(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();//在當前目錄下建立一個名為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對象,并得到它的一個引用
          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站點主目錄的位置為<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();//在當前目錄下建立一個名為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);//建立當前目錄中文件的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對象,并實例化為fr
          //對FileReader類生成的對象使用read()方法,可以從字符流中讀取下一個字符。
          if(fr.read()==-1)//判斷是否已讀到文件的結(jié)尾
          {
          out.print("AtEnd.txt文件中沒有數(shù)據(jù)<br>");
          }else{
          out.println("AtEnd.txt文件中有數(shù)據(jù)");
          }
          fr.close();
          %>
          </body>
          </html>
          ??? <B>讀取所有的文件數(shù)據(jù)</B>
          <ccid_nobr>
          <table width="400" border="1" cellspacing="0" cellpadding="2"
          bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">
          <tr>
          <td bgcolor="e6e6e6" class="code" style="font-size:9pt">
          <pre><ccid_code> <%@ 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)到了文件的末尾,
          并且這個字符是不是文件中的斷行符,即判斷該字符值是否為13。
          int c=fr.read();//從文件中讀取一個字符
          //判斷是否已讀到文件結(jié)尾
          while(c!=-1){
          out.print((char)c);//輸出讀到的數(shù)據(jù)
          c=fr.read();//從文件中繼續(xù)讀取數(shù)據(jù)
          if(c==13){//判斷是否為斷行字符
          out.print("<br>");//輸出分行標簽
          fr.skip(1);//略過一個字符
          //c=fr.read();//讀取一個字符
          }
          }
          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("");//取得當前目錄的路徑
          FileReader fr=new FileReader(path + " \\file\\inc\\t.txt");// 建立FileReader對象,并實例化為fr
          BufferedReader br=new BufferedReader(fr);//建立BufferedReader對象,并實例化為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個字節(jié)
          int c=fr.read();//讀取一個字節(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對象,并實例化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對象,并實例化為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 ");
          //定義一個類RandomAccessFile的對象,并實例化
          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 2007-03-23 09:46 liaojiyong 閱讀(997) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 赤峰市| 塘沽区| 平遥县| 汶川县| 正镶白旗| 荆门市| 瑞安市| 沙田区| 砀山县| 麻江县| 富川| 南京市| 泗水县| 灵台县| 朔州市| 康平县| 独山县| 育儿| 吉林市| 成安县| 新闻| 阳城县| 靖西县| 若羌县| 东至县| 怀宁县| 西昌市| 金寨县| 新巴尔虎左旗| 麻城市| 黔江区| 牡丹江市| 凤庆县| 库车县| 大庆市| 永安市| 泗水县| 怀宁县| 全椒县| 天长市| 辉南县|