写了一个,输入到all.txt里的格式如下:
C:\data\StudentInfo.txt
--------
C:\data\新建 文本文档.txt
(该文件内容为空!)
C:\data\新建文件夹\1.txt
第一行
第二行
代码如下:
import java.io.*;
public class AllTxt {
public static void main(String[] args) {
moveAllContentToOne("C:\\data\\", "C:\\all.txt");
}
public static void moveAllContentToOne(String path, String target) {
File source = new File(path);
File targetfile = new File(target);
moveAllContentToOne(source, targetfile);
}
public static void moveAllContentToOne(File source, File targetfile) {
File[] files = null;
if(source.isDirectory()) {
files = source.listFiles();
for(int i=0; i
}
return;
}
if(source.getName().matches(".*\\.[tT][xX][tT]$")) {
System.out.println(source.getName());
try {
BufferedReader br = new BufferedReader(new FileReader(source));
BufferedWriter bw = new BufferedWriter(new FileWriter(targetfile, true));
bw.write(source.getPath());
bw.newLine();
String str = br.readLine();
if(str == null) {
bw.write("(该文件内容为空!)");
bw.newLine();
}
else {
while(str != null) {
bw.write(str);
bw.newLine();
str = br.readLine();
}
}
bw.newLine();
bw.flush();
bw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
java调用DOS命令。
我可以给你一个思路吧,
就是用IO流迭代地读取每个文件夹下的内容是文件就把它里面的内容读出来,加到你是合成的文件里面,是文件夹就进行迭代。最后就完成了。
不过你是注意,好像有的流是写文件的时候会把原来 的内容给删了,有的不会,具体我不记得了。你自己还是去网上找下吧。
package com;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
private String path = "E:\\workspace\\test\\src\\com\\all.txt";// all.txt路径
private String tempPath = "E:\\workspace\\test\\src\\com\\temp.txt";
private File finalFile = new File(path);
private File tempFile = new File(tempPath);// 临时文件,完成后请删除
public static void main(String[] args) {
Test t = new Test();
File file = new File("E:\\workspace\\test\\src\\com\\");// 指定目录
t.writeAll(file);
// Runtime run = Runtime.getRuntime();
// String cmd = "del E:\\workspace\\test\\src\\com\\temp.txt";
// try {
// Process p = run.exec(cmd);
// p.waitFor();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
public void writeAll(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
writeAll(files[i]);
}
} else if (file.isFile()) {
writeFile(file);
}
}
public void writeFile(File file) {
FileReader fr;
BufferedReader br;
FileReader fr1;
BufferedReader br1;
FileWriter fw;
BufferedWriter bw;
String line;
try {
fr = new FileReader(finalFile);
br = new BufferedReader(fr);
fw = new FileWriter(tempFile);
bw = new BufferedWriter(fw);
line = br.readLine();
while (line != null) {
bw.write(line);
bw.newLine();
line = br.readLine();
}
br.close();
bw.close();
fr = new FileReader(file);
br = new BufferedReader(fr);
fr1 = new FileReader(tempFile);
br1 = new BufferedReader(fr1);
fw = new FileWriter(finalFile);
bw = new BufferedWriter(fw);
line = br1.readLine();
while (line != null) {
bw.write(line);
bw.newLine();
line = br1.readLine();
}
bw.newLine();
bw.write(file.getName());
bw.newLine();
bw.newLine();
line = br.readLine();
while (line != null) {
bw.write(line);
bw.newLine();
line = br.readLine();
}
tempFile.deleteOnExit();
br.close();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}