溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java的文件操作--1

發(fā)布時間:2020-08-10 15:16:53 來源:ITPUB博客 閱讀:111 作者:lotuszm 欄目:編程語言
前兩天看了《java核心編程》中對于java中的IO輸入輸出講解,收獲良多,下面的程序是我讀完書以后自己寫的一個將zip文件集中的文件內(nèi)容讀取出來然后顯示(只限于文本,其他文件將會產(chǎn)生亂碼),此程序也可通過改進可以得到一個和winzip功能一樣的類(但是沒有界面,主要是awt不會:--( ),過兩天就把源碼貼出來。很簡單的一個類,希望對大家有用。[@more@]**
*功能:Zip文件釋放,Zip文件目錄、選擇目錄后顯示文件內(nèi)容
* 首先用戶輸入zip文件路徑及名稱,讀取zip文件將文件集中的文件清單顯示出來,
* 用戶選擇需要顯示的文件,之后通過DataInputStream類將文件內(nèi)容讀取出來并
* 顯示在標準輸出端
*日期:2005-06-27
*作者:Pcera
*/
import java.util.*;
import java.util.zip.*;
import java.io.*;

class ZipFileHandle{
private String[] FileNameArray; //真實文件名存放數(shù)組
private String[] FileNameArrayShow; //需要顯示的文件名存放數(shù)組
private ZipInputStream zipFile; //zip輸入流對象
private ZipEntry entry; //zip文件入口對象
private int zipFileCount = 0; //zip中的文件總數(shù)

/**
*初始化各個參數(shù)
*通過類的套嵌來訪問文件
*將得到文件的清單附值給數(shù)組,以便在后面用戶選擇時從數(shù)組中獲得文件名
*/
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();
}
}
/**
*生成文件目錄
*根據(jù)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();
//根據(jù)條件返回文件數(shù)組
if(show == "sh"){
return FileNameArrayShow;
}else{
return FileNameArray;
}
}catch(IOException e){
System.out.println("讀取zip文件內(nèi)的文件名出錯!?。?);
e.printStackTrace();
return null;
}
}

/**
*讀取文件內(nèi)容
*根據(jù)傳遞進來的zip文件對象和
*zip中所要顯示的文件
*用戶根據(jù)type來選擇返回的是unicode信息還字節(jié)信息
*如果type是"Str"則返回字符串信息,"Byte"則返回通過字節(jié)獲得的文件內(nèi)容
*/
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
//找到要顯示的文件入口,然后讀取通過文本格式讀取文件內(nèi)容
zipFile = new ZipInputStream(new FileInputStream(file));
//讀取文件內(nèi)容
while ((entry = zipFile.getNextEntry()) != null){
if (entry.getName().equals(fileName)){
//通過字節(jié)讀取文件內(nèi)容
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字符讀取文件內(nèi)容
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("讀取文件內(nèi)容失敗?。?!");
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);
}
//顯示文件目錄,顯示選擇的文件的內(nèi)容
//獲得真實文件名和顯示文件名數(shù)組
ZipFileHandle zipFile = new ZipFileHandle(file);
showFileArr = zipFile.getFileNameList("sh",file);
realFileArr = zipFile.getFileNameList("gr",file);
//打印顯示文件名數(shù)組
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());
}
//獲得用戶選擇的文件的內(nèi)容
fileContent = zipFile.loadFileCon(realFileArr[choose],file,"Byte");
System.out.println(fileContent);

}catch(Exception e){
System.out.println("測試程序出錯?。?!");
e.printStackTrace();
}
}
}

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI