自己使用文件操作不多,因此每次需要写这块代码时,都得网上查资料,因此今天总结一份资料,供日后参考。
本文介绍java对文件的基础操作,主要介绍读取与写入,其它操作较为简单,一带而过。
(1)文件读取
文本类的文件即可以用字节去读取也可以用字符去读取,其它类的文件一般用字节读取。这里以文本类的文件为例介绍两种读取方法,并做效率比较。
/**
* 按字符读取文件
* @param filePath 文件路径
* @return 文本
*/
public static String readFileByChar(String filePath) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filePath);
br = new BufferedReader(fr);
int size = 2048;
char[] buffer = new char[size];
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = br.read(buffer, 0, size)) != -1) {
sb.append(buffer, 0, len);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 按字节读取文件
* @param filePath 文件路径
* @return 文本
*/
public static String readFileByByte(String filePath) {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
fis = new FileInputStream(filePath);
isr = new InputStreamReader(fis);
int size = 2048;
char[] buffer = new char[size];
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = isr.read(buffer, 0, size)) != -1) {
sb.append(buffer, 0, len);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
从上面代码中可以看到
按字符读取文件时主要使用reader,为了提高提取效率使用了bufferedreader,这里读取出来的就是char[]字符数组,直接可以转成字符串;
按字节读取文件时主要使用inputstream,为了将字节转成字符,又使用了inputstreamreader(这里用到了java中流与字符相互转换的知识,核心就是inputstreamreader和outputstreamwriter,前者是将字节转成字符,后者将字符转成字节);
编写代码时需注意:当出现容器嵌套时,关闭最外层的容器即可,比如下面代码中
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
我们只需要关闭br即可。
下面通过代码来比较一下两种读取文件的执行效率(说明一下,我这里使用的文件是个70M的文本类文件)
long begin = System.currentTimeMillis();
readFileByChar("c:/zhxy.kqf");
long end = System.currentTimeMillis();
System.out.println("按字符读取文件所用时间为:" + (end - begin));
begin = System.currentTimeMillis();
readFileByByte("c:/zhxy.kqf");
end = System.currentTimeMillis();
System.out.println("按字节读取文件所用时间为:" + (end - begin));
看下执行结果
按字符读取文件所用时间为:328
按字节读取文件所用时间为:281
因此在进行文件读取时推荐使用字节读取
(2)文件写入
文件写入与文件读取正好相反,相应地也有按字符写入与按字节写入,这里只介绍一种按字节写入的简单方法
/**
* 文件写入
* @param filePath 文件路径
* @param content 内容
* @param endBegin 是否追加:true表示在文件末尾追加新内容,false表示重写文件内容
*/
public static void writeFile(String filePath, String content, boolean endBegin) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(filePath, endBegin);
bos = new BufferedOutputStream(fos);
bos.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件写入时需要注意最后的那个boolean类型的参数,当为true时表示在文件末尾追加新内容,当为false时表示重写文件内容。
(3)文件复制
文件复制是对读与写的一种结合应用,而且为了提高效率,一般都是设置缓存,然后边读边写
/**
* 文件复制
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFile(String sourcePath, String targetPath) {
// 若目标文件不存在则先创建
createFile(targetPath);
// 复制文件内容
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(targetPath);
int size = 2048;
byte[] buffer = new byte[2048];
int len = 0;
while ((len = fis.read(buffer, 0, size)) != -1) {
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(4)文件创建删除
创建与删除比较简单,这里还特别介绍一下文件夹的创建方法,代码如下:
/**
* 创建文件
* @param filePath 文件路径
* @return true 创建成功,false 创建失败或文件已存在
*/
public static boolean createFile(String filePath) {
File f = new File(filePath);
try {
return f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 创建文件夹
* @param path 文件夹路径
* @return true 创建成功,false 创建失败
*/
public static boolean createPath(String path) {
File f = new File(path);
return f.mkdirs();
}
/**
* 删除文件或者目录
* @param filePath 目录
* @return true 删除成功,false 删除失败
*/
public static boolean deletePath(String filePath) {
File f = new File(filePath);
return f.delete();
}