首页 > 代码库 > java io 文件管理
java io 文件管理
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class MenuTest {
private String sourceFilePath;
private String targetFilePath;
public String getSourceFilePath() {
return sourceFilePath;
}
public void setSourceFilePath(String sourceFilePath) {
this.sourceFilePath = sourceFilePath;
}
public String getTargetFilePath() {
return targetFilePath;
}
public void setTargetFilePath(String targetFilePath) {
this.targetFilePath = targetFilePath;
}
public String getFilePath(String message) {
String filePath;
System.out.println(message);
Scanner scanner = new Scanner(System.in);
filePath = scanner.next();
filePath.replace("\\", File.separator);
return filePath;
}
public void fileCopy () throws IOException{
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File sourceFile = new File(getSourceFilePath());
File targetFile = new File(getTargetFilePath());
if (!sourceFile.exists()) {
System.out.println("souce file not exists.");
System.exit(1);
}
InputStream input = new FileInputStream(sourceFile);
OutputStream output = new FileOutputStream(targetFile);
if((input!=null)&&(output!=null)){
int temp=0;
while((temp=input.read())!=(-1)){
output.write(temp);
}
}
input.close();
output.close();
}
public void zipDecompress() throws IOException {
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File souceFile = new File(getSourceFilePath());
File outFile = null;
ZipFile zipFile = new ZipFile(souceFile);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(
souceFile));
ZipEntry entry = null;
InputStream input = null;
OutputStream output = null;
while ( (entry = zipInput.getNextEntry()) != null) {
System.out.println("解压缩" + entry.getName() + "文件");
outFile = new File(getTargetFilePath());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if(!outFile.exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
}
public void zipCompress() throws IOException {
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File sourceFile = new File(getSourceFilePath());
File zipFile = new File(getTargetFilePath());
InputStream input = new FileInputStream(sourceFile);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
zipOut.close();
}
public static void main(String[] args) {
MenuTest menuTest = new MenuTest();
Integer menu = 0;
System.out.println("Please input a number to select a menu:");
System.out.println("1.Copy File 2.Comress File 3.Decomress File 4.quit");
Scanner scanner = new Scanner(System.in);
menu = scanner.nextInt();
try {
switch (menu) {
case 1:
menuTest.fileCopy();
break;
case 2:
menuTest.zipCompress();
break;
case 3:
menuTest.zipDecompress();
break;
default :
System.exit(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}