温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java的文件操作--1

发布时间:2020-08-10 15:16:53 来源:ITPUB博客 阅读:97 作者:lotuszm 栏目:编程语言
前两天看了《java核心编程》中对于java中的IO输入输出讲解,收获良多,下面的程序是我读完书以后自己写的一个将zip文件集中的文件内容读取出来然后显示(只限于文本,其他文件将会产生乱码),此程序也可通过改进可以得到一个和winzip功能一样的类(但是没有界面,主要是awt不会:--( ),过两天就把源码贴出来。很简单的一个类,希望对大家有用。[@more@]**
*功能:Zip文件释放,Zip文件目录、选择目录后显示文件内容
* 首先用户输入zip文件路径及名称,读取zip文件将文件集中的文件清单显示出来,
* 用户选择需要显示的文件,之后通过DataInputStream类将文件内容读取出来并
* 显示在标准输出端
*日期:2005-06-27
*作者:Pcera
*/
import java.util.*;
import java.util.zip.*;
import java.io.*;

class ZipFileHandle{
private String[] FileNameArray; //真实文件名存放数组
private String[] FileNameArrayShow; //需要显示的文件名存放数组
private ZipInputStream zipFile; //zip输入流对象
private ZipEntry entry; //zip文件入口对象
private int zipFileCount = 0; //zip中的文件总数

/**
*初始化各个参数
*通过类的套嵌来访问文件
*将得到文件的清单附值给数组,以便在后面用户选择时从数组中获得文件名
*/
public ZipFileHandle(File file){
try{
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String filepath = userInput.readLine();
file = new File(filepath);
}
zipFile = new ZipInputStream(new FileInputStream(file));
while ((zipFile.getNextEntry()) != null){
zipFileCount++;
}
FileNameArray = new String[zipFileCount];
FileNameArrayShow = new String[zipFileCount];
}catch(IOException e){
System.out.println("初始化错误!!!");
e.printStackTrace();
}
}
/**
*生成文件目录
*根据show的值来确定返回值
*如果show的值为"sh"则返回在屏幕上显示的名称
*如果show的值为"gr"则返回实际名称
*/
public String[] getFileNameList(String show,File file){
try{
int i = 0;
String FileName;
zipFile = new ZipInputStream(new FileInputStream(file));
while ((entry = zipFile.getNextEntry()) != null){
FileName = entry.getName();
//真实文件名附值
FileNameArray[i] = FileName;
//显示文件名附值
if (FileName.equals("")) FileName = "...";
FileName = Integer.toString(i) + "-|" + FileName;
FileNameArrayShow[i] = FileName;
i++;
}
zipFile.close();
//根据条件返回文件数组
if(show == "sh"){
return FileNameArrayShow;
}else{
return FileNameArray;
}
}catch(IOException e){
System.out.println("读取zip文件内的文件名出错!!!");
e.printStackTrace();
return null;
}
}

/**
*读取文件内容
*根据传递进来的zip文件对象和
*zip中所要显示的文件
*用户根据type来选择返回的是unicode信息还字节信息
*如果type是"Str"则返回字符串信息,"Byte"则返回通过字节获得的文件内容
*/
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
//找到要显示的文件入口,然后读取通过文本格式读取文件内容
zipFile = new ZipInputStream(new FileInputStream(file));
//读取文件内容
while ((entry = zipFile.getNextEntry()) != null){
if (entry.getName().equals(fileName)){
//通过字节读取文件内容
if (type.equals("Byte")){
fileConfByte = new byte[(int)entry.getSize()];
DataInputStream reader = new DataInputStream(zipFile);
reader.readFully(fileConfByte,0,(int)entry.getSize());
fileContent = new String(fileConfByte);
}else if ((type.equals("Str"))){
//通过unicode字符读取文件内容
BufferedReader in = new BufferedReader(new InputStreamReader(zipFile));
while ((conTemp = in.readLine()) != null){
fileContent = fileContent + conTemp + " ";
}
}
}
}
//关闭文件zip流
zipFile.closeEntry();
zipFile.close();
//返回
return fileContent;

}catch(IOException e){
System.out.println("读取文件内容失败!!!");
e.printStackTrace();
return null;
}

}

/**
*演示方法
*/
public static void main(String args[]){
String filePath;
String[] showFileArr;
String[] realFileArr;
String fileContent;
int choose;
//通过用户获得文件路径和文件名
try{
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Plase zip file path and name !!!");
filePath = userInput.readLine();
File file = new File(filePath);
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
filePath = userInput.readLine();
file = new File(filePath);
}
//显示文件目录,显示选择的文件的内容
//获得真实文件名和显示文件名数组
ZipFileHandle zipFile = new ZipFileHandle(file);
showFileArr = zipFile.getFileNameList("sh",file);
realFileArr = zipFile.getFileNameList("gr",file);
//打印显示文件名数组
int i = 0;
while (i < showFileArr.length){
System.out.println(showFileArr[i]);
i++;
}
//通过用户输入获得需要显示的文件
System.out.println("Plase choose file num!!!");
choose = Integer.parseInt(userInput.readLine());
if ((choose <0)||(choose > showFileArr.length)){
System.out.println("Plase choose file num retry!!!");
choose = Integer.parseInt(userInput.readLine());
}
//获得用户选择的文件的内容
fileContent = zipFile.loadFileCon(realFileArr[choose],file,"Byte");
System.out.println(fileContent);

}catch(Exception e){
System.out.println("测试程序出错!!!");
e.printStackTrace();
}
}
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI