溫馨提示×

溫馨提示×

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

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

JAVA輸入輸出流詳解(H)

發(fā)布時間:2020-08-06 19:31:47 來源:ITPUB博客 閱讀:122 作者:lotuszm 欄目:編程語言
*/
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
[@more@]//找到要顯示的文件入口,然后讀取通過文本格式讀取文件內(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 + " ";
}
}
}
}
//
關(guān)閉文件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
!?。?/font>");
choose = Integer.parseInt(userInput.readLine());
if ((choose <0)||(choose > showFileArr.length)){
System.out.println("Plase choose file num retry
?。。?/font>");
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)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI