📄 java07_04.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"> 前面讲的输入输出流以顺序流的形式对文件内容进行处理,但它们毕竟不是专用的文件处理类,无法实现对文件某些表征的处理,如:文件的大小、读写属性等。另外流的读写是顺序的,无法实现对文件的随机访问,也不能在读取数据的同时写数据。要实现这些功能,需要用到专门的两个类:File和RandomAccessFile</p>
<p align="left">一、File类</p>
<p align="left"> 每个File类的对象表示一个磁盘文件或目录,其对象属性中包含了文件或目录的相关信息,调用相应的方法可以完成对文件或目录的管理操作,如创建、删除等。</p>
<p align="left"> 要注意的是: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 parent, String child)</code><br>
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 pathname)</code><br>
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 parent, String child)</code><br>
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 uri)</code><br>
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> boolean</code></font></td>
<td><code><b>canRead</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>canWrite</b>()</code><br>
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> int</code></font></td>
<td><code><b>compareTo</b>(File pathname)</code><br>
Compares two
abstract pathnames lexicographically.</td>
</tr>
<tr class="TableRowColor" bgColor="white">
<td vAlign="top" align="right" width="1%"><font size="-1"><code> boolean</code></font></td>
<td><code><b>createNewFile</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>delete</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>exists</b>()</code><br>
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> String</code></font></td>
<td><code><b>getName</b>()</code><br>
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> String</code></font></td>
<td><code><b>getPath</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>isDirectory</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>isFile</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>isHidden</b>()</code><br>
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> long</code></font></td>
<td><code><b>lastModified</b>()</code><br>
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> long</code></font></td>
<td><code><b>length</b>()</code><br>
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> String[]</code></font></td>
<td><code><b>list</b>()</code><br>
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> File[]</code></font></td>
<td><code><b>listFiles</b>()</code><br>
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> File[]</code></font></td>
<td><code><b>listFiles</b>(FileFilter filter)</code><br>
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> boolean</code></font></td>
<td><code><b>mkdir</b>()</code><br>
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> boolean</code></font></td>
<td><code><b>renameTo</b>(File dest)</code><br>
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> boolean</code></font></td>
<td>
<p align="left"><code><b>setLastModified</b>(long time)</code><br>
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> boolean</code></font></td>
<td><code><b>setReadOnly</b>()</code><br>
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>
public class dir{<br>
public static void main(String argv[]){<br>
File fdir=new File(".");<br>
File[] AllFile;<br>
int i;<br>
AllFile=fdir.listFiles();<br>
for(i=0;i<AllFile.length;i++)<br>
System.out.println(AllFile[i].getName());<br>
System.out.println("共有"+i+"个文件和目录");<br>
} <br>
} </p>
<p align="left">二、RandomAccessFile类</p>
<p align="left"> <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 file, String mode)</code><br>
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 name, String mode)</code><br>
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"> 这里的mode有四种模式:"r"——打开文件只读,"rw"——打开文件读写,"rws","rwd"——按读写方式打开文件,但写的时候不使用缓冲。该类提供了大量的读写方法,都和InputStream以及OutputStream的使用差不多,我们不再重复。另有一个seek方法可以随意移动文件指针到任意位置,以实现随机的读写。</p>
<p align="left">例2、对文件加密</p>
<p align="left">基本思路:将文件中的内容读入到一片缓冲区,将每个字符都按位取反,然后将内容写回原文件。</p>
<p align="left">import java.io.*;<br>
public class encrypt{<br>
public static void main(String argv[]){<br>
File file;<br>
RandomAccessFile fp;<br>
byte[] buf;<br>
int i;<br>
<br>
if (argv.length<1)<br>
{<br>
System.out.println("你需要指定待加密的文件名称!");<br>
return ;<br>
}<br>
try{<br>
file=new File(argv[0]);<br>
fp=new RandomAccessFile(file,"rw");<br>
buf=new byte[(int)file.length()];<br>
fp.read(buf);<br>
for(i=0;i<buf.length;i++)<br>
buf[i]=(byte)(~buf[i]); <br>
fp.seek(0);<br>
fp.write(buf);<br>
fp.close();<br>
System.out.println("加密成功");<br>
}catch(IOException e){<br>
System.out.println("文件读写失败!");<br>
}<br>
} <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 + -