Java的文件操作--1

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

前两天看了《java核心编程》中对于java中的IO输入输出讲解,收获良多,下面的程序是我读完书以后自己写的一个将zip文件集中的文件内容读取出来然后显示(只限于文本,其他文件将会产生乱码),此程序也可通过改进可以得到一个和winzip功能一样的类(但是没有界面,主要是awt不会:--( ),过两天就把源码贴出来。很简单的一个类,希望对大家有用。

这是本人第一次发文章,不足之处请多多指教!!!

/**

*功能: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 + "\n\r";

}

}

}

}

//关闭文件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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有