62,626
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws IOException {
File srcFile = new File("F:\\test\\test.java");//你需要copy的文件
String relativePath = "\\test\\";//你需要指定相对路径是什么
InputStream is = new FileInputStream(srcFile);//读取文件
String tarPath = "E:\\bak" + relativePath;//E:\\bak是你要考的路径,后面的是你所说的相对路径
File tarF = new File(tarPath);//
tarF.mkdirs();//创建文件夹
File tarFile = new File(tarPath + srcFile.getName());//
tarFile.createNewFile();//创建文件
OutputStream os = new FileOutputStream(tarFile);//输出
int i;
byte[] b = new byte[255];
while ((i = is.read(b)) != -1) {
os.write(b);
}
is.close();
os.flush();
os.close();
}
import java.io.*;
public class CopyFiles {
public static void Copy(String src, String dst) {
File srcFile = new File(src);
File dstFile = new File(dst);
if (!srcFile.exists() || srcFile.isFile()) return;
if (!dstFile.exists() && !dstFile.mkdirs()) return;
File srcChilds[] = srcFile.listFiles();
for (int i=0; i<srcChilds.length; ++i) {
// Generate source and destination path
String srcpath = srcChilds[i].getAbsolutePath();
String dstpath = dstFile.getAbsolutePath() + srcpath.substring(srcFile.getAbsolutePath().length());
if (srcChilds[i].isFile()) {
// Copy file to destination
FileInputStream in = null;
FileOutputStream out = null;
byte buffer[] = new byte[8192];
int bytesRead;
try {
in = new FileInputStream(srcpath);
out = new FileOutputStream(dstpath);
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
} catch (Exception e) {}
if (null != in) try { in.close(); } catch (Exception e) {}
if (null != out) try { out.close(); } catch (Exception e) {}
} else {
// Recursion for directory
Copy(srcpath, dstpath);
}
}
}
public static void main(String[] args) {
if (args.length != 2)
System.err.println("Wrong parameters. Correct usage: java CopyFiles <srcDir> <dstDir>");
else {
Copy(args[0], args[1]);
}
}
}
public static void getAllFile(String rootpath, String filepath,
ArrayList<String> flist, boolean isconsubdir) {
// 获得某个目录下的所有文件列表(可以包括子目录)
String ffullpath = rootpath + File.separator + filepath;
File file = new File(ffullpath);
if (file.exists() && file.isDirectory()) {
String[] fileStr = file.list();
for (int i = 0; i < fileStr.length; i++) {
if (new File(ffullpath + File.separator + fileStr[i])
.isDirectory()) {
if (isconsubdir) {
// 是否包含子目录文件
flist.add("#");// 纪录以下是是目录
flist.add(filepath + File.separator + fileStr[i]);
getAllFile(rootpath, filepath + File.separator
+ fileStr[i], flist, isconsubdir);
}
} else {
flist.add(filepath + File.separator + fileStr[i]);
System.out.println(filepath + File.separator + fileStr[i]);
}
}
}
}
//调用此方法 复制文件函数
public void creatFile(String oldPath, String newPath,String type) {
String temp;
int dirnum = 0;
ArrayList<String> flist = new ArrayList<String>();
ReadFileandDir.getAllFile(oldPath, "", flist, true);//得到某个目录下的全部文件和文件夹
while (flist.size() != 0) {
temp = flist.get(0);
// if (temp.endsWith(type))
// System.out.println(temp);
if (temp.startsWith("#")) {
dirnum++;
} else {
if (dirnum > 0) {
//System.out.println("below is dir");
new File(newPath + temp).mkdir();
dirnum--;
} else {
// 如果是文件则dosometing
if (temp.endsWith(type))
BmpToBit(oldPath + temp);
//BMPReverse(oldPath + temp, newPath + temp);
}
}
flist.remove(0);
}
}
public static void main(String[] arg) {
ReadBMP readBMP = new ReadBMP();
String oldPath = "D:/oldbimp";
String newPath = "d:/bimp";
//readBMP.creatFile(oldPath, newPath,".bmp");
readBMP.BmpToBit("D://11.bmp");
// readBMP.ReadFile("D:\\oldbimp\\Bliss11.bmp","d:\\bimp\\1.txt");
}