本来打算今天写的,只是时间不够了,明天补上吧。今天留个标题吧。(4月4日写)
CopyImage
以上代码只是简单实现了一下,主要是了解这个思想就可以了。
package
copyImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
public class CopyImage {
/**
* @param args
* @author 文化遗产新编
* @since 2011/4/5
*/
public static void main(String[] args){
// TODO Auto-generated method stub
BufferedReader br; // 读取屏幕输入的一行(路径)
String copyedPath; // 存放屏幕上输入的源文件的路径
File file; // 存放输入的源文件
FileInputStream fileIn; // 读取诸如图像数据之类的原始字节流
Vector < Integer > data; // 存放源图片的字节
boolean end = false ; // 标识:是否读到文件末尾
String finalPath; // 存储目标文件的路径
FileOutputStream fileOut; // 用于写入诸如图像数据之类的原始字节的流
try {
while ( true ){
System.out.println( " 请输入要复制的文件(包括文件名): " );
br = new BufferedReader( new InputStreamReader(System.in));
copyedPath = br.readLine();
file = new File(copyedPath);
if ( ! file.exists()){
System.out.println( " 文件没有找到,请重新输入 " );
continue ;
}
fileIn = new FileInputStream(copyedPath);
data = new Vector < Integer > ();
while ( ! end){
int input = fileIn.read();
if (input == - 1 ){
end = true ;
}
else {
// System.out.print(input+","); // 测试用的
data.add( new Integer(input));
}
}
fileIn.close(); // 关闭输入流
/* 测试用,查看读入的数据
for(int k=0;k<data.size();k++){
System.out.print(data.get(k).intValue()+",");
if(k%10 == 0){
System.out.println();
}
}
*/
System.out.println( " 请输入复制到的文件路径(不包括文件名): " );
// file.getName():返回由此抽象路径名表示的文件或目录的名称。该名称是路径名的名称序列中的最后一个名称。
// 如果路径名的名称序列为空,则返回空字符串。
finalPath = br.readLine() + file.getName();
fileOut = new FileOutputStream(finalPath);
for ( int j = 0 ;j < data.size();j ++ ){
fileOut.write(data.get(j).intValue());
}
fileOut.close(); // 关闭输出流
}
}
catch (IOException ie){
System.out.println( " Error:-- " + ie.toString());
}
catch (Exception e){
System.out.println( " Erroe:-- " + e.toString());
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
public class CopyImage {
/**
* @param args
* @author 文化遗产新编
* @since 2011/4/5
*/
public static void main(String[] args){
// TODO Auto-generated method stub
BufferedReader br; // 读取屏幕输入的一行(路径)
String copyedPath; // 存放屏幕上输入的源文件的路径
File file; // 存放输入的源文件
FileInputStream fileIn; // 读取诸如图像数据之类的原始字节流
Vector < Integer > data; // 存放源图片的字节
boolean end = false ; // 标识:是否读到文件末尾
String finalPath; // 存储目标文件的路径
FileOutputStream fileOut; // 用于写入诸如图像数据之类的原始字节的流
try {
while ( true ){
System.out.println( " 请输入要复制的文件(包括文件名): " );
br = new BufferedReader( new InputStreamReader(System.in));
copyedPath = br.readLine();
file = new File(copyedPath);
if ( ! file.exists()){
System.out.println( " 文件没有找到,请重新输入 " );
continue ;
}
fileIn = new FileInputStream(copyedPath);
data = new Vector < Integer > ();
while ( ! end){
int input = fileIn.read();
if (input == - 1 ){
end = true ;
}
else {
// System.out.print(input+","); // 测试用的
data.add( new Integer(input));
}
}
fileIn.close(); // 关闭输入流
/* 测试用,查看读入的数据
for(int k=0;k<data.size();k++){
System.out.print(data.get(k).intValue()+",");
if(k%10 == 0){
System.out.println();
}
}
*/
System.out.println( " 请输入复制到的文件路径(不包括文件名): " );
// file.getName():返回由此抽象路径名表示的文件或目录的名称。该名称是路径名的名称序列中的最后一个名称。
// 如果路径名的名称序列为空,则返回空字符串。
finalPath = br.readLine() + file.getName();
fileOut = new FileOutputStream(finalPath);
for ( int j = 0 ;j < data.size();j ++ ){
fileOut.write(data.get(j).intValue());
}
fileOut.close(); // 关闭输出流
}
}
catch (IOException ie){
System.out.println( " Error:-- " + ie.toString());
}
catch (Exception e){
System.out.println( " Erroe:-- " + e.toString());
}
}
}
至于实现其他更加详细的功能,比如:将方法封装一下;检测目标文件夹中是否含有目标文件。有的话提示是否覆盖等等,在这里就不一一说了。