IO流--创建文件夹,复制移动文件
创建多级文件夹
final String ROOTPATH = "/Users/mac/Downloads"; // 默认文件下载的位置 @Test //创建多级文件夹 public void test1() { String path = "com.id1.3rd"; // 这是文件夹,用.分割 String[] split = path.split("\\."); String fullPath = ROOTPATH; for (String s : split) { fullPath += "/" + s; File file = new File(fullPath); file.mkdir(); }
复制移动文件到文件夹
/**
* @param file 被移动的文件
* @param dictionary 移动到的目标文件夹
*/
public void copyAndRemoveFile1(String file, String dictionary) {
try {
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(dictionary+file.substring(file.lastIndexOf("/"),file.length()));
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test1() {
String file = "/Users/mac/Desktop/artifactIdApplication.java"; // 必须是文件,不可以是文件夹
String dictionary = "/Users/mac/Desktop/未命名文件夹"; // 文件夹
copyAndRemoveFile1(file,dictionary);
}