⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java07_04.htm

📁 JAVA的课件
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Java程序设计</title>
</head>

<body background="Bg.gif">

<p align="center"><font size="5"><b>§7.4 文件处理</b></font></p>     
    
<p align="left">&nbsp;&nbsp; 前面讲的输入输出流以顺序流的形式对文件内容进行处理,但它们毕竟不是专用的文件处理类,无法实现对文件某些表征的处理,如:文件的大小、读写属性等。另外流的读写是顺序的,无法实现对文件的随机访问,也不能在读取数据的同时写数据。要实现这些功能,需要用到专门的两个类:File和RandomAccessFile</p>    
   
<p align="left">一、File类</p>   
   
<p align="left">&nbsp;&nbsp;&nbsp; 每个File类的对象表示一个磁盘文件或目录,其对象属性中包含了文件或目录的相关信息,调用相应的方法可以完成对文件或目录的管理操作,如创建、删除等。</p>    
   
<p align="left">&nbsp;&nbsp;&nbsp; 要注意的是:File类的对象实例表示的只是一个“抽象”的文件或目录,当程序创建一个File类的对象时,并没有真正创建或准备创建一个文件,也不会去打开对应的文件,只是获取了文件的相关信息而已。File类也没有提供任何对文件内容进行读写的方法。例外的是它提供了两个方法:mkdir可以用来创建目录,createNewFile可以创建一个空文件。下面是它的一些常用方法:</p>   
  
<table border=1>  
  <tr class="TableHeadingColor" bgColor="#ccccff">  
    <td colSpan="2"><font size="+2"><b>Constructor Summary</b></font></td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td><code><b>File</b>(File&nbsp;parent, String&nbsp;child)</code><br>  
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a new <code>File</code>     
      instance from a parent abstract pathname and a child pathname string.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td><code><b>File</b>(String&nbsp;pathname)</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a new <code>File</code>     
      instance by converting the given pathname string into an abstract     
      pathname.</td>   
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td><code><b>File</b>(String&nbsp;parent, String&nbsp;child)</code><br>  
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a new <code>File</code>     
      instance from a parent pathname string and a child pathname string.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td><code><b>File</b>(URI&nbsp;uri)</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a new <tt>File</tt>     
      instance by converting the given <tt>file:</tt> URI into an abstract     
      pathname.</td>   
  </tr>   
</table>   
<table border=1>   
  <tr class="TableHeadingColor" bgColor="#ccccff">   
    <td colSpan="2"><font size="+2"><b>Method Summary</b></font></td>   
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>canRead</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the application can read the file denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>canWrite</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the application can modify to the file denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;int</code></font></td>   
    <td><code><b>compareTo</b>(File&nbsp;pathname)</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Compares two     
      abstract pathnames lexicographically.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>createNewFile</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Atomically     
      creates a new, empty file named by this abstract pathname if and only if a     
      file with this name does not yet exist.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>delete</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deletes the     
      file or directory denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>exists</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the file or directory denoted by this abstract pathname exists.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;String</code></font></td>   
    <td><code><b>getName</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the     
      name of the file or directory denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;String</code></font></td>   
    <td><code><b>getPath</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Converts this     
      abstract pathname into a pathname string.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>isDirectory</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the file denoted by this abstract pathname is a directory.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>isFile</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the file denoted by this abstract pathname is a normal file.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>isHidden</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests whether     
      the file named by this abstract pathname is a hidden file.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;long</code></font></td>   
    <td><code><b>lastModified</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the     
      time that the file denoted by this abstract pathname was last modified.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;long</code></font></td>   
    <td><code><b>length</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the     
      length of the file denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;String[]</code></font></td>   
    <td><code><b>list</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns an     
      array of strings naming the files and directories in the directory denoted     
      by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;File[]</code></font></td>   
    <td><code><b>listFiles</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns an     
      array of abstract pathnames denoting the files in the directory denoted by     
      this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;File[]</code></font></td>   
    <td><code><b>listFiles</b>(FileFilter&nbsp;filter)</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns an     
      array of abstract pathnames denoting the files and directories in the     
      directory denoted by this abstract pathname that satisfy the specified     
      filter.</td>   
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>mkdir</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates the     
      directory named by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>renameTo</b>(File&nbsp;dest)</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Renames the     
      file denoted by this abstract pathname.</td>    
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td>   
      <p align="left"><code><b>setLastModified</b>(long&nbsp;time)</code><br>  
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sets the     
      last-modified time of the file or directory named by this abstract     
      pathname.</p>   
    </td>   
  </tr>   
  <tr class="TableRowColor" bgColor="white">   
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>   
    <td><code><b>setReadOnly</b>()</code><br>   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marks the file     
      or directory named by this abstract pathname so that only read operations     
      are allowed.</td>    
  </tr>   
</table>   
<p align="left">例1 列出当前目录下的所有文件和子目录:</p>  
 
<p align="left">import java.io.*;<br>   
&nbsp; public class dir{<br>   
&nbsp;&nbsp; public static void main(String argv[]){<br>   
&nbsp;&nbsp;&nbsp;&nbsp; File fdir=new File(".");<br>   
&nbsp;&nbsp;&nbsp;&nbsp; File[] AllFile;<br>   
&nbsp;&nbsp;&nbsp;&nbsp; int i;<br>   
&nbsp;&nbsp;&nbsp;&nbsp; AllFile=fdir.listFiles();<br>  
&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;AllFile.length;i++)<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(AllFile[i].getName());<br>  
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("共有"+i+"个文件和目录");<br>  
&nbsp; }&nbsp;<br>  
} </p>

<p align="left">二、RandomAccessFile类</p>

<p align="left">&nbsp;&nbsp; <b>RandomAccessFile类是Object类的直接子类,</b>利用它,可以实现对文件的随机访问,它的构造方法如下:</p>  
 
<table border=1> 
  <tr class="TableHeadingColor" bgColor="#ccccff"> 
    <td colSpan="2"><font size="+2"><b>Constructor Summary</b></font></td>  
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>RandomAccessFile</b>(File&nbsp;file, String&nbsp;mode)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a   
      random access file stream to read from, and optionally to write to, the   
      file specified by the <code>File</code> argument.</td>  
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>RandomAccessFile</b>(String&nbsp;name, String&nbsp;mode)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a   
      random access file stream to read from, and optionally to write to, a file   
      with the specified name.</td>  
  </tr> 
</table> 
<p align="left">&nbsp;  这里的mode有四种模式:&quot;r&quot;——打开文件只读,&quot;rw&quot;——打开文件读写,&quot;rws&quot;,&quot;rwd&quot;——按读写方式打开文件,但写的时候不使用缓冲。该类提供了大量的读写方法,都和InputStream以及OutputStream的使用差不多,我们不再重复。另有一个seek方法可以随意移动文件指针到任意位置,以实现随机的读写。</p>

<p align="left">例2、对文件加密</p>

<p align="left">基本思路:将文件中的内容读入到一片缓冲区,将每个字符都按位取反,然后将内容写回原文件。</p>

<p align="left">import java.io.*;<br>  
public class encrypt{<br>  
&nbsp;public static void main(String argv[]){<br>  
&nbsp;&nbsp; File file;<br>  
&nbsp;&nbsp; RandomAccessFile fp;<br>  
&nbsp;&nbsp; byte[] buf;<br>  
&nbsp;&nbsp; int i;<br>  
<br> 
&nbsp;&nbsp; if (argv.length&lt;1)<br>  
&nbsp;&nbsp; {<br> 
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("你需要指定待加密的文件名称!");<br> 
&nbsp;&nbsp;&nbsp;&nbsp; return ;<br>  
&nbsp;&nbsp; }<br> 
&nbsp;&nbsp; try{<br> 
&nbsp;&nbsp;&nbsp;&nbsp; file=new File(argv[0]);<br>  
&nbsp;&nbsp;&nbsp;&nbsp; fp=new RandomAccessFile(file,"rw");<br>  
&nbsp;&nbsp;&nbsp;&nbsp; buf=new byte[(int)file.length()];<br>  
&nbsp;&nbsp;&nbsp;&nbsp; fp.read(buf);<br> 
&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;buf.length;i++)<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buf[i]=(byte)(~buf[i]);&nbsp;<br> 
&nbsp;&nbsp;&nbsp;&nbsp; fp.seek(0);<br> 
&nbsp;&nbsp;&nbsp;&nbsp; fp.write(buf);<br> 
&nbsp;&nbsp;&nbsp;&nbsp; fp.close();<br> 
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("加密成功");<br> 
&nbsp;&nbsp; }catch(IOException e){<br>  
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("文件读写失败!");<br> 
&nbsp;&nbsp; }<br> 
&nbsp;}&nbsp;<br>
}</p>

<p align="left"><a href="index.htm">回目录</a>   <a href="java07_03.htm">上一课</a>  
 <a href="file:///E:/我的文档/Java课件/java08_01.htm">下一课</a> 
</p>

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -