package file;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class readFile {
//文件读取
public static void read(String path){
File file = new File(path);
if (!file.isDirectory()){
System.out.println("文件");
System.out.println("path="+file.getPath());
System.out.println("absolutepath="+file.getAbsolutePath());
System.out.println("name="+file.getName());
List<String> list = readFile(file);
if (list.size()!=0){
writeFile(path+"\\abc.txt",list);
}
}else if(file.isDirectory()){
System.out.println("文件夹");
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
File readFile = new File(path+"\\"+fileList[i]);
if (!readFile.isDirectory()){
System.out.println("path="+readFile.getPath());
List<String> list = readFile(readFile);
//写入一个大文件中
if (list.size()!=0){
writeFile(path+"\\abc.txt",list);
}
}else if (readFile.isDirectory()){
read(path+"\\"+readFile);
}
}
}
}
//文件读取
public static List<String> readFile(File file){
List<String> list = new ArrayList<String>();
BufferedReader reader = null;
InputStreamReader ior = null;
String string = null;
try {
ior = new InputStreamReader(new FileInputStream(file),"UTF-8");
reader = new BufferedReader(ior);
while ((string = reader.readLine())!=null){
list.add(string);
System.out.println(string);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ior!=null){
try {
ior.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
//文件写入
public static void writeFile(String path,List<String> list){
File file = new File(path);
//FileOutputStream outStr = null;
//BufferedOutputStream buff = null;
BufferedWriter bw = null;
FileWriter fw = null;
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//outStr = new FileOutputStream(file,true);
//buff = new BufferedOutputStream(outStr);
fw = new FileWriter(file,true);
bw = new BufferedWriter(fw);//在这个位置更换为自己想使用的换行符
String property = System.getProperty("line.separator");
for (int i = 0; i < list.size(); i++) {
bw.write(list.get(i));
bw.write(property);
//bw.newLine();
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//buff.close();
if (bw!=null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
/* if (outStr!=null){
outStr.close();
}*/
if (fw!=null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//File类提供的方法
public static void test() {
String path = "E:\\test";
File file = new File(path);
file.getName();//获取文件的名称
file.canRead();//判断文件是否为可读的
file.canWrite();//判断文件是否为可写
file.exists();//判断文件是否存在
file.length();//获取文件长度(以字节为长度)
file.getAbsolutePath();//获得文件的绝对位置
file.getParent();//获取文件的父路径
file.isFile();//判断未见是否存在
file.isDirectory();//判读文件是否是一个目录
file.isHidden();//判断文件是否为隐藏文件
file.lastModified();//获取文件最后修改时间
/*String str = "E:\\test1\\test.txt";
int i = str.lastIndexOf("\\");
//System.out.println(i);
String s = str.substring(0,i);
String substring = str.substring((i + 1), str.length());
File file = new File(str);
if (!file.getParentFile().exists()){
System.out.println(file.getParentFile());
file.getParentFile().mkdirs();
}
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}*/
/*if (!file.exists()){
file.mkdirs();
}
file = new File(str);
System.out.println(file.getParentFile());
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
public static void main(String[] args) {
String path = "E:\\test";
read(path);
}
}