原文链接:http://www.faceye.net/search/143858.html
文件操作方法
/*
* FileOperation.java
*
* Created on 2006年8月22日, 下午4:25
*
* 文件操作方法
*/
package study.iostudy;
import .*;
/*
* 有关文件操作方面的方法有如下几种:
* boolean createNewFile(); 根据当前的文件对象创建一个新的文件。
* boolean mkdir(); 根据当前的文件对象生成一目录,也就是指定路径下的文件夹。
* boolean mkdirs(); 也是根据当前的文件对象生成一个目录,
* 不同的地方在于该方法即使创建目录失败,
* 也会成功参数中指定的所有父目录。
* boolean delete(); 删除当前的文件。
* void deleteOnExit(); 当前Java虚拟机终止时删除当前的文件。
* String list(); 列出当前目录下的文件。
*/
public class FileOperation
{
public static void main(String[] args)
{
File fileObject = new File("d:\\mydir", "firstFile.txt");
File dirObject1 = new File("d:\\mydir\\01");
File dirObject2 = new File("d:\\mydir\\02");
File dirObject3 = new File("d:\\mydir");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("file object: " + fileObject);
System.out.println("dir object 1: " + dirObject1);
System.out.println("dir object 2" + dirObject2);
try
{
dirObject1.mkdir();
dirObject2.mkdirs();
}catch(SecurityException e)
{
e.printStackTrace();
}
try
{
fileObject.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
String[] files = dirObject2.list();
for (int i = 0; i < files.length; i++)
{
System.out.println("list files in myhdir: " + files[i]);
}
System.out.println("dir object 1 exist? " + dirObject1.exists());
System.out.println("dir object 2 exist? " + dirObject2.exists());
boolean dir1Del = dirObject1.delete();
dirObject2.deleteOnExit();
System.out.println("dir object 1 exist? " + dirObject1.exists());
System.out.println("dir object 2 exist? " + dirObject2.exists());
}
}
找出一个目录下所有的文件
/*
* SearchFile.java
*
* Created on 2006年8月22日, 下午4:45
*
* 找出一个目录下所有的文件
*/
package study.iostudy;
import .*;
public class SearchFile
{
public static void main(String[] args)
{
File dirObject = new File("D:\\aa");
Filter1 filterObj1 = new Filter1("HTML");
Filter2 filterObj2 = new Filter2("Applet");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("list HTML files in directory: " + dirObject);
String[] filesObj1 = dirObject.list(filterObj1);
for (int i = 0; i < filesObj1.length; i++)
{
File fileObject = new File(dirObject, filesObj1[i]);
System.out.println(((fileObject.isFile())
? "HTML file: " : "sub directory: ") + fileObject);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String[] filesObj2 = dirObject.list(filterObj2);
for (int i = 0; i < filesObj2.length; i++)
{
File fileObject = new File(dirObject, filesObj2[i]);
System.out.println(((fileObject.isFile())
? "htm file: " : "sub directory: ") + fileObject);
}
}
}
class Filter1 implements FilenameFilter
{
String fileExtent;
Filter1(String extentObj)
{
fileExtent = extentObj;
}
public boolean accept(File dir, String name)
{
return name.endsWith("." + fileExtent);
}
}
class Filter2 implements FilenameFilter
{
String fileName;
Filter2(String fileName)
{
this.fileName = fileName;
}
public boolean accept(File dir, String name)
{
return name.startsWith(fileName + ".");
}
}
文件内容的拷贝(任意文件)
/*
* CopyFileContent.java
*
* Created on 2006年8月22日, 下午5:37
*
* 文件内容的拷贝(任意文件)
*/
package study.iostudy;
import .*;
public class CopyFileContent
{
static void copyContent(FileInputStream inObj, FileOutputStream outObj)
{
int copyLen;
byte[] copyBuf = new byte[1024];
try
{
while ((copyLen = inObj.read(copyBuf, 0, 1024)) != -1)
{
String copyStr = new String(copyBuf);
System.out.println(copyStr);
outObj.write(copyBuf, 0, copyLen);
}
}catch(IOException e)
{
System.out.println("error: " + e);
}
}
public static void main(String[] args)
{
String secondFileName = "d:\\mydir\\secondFile.wmv";
String thirdFileName = "d:\\mydir\\thirdFile.wmv";
File fileObject = new File(thirdFileName);
FileInputStream inStream;
FileOutputStream outStream;
try
{
fileObject.createNewFile();
inStream = new FileInputStream(secondFileName);
outStream = new FileOutputStream(thirdFileName);
copyContent(inStream, outStream);
}catch(FileNotFoundException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
文件随机访问
/*
* RandomFile.java
*
* Created on 2006年8月22日, 下午9:47
*
* 文件随机访问
*/
package study.iostudy;
import .*;
/*
* 文件随机访问的方法
*
* void seek(long pos); 将文件指针移动到参数指定的位置。
* long getFilePointer(); 得到当前文件指针的位置。
* int skipBytes(int n); 将文件指针向前移动参数指定的n个字节。
* String readLine(); 从当前文件指定位置读取一行。
*
*/
public class RandomFile
{
public static void main(String[] args)
{
String tempStr;
int fileLines = 0;
long pointerLast = 0;
try
{
RandomAccessFile inObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
while (inObj.readLine() != null)
fileLines++;
for (int i =0; i < fileLines / 2; i++)
{
inObj.seek(2 * i);
tempStr = inObj.readLine();
System.out.println(tempStr);
}
pointerLast = inObj.getFilePointer();
}catch(IOException e)
{
e.printStackTrace();
}
try
{
RandomAccessFile fileObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
String writeStr = new String("Insert a string!");
fileObj.seek(pointerLast);
fileObj.writeChars(writeStr);
}catch(IOException e)
{
e.printStackTrace();
}
}
}