资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java的文件操作--1

前两天看了《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;

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供网站制作、成都做网站、网站策划、网页设计、域名注册、网页空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

}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();
}
}
}


当前标题:Java的文件操作--1
本文路径:http://cdkjz.cn/article/igdgeh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220