黑马程序员技术交流社区
标题:
io流复制文件夹
[打印本页]
作者:
萧萧0_0
时间:
2016-9-16 20:59
标题:
io流复制文件夹
io流复制文件夹,最简洁的写法,求教
要考虑是否是文件夹,复制文件时,把文件的后缀改成".java",文件名在原文件名的后面加"(copy)"
作者:
somnus-sir
时间:
2016-9-16 21:23
package com.whn.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Test8 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要复制的文件夹: ");
String from = sc.nextLine();
File fromFile = new File(from);
System.out.println("请输入你要复制到哪个文件夹下: ");
String to = sc.nextLine();
File toFile = new File(to);
copy(fromFile,toFile);
System.out.println("复制完成");
}
/**
* 复制方法
* @param fromFile
* @param toFile
* @throws Exception
*/
private static void copy(File fromFile, File toFile) throws Exception {
File file = new File(toFile,fromFile.getName());
file.mkdirs();
File[] files = fromFile.listFiles();
for (File subFile : files) {
if (subFile.isDirectory()) {
copy(subFile, file);
}else{
//把文件的后缀改成".java",文件名在原文件名的后面加"(copy)"
String name = subFile.getName();
String[] names = name.split("\\.");
String firstName = names[0]+"copy" ;
String lastName = ".java";
String newName = firstName+lastName;
//复制文件
FileInputStream fis = new FileInputStream(subFile);
FileOutputStream fos = new FileOutputStream(new File(file,newName));
int a ;
while((a=fis.read()) !=-1){
fos.write(a);
}
fis.close();
fos.close();
}
}
}
}
作者:
小东先森
时间:
2016-9-16 22:02
public class Test {
public static void main(String[] args) throws IOException {
File srcFile = new File("E:\\Java课程\\Java01");
File destFile = new File("E:\\Java课程\\Java02");
copyFileAndRename(srcFile, destFile);
System.out.println("复制完毕...");
}
public static void copyFileAndRename(File srcFile, File destFile) throws IOException {
if(srcFile.isFile() || srcFile.getName().endsWith(".txt")) {
FileInputStream in = new FileInputStream(srcFile);
File lastdestFile = new File(destFile.getAbsolutePath().replace(".txt",".java"));
FileOutputStream out = new FileOutputStream(lastdestFile);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) != -1) {
out.write(b, 0, i);
}
in.close();
out.close();
System.out.println(srcFile.getName()+"复制完成...");
}else {
File[] arrFile = srcFile.listFiles();
for (File newfile : arrFile) {
String newDestFile = destFile.getAbsolutePath()+"\\"+newfile.getName();
if(newfile.isDirectory()) {
new File(newDestFile).mkdirs();
}
copyFileAndRename(newfile,new File(newDestFile));
}
}
}
}
作者:
Aggie_Li
时间:
2016-9-16 22:53
public class Test {
public static void main(String[] args) throws IOException {
File srcFile = new File("E:\\1课程\\Java_1");
File destFile = new File("E:\\1课程\\Java_2");
copyFileAndRename(srcFile, destFile);
System.out.println("完成");
}
public static void copyFileAndRename(File srcFile, File destFile) throws IOException {
if(srcFile.isFile() || srcFile.getName().endsWith(".txt")) {
FileInputStream in = new FileInputStream(srcFile);
File lastdestFile = new File(destFile.getAbsolutePath().replace(".txt",".java"));
FileOutputStream out = new FileOutputStream(lastdestFile);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) != -1) {
out.write(b, 0, i);
}
in.close();
out.close();
System.out.println(srcFile.getName()+"完成");
}else {
File[] arrFile = srcFile.listFiles();
for (File newfile : arrFile) {
String newDestFile = destFile.getAbsolutePath()+"\\"+newfile.getName();
if(newfile.isDirectory()) {
new File(newDestFile).mkdirs();
}
copyFileAndRename(newfile,new File(newDestFile));
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2