输入输出:InputStream & OutputStream
1、文件copy 和 照片的copy:
package com.dayuanit;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你需要的文件名,格式如:copy F://x.txt F://y.txt");
try {
dominate(sc.nextLine());
} catch(IOException io) {
io.printStackTrace();
}
}
public static void copy(String entry[]) throws IOException {
File fountain = new File(entry[1]);
File target = new File(entry[2]);
InputStream input = new FileInputStream(fountain);
byte[] buf = new byte[1024];
int flag;
String msg = "";
while(-1 != (flag = input.read(buf))) {
msg = new String(buf, 0, flag);
System.out.println("文件的长度为:" + flag + "," + "\n" + "文件的内容为:" + msg);
}
OutputStream output = new FileOutputStream(target);
output.write(msg.getBytes());
output.flush();
}
public static void dominate(String order) throws IOException {
String entry[] = order.split(" ");
if("copy" .equals(entry[0])) {
copy(entry);
}
if("cat".equals(entry[0])) {
reproduce(entry);
}
}
public static void reproduce(String entry[]) throws IOException {
File fountain = new File(entry[1]);
File target = new File(entry[2]);
InputStream fis= null;
OutputStream fos = null;
try {
fis = new FileInputStream(fountain);
fos = new FileOutputStream(target);
byte[] buf = new byte[1024];
int flag;
while(-1 != (flag = fis.read(buf))) {
fos.write(buf , 0, flag);
System.out.println("文件的长度为:" + flag);
}
} catch(FileNotFoundException fn) {
fn.getStackTrace();
} catch(IOException io) {
io.getStackTrace();
} finally {
if(null != fis) {
fis.close();
}
if(null != fos) {
fos.close();
}
}
}
}