温馨提示×

温馨提示×

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

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

JAVA输入输出流详解(H)

发布时间:2020-08-06 19:31:47 来源:ITPUB博客 阅读:116 作者:lotuszm 栏目:编程语言
*/
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
[@more@]//找到要显示的文件入口,然后读取通过文本格式读取文件内容
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