1.获取给定文件的文件名 获取给定文件的文件名,如程序3-22所示。该函数比较简单,就是通过文件分隔符,返回给定路径的最后一个分隔符之后的字符串,如果查找不到文件分隔符,则表明当前的参数s就是文件名称,直接返回即可。比如在Windows系统下的一个文件存储,路径可能是“C://temp//image_log.txt”,程序最后返回的结果将是“image_log.txt”。
在Windows下的文件分隔符为“/”,而Linux下则为“/”,注意区分。
程序3-22 获取文件名
public static String extractFileName(String s)
{
int pos = s.lastIndexOf(File.separatorChar);
if (pos != -1) return s.substring(pos+1); else return s;
}
2.文件到文件流的操作 本模块的示例程序3-23实现了从文件读取到文件流及输入输出文件流之间的转换两部分。首先是loadFile函数,它把文件filename读入输入文件流,然后转换到输出文件流,这里使用ByteArrayOutputStream字节数组输出流。loadFile函数中使用convertStream函数,就是输入文件流和输出文件流之间的转换。循环读取,然后依次写入。
程序3-23 读文件
public static String loadFile(String filename)
{
try {
FileInputStream in = new FileInputStream(new File(filename));
ByteArrayOutputStream os = new ByteArrayOutputStream();
convertStream(in, os);
return os.toString();
}
catch (Exception ex) {}
return "";
}
//输入、输出文件流的转换
public static void convertStream(InputStream is, OutputStream os) throws NullPointerException, IOException
{
if (is == null || os == null) throw new NullPointerException();
byte[] buf = new byte[1024];
int cnt;
while ((cnt = is.read(buf)) != -1) os.write(buf, 0, cnt);
is.close();
os.close();
}
3.文件的复制 复制文件模块如程序3-24所示。文件复制的实现机制如下,首先建立两个文件流。一个是使用起始文件路径建立的输入文件流,一个是使用目标文件路径建立的输出文件流,然后循环从输入文件流读取,依次把内容写入输出文件流,这样就完成了文件的复制功能。
程序3-24 文件复制
/**
* 文件复制功能
*
* @param from 起始文件路径
* @param to 目标文件路径
* @return size 文件复制过程中操作的字节数
* @throws IOException
*/
public static long copyFile(File from, File to) throws IOException {
if (from == null || to == null || !from.isFile()) return -1;
FileInputStream fis = new FileInputStream( from );
FileOutputStream fos = new FileOutputStream( to );
byte [] buff = new byte[1024];
int read = 0;
long size = 0;
while ( (read = fis.read(buff)) > 0 ) {
fos.write(buff, 0, read);
size += read;
}
fos.close();
fis.close();
return size;
}
4.按照特定编码从文本文件读/写字符串 使用给定的编码encode从文件中读文件到字符串及使用给定的编码encode把字符串写入文件的模块,如程序3-25所示。函数readFromFile把文件读取到FileInputStream文件流中,然后显示结果的时候使用给定的编码encode返回。函数writeToFile是readFromFile的逆向过程,它的作用是向文件中写入字符串内容,写入前需要先把字符串text使用给定的编码转换一下,这样才能正确地写入到文件中去。
程序3-25 读/写文件
/**
*从文本文件读取文件内容为字符串,按照encode编码返回结果
*
* @param filename 要读取的文件
* @param encode 编码
* @return string readed
*/
public static String readFromFile(String filename, String encode) {
try {
FileInputStream fis = new FileInputStream(filename);
byte[] buf = new byte[fis.available()];
fis.read(buf);
fis.close();
return new String(buf, encode);
}
catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 按照encode向文本文件写入字符串
*
* @param text 需要写入的字符串
* @param filename 待写入的文件
* @param encode 编码
*/
public static void writeToFile(String text, String filename, String encode) {
try {
byte[] buf = text.getBytes(encode);
FileOutputStream fos = new FileOutputStream(filename);
fos.write(buf);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.4.2 文件目录处理模块 1.返回文件所在的目录 返回文件所在目录的模块,如程序3-26所示。该函数比较简单,就是通过文件分隔符,返回给定路径的最后一个分隔符之前的字符串即可。
程序3-26 返回文件所在目录
public static String extractFilePath(String s)
{
int pos = s.lastIndexOf(File.separatorChar);
if (pos != -1) return s.substring(0, pos+1); else return "";
}
2.把文件从一个目录移动到另外一个目录 在不同目录之间移动文件的模块,如程序3-27所示。文件移动操作的实现比较简单,如果文件from和文件to在同一目录下,只需要使用File类的renameTo函数即可,就是把源文件重命名成目标文件即可。当文件不在同一个目录下的时候,只需要使用文件复制功能,把源文件夹复制到目标文件夹即可。
程序3-27 文件移动
/**
* 在不同路径之间移动文件
*
* @param from 源地址
* @param to 目标地址
* @return 返回已经写入的字节数
* @throws IOException
*/
public static long moveFile(File from, File to) throws IOException {
if (from == null || to == null || !from.isFile()) return -1;
long size = 0;
if (from.getParent().equals(to.getParent())) {
from.renameTo(to);
FileInputStream fis = new FileInputStream( to );
size = fis.available();
fis.close();
} else {
size = copyFile(from, to);
from.delete();
}
return size;
}
3.使用给定的路径生成文件路径 使用给定的路径生成文件路径的模块代码,如程序3-28所示。基本原理是,使用File类的mkdir函数创建路径,代码递归调用createDirectory函数,使用绝对路径创建给定参数的文件路径。
程序3-28 生成文件路径
/**
* 使用给定的目录字符串创建文件目录,类似"/dd/aa/bb"
*
* @param path 要创建的路径,需要使用绝对路径
*/
public static void createDirectory( String fullpath ) {
File dir = new File(fullpath);
if(!dir.exists())
{
createDirectory( dir.getParentFile().getAbsolutePath() );
dir.mkdir();
}
}
4.删除文件目录 删除文件目录的模块代码,如程序3-29所示。该程序也是一个递归调用的过程,因为在目录的下面还可能包含其他的目录和文件,所以程序使用了一个for循环,依次删除子目录和文件。File类的delete方法只能删除该路径下的文件或者空目录,所以当判断子目录为文件目录时,需要递归调用deleteDirectory(File dir)函数方法,即下面程序代码中的deleteDirectory( children[i] );语句,最后遍历删除完该目录下的所有文件后,使用dir.delete()方法删除该空目录,完成文件目录删除的功能
程序3-29 删除文件目录
/**
* 递归删除特定的文件目录
*
* @param dir 需要删除的目录
*/
public static void deleteDirectory(File dir) {
if (dir == null || !dir.isDirectory()) return;
File[] children = dir.listFiles();
if (children != null) {
for (int i=0; i<children.length; i++) {
if ( children[i].isFile() ) {
children[i].delete();
} else if ( children[i].isDirectory() ) {
deleteDirectory( children[i] );
}
}
}
dir.delete();
}