你的意思是不是A.java文件可能存在文件夹A下,也可能存在文件夹A的子文件夹中??
用我的代码试试
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class T2 {
public static void main(String[] args) {
File fileA = new File("c:\\A");
if(!fileA.exists()){
System.out.println("未在C盘检测到文件夹A...自动创建A");
fileA.mkdirs();
}
File fileB = new File("c:\\B");
if(!fileB.exists()){
System.out.println("未在C盘检测到文件夹B...自动创建B");
fileB.mkdirs();
}
String str = "天王盖地虎,携手共甘苦.";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileA+"\\A.java");
fos.write(str.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
try {
BufferedInputStream inBuff = new BufferedInputStream(
new FileInputStream(fileA+"\\A.java"));
BufferedOutputStream outBuff = new BufferedOutputStream(
new FileOutputStream(fileB+"\\B.txt"));
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
outBuff.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("end...");
}
}
这个太简单了,我都不屑于写
试试用递归+IO