今天实习第三天,老大给了我一个需求,把一个工程的src目录下的文件夹及文件复制到另外一个文件夹,并且限制文件大小,如果过大,则提示出错信息。
以下是我的解题思路:
1:建一个文件复制工具类
2:同包下,建有一个main方法的类,
3:在cmd中输入 java -jar xxx.jar path1 path2; 即可完成复制
步骤:
1:
package filepath;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FilePath {
//当前的工程下的路径
public static String getProjectPath()
{
return System.getProperty("user.dir").toString()+"\\";
}
// 返回当当前工程下的所有文件夹及文件名
public static String[] getProjectPathFiles()
{
File file = new File(System.getProperty("user.dir").toString()+"\\");
String[] strlist = file.list();
return strlist;
}
//编译之后src下的文件路径,根据工程路径拼接
public static String getSrcPath()
{
return System.getProperty("user.dir").toString()+"\\bin\\";
}
//根据反射获取编译之后的src下面的文件路径
public static String getSrcPath(Class<?> class1)
{
String path = class1.getClassLoader().getResource("").toString();
// System.out.println("编译后src路径:"+path);//file:/D:/dev/workspase2Spring/XMLreader/bin/
int m = path.indexOf("/");// file:/<----点位到file:后面的反斜杠
path = path.substring(m + 1);// 从反斜杠之后的一位开始截取字符串
// System.out.println("编译后src路径:"+path);
return path;
}
//根据反射获取编译之后包下面的文件路径
public static String getPackagePath(Class<?> class1)
{
//获取包路径
String thisPackagePath=class1.getResource("").toString();
// System.out.println("路径:"+thisPackagePath);
int m=thisPackagePath.indexOf("/");//去掉前面的file:
thisPackagePath=thisPackagePath.substring(m+1);
// System.out.println("路径:"+thisPackagePath);
return thisPackagePath;//返回当前包返回的路径。
}
public static String getProjectPath(Class<?> class1)
{
//获取src路径
String path = class1.getClassLoader().getResource("").toString();
// System.out.println("编译后src路径:"+path);//file:/D:/dev/workspase2Spring/XMLreader/bin/
//文件定位到src路径
File file=new File(path);//定位到这个目录下面
path=file.getParent();//返回src路径的父路径,也就是工程路径
// 结果 path=file:\D:\dev\workspase4javaBasis\FilePath
int m = path.indexOf("\\");// file:\<----点位到file:后面的反斜杠
path = path.substring(m + 1);// 从反斜杠之后的一位开始截取字符串
// System.out.println("编译后src路径:"+path);
return path;
}
// 结合方法
public static void copyDir(String sourcePath, String newPath) throws IOException {
File file = new File(sourcePath);
String[] filePath = file.list();
if (!(new File(newPath)).exists()) {
(new File(newPath)).mkdir();
}
for (int i = 0; i < filePath.length; i++) {
if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
}
if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
// 这步是进行判断,如果要复制的文件过大,则显示文件复制失败,并跳出本次复制过程
File fi = new File(sourcePath+file.separator+filePath[i]);
FileInputStream in = new FileInputStream(fi);
int a = in.available();
byte[] buffer = new byte[1024000];
if(a > buffer.length){
System.out.println("这个"+fi.getName()+"文件过大,复制失败");
in.close();
continue;
}
copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
}
}
}
public static void copyFile(String oldPath, String newPath) throws IOException {
File oldFile = new File(oldPath);
File file = new File(newPath);
FileInputStream in = new FileInputStream(oldFile);
FileOutputStream out = new FileOutputStream(file);
//int a = in.read();
int a = in.available();
byte[] buffer=new byte[1024000];
int readByte = 0;
if(a <= buffer.length){
while((readByte = in.read(buffer)) != -1){
String fileStr = oldFile.toString();
String fileStr1 = fileStr.replace(File.separator, "-");
String[] strs = fileStr1.split("-");
String name = strs[strs.length-1];
System.out.print("正在复制:"+name+"; ");
System.out.print("复制成功, ");
System.out.print("大小是: "+readByte);
System.out.println();
out.write(buffer, 0, readByte);
}
in.close();
out.close();
}else{
System.out.println("文件过大,复制失败。。。");
in.close();
out.close();
}
}
}
2:
package filepath;
import java.io.File;
import java.io.IOException;
public class TestPath {
public static void main(String[] args) throws IOException {
if(!(new File(args[1])).exists()){
(new File(args[1])).mkdir();
System.out.println("新建文件夹成功!");
}
FilePath.copyDir(args[0], args[1]);
}
}
或者你可以新建一个类,或进行junit单元测试,测试步骤1的各种静态方法,如下:
package com.lan.testMyJar;
import filepath.FilePath;
public class Test
{
public static void main(String[] args)
{
System.out.println("当前的包路径为:"+FilePath.getPackagePath(Test.class));
System.out.println("当前的工程路径为:"+FilePath.getProjectPath());
System.out.println("当前的工程路径为:"+FilePath.getPackagePath(Test.class));
System.out.println("编译后的src路径为:"+FilePath.getSrcPath());
System.out.println("编译后的src路径为:"+FilePath.getSrcPath(Test.class));
}
}
3:然后在cmd的控制台输入两个参数,分别是原路径和新目标路径,就可以了