1、利用BufferedReader字符流。
public static StringBuffer getFileContent(File target) {
StringBuffer content = new StringBuffer();
BufferedReader read = null;
try {
InputStream is = new FileInputStream(target);
read = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = read.readLine()) != null) {
content.append(line);
}
// 关闭流
read.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
方式二:使用BufferedInputSteam读取
/**
* 描述:读取一个文件
*/
public static void readFile(String filePath) throws Exception {
BufferedInputStream bis = null;
byte[] buffer = new byte[1024];
// 文本内容
StringBuffer content = new StringBuffer();
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
bis = new BufferedInputStream(new FileInputStream(file));
int temp = 0;
while ((temp = bis.read(buffer)) != -1) {
// 将读取的数据保存为字符串
content.append(new String(buffer, 0, temp));
}
// 关闭流
bis.close();
}
3、方式三:利用NIO
/**
* 描述:读取一个文件
*/
public static void readFile(String filePath) throws Exception {
FileInputStream fis = null;
FileChannel channel = null;
StringBuffer content = new StringBuffer();
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
fis = new FileInputStream(file);
channel = fis.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int temp = 0;
while ((temp = channel.read(buffer)) != -1) {
content.append(new String(buffer.array()));
//让缓冲区可以将新的数据写入到另一个通道,一定要写否则会跑出内存溢出的异常
buffer.flip();
}
channel.close();
fis.close();
}
其它方式略。
2、文件的压缩与解压缩
压缩流:ZipOutputStream,ZipInputStream
ZipEntry:压缩包文件
public ZipEntry(String name):创建对象并指定ZipEntry的名称
public boolean isDirectory()
ZipOutputStream(OutputStream out):创建新的Zip输出流
public void putNextEntry(ZipEntry e):设置ZipEntry对象
public void setComment(String s):设置Zip文件注释
创建压缩文件的的步骤:
1)实例化输入输出流
2)创建ZipEntity对象
3)设置ZipEntity对象(把zipEntity对象加入到压缩包中)
4)设置注释
5)读文件/压缩文件
6)关闭流
/**
* 创建一个压缩文件,只考虑一级目录,多级使用递归进行操作
*
* @param filePath
* :文件路径
* @param zipFilePath
* : 压缩文件名称路径
*/
public static void createZipFile(String filePath, String zipFilePath) throws Exception {
ZipOutputStream zos = null;
BufferedInputStream bis = null;
File file = new File(filePath);
File zipFile = new File(zipFilePath);
if (!zipFile.exists()) {
zipFile.createNewFile();
}
// 创建压缩流
zos = new ZipOutputStream(new FileOutputStream(zipFile));
File[] subFiles = file.listFiles();
for (File subFile : subFiles) {
// 创建ZipEntry对象
ZipEntry ze = new ZipEntry(subFile.getName());
// 设置ZipEntry对象
zos.putNextEntry(ze);
bis = new BufferedInputStream(new FileInputStream(subFile));
int temp=0;
while ((temp = bis.read()) != -1) {
// 边读边写
zos.write(temp);
}
// 关闭流
bis.close();
}
zos.close();
}
解压缩:同样只考虑一级目录,多级使用递归
/**
* 解压缩
*
* @param openFilePath
* :解开压缩文件的存放路径
* @param zipFilePath
* :压缩文件名称路径
*/
public static void openZipFile(String openFilePath, String zipFilePath) throws Exception {
OutputStream os = null;
ZipInputStream zis = null;
ZipFile zipFile = new ZipFile(zipFilePath);
File file = new File(openFilePath);
if (!file.exists()) {
file.mkdirs();
}
// 实例化
zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry ze = zis.getNextEntry();
int fileCount=0;
while (ze != null) {
System.out.println("正在解压" + ze.getName() + "文件...");
File subFile = new File(openFilePath + File.separator + ze.getName());
if(!subFile.exists()){
subFile.createNewFile();
}
InputStream is=zipFile.getInputStream(ze);
os=new FileOutputStream(subFile);
int temp=0;
while((temp=is.read())!=-1){
os.write(temp);
}
ze=zis.getNextEntry();
is.close();
fileCount++;
}
System.out.println("解压完成!");
os.close();
zis.close();
}