package cn.net.cdsz.ccb.test;
import cn.hutool.core.util.StrUtil;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws Exception,IOException{
// testCode();
//getFileContent();
// getFileContentTwo();
// getFileContentThree();
List<File> allFile = getAllFile("G:\\百度网盘\\xxx\\yyy");
allFile.stream().forEach(e->{
try {
File file = new File("G:\\百度网盘\\xxx\\ccc\" + e.getPath().replace("G:\\百度网盘\\xxx\\yyy\\", "").replace("\\", " "));
copyFileUsingStream(e,file);
System.out.println(e.getName()+"copy 完成!");
} catch (IOException e1) {
e1.printStackTrace();
}
});
System.out.println("完毕!");
}
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
/**
* 获取指定文件夹下所有文件,不含文件夹里的文件
*
* @param dirFilePath 文件夹路径
* @return
*/
public static List<File> getAllFile(String dirFilePath) {
if (StrUtil.isBlank(dirFilePath))
return null;
return getAllFile(new File(dirFilePath));
}
/**
* 获取指定文件夹下所有文件,不含文件夹里的文件
*
* @param dirFile 文件夹
* @return
*/
public static List<File> getAllFile(File dirFile) {
// 如果文件夹不存在或着不是文件夹,则返回 null
if (Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile())
return null;
File[] childrenFiles = dirFile.listFiles();
if (Objects.isNull(childrenFiles) || childrenFiles.length == 0)
return null;
List<File> files = new ArrayList<>();
for (File childFile : childrenFiles) {
// 如果是文件,直接添加到结果集合
if (childFile.isFile()) {
files.add(childFile);
}
//以下几行代码取消注释后可以将所有子文件夹里的文件也获取到列表里
else {
// 如果是文件夹,则将其内部文件添加进结果集合
List<File> cFiles = getAllFile(childFile);
if (Objects.isNull(cFiles) || cFiles.isEmpty()) continue;
files.addAll(cFiles);
}
}
return files;
}
//方法一
public static void getFileContent() throws IOException{
FileReader fr = new FileReader("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
BufferedReader br = new BufferedReader(fr);
while(br.ready()){
System.out.println(br.readLine());
}
}
//方法二
public static void getFileContentTwo() throws IOException{
String encoding="GBK";
File file=new File("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
//判断文件是否存在
if(file.isFile() && file.exists()) {
//考虑到编码格式
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
read.close();
}else {
System.out.println("该文件不存在");
}
}
//方法三
public static void getFileContentThree() throws IOException{
Scanner in = new Scanner(new File("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt"));
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}
/**
*
*@param:
*@return:获得txt文件编码格式
*@Date:2019/8/26
*/
public static void testCode() throws Exception{
BufferedInputStream bin=new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt"));
int p=(bin.read()<<8)+bin.read();
String code=null;
switch (p){
case 0xefbb:
code="UTF-8";
break;
case 0xfffe:
code="Unicode";
break;
case 0xfeff:
code="UTF-16BE";
break;
default:
code="GBK";
}
System.out.println(String.format("测试文件的编码格式是:"+code));
}
}
收藏一下,