此题大意为:读出文件夹下的所有txt文件,并且,将各个文件的内容输出到另外一个txt文件中去。
class="java" name="code">
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
public class Test22 {
public static void main(String[] args) throws IOException {
readAndWirte("liangxiaodirroot","C:\\Users\\shown\\Workspaces\\MyEclipse 8.6\\Test\\test.txt");
}
public static void readAndWirte(String dirName, String fileName) throws IOException{
File dirRoot = new File(dirName);
for (File file : dirRoot.listFiles()) {
if (file.isDirectory()) {
System.out.println(file.getAbsolutePath());
readAndWirte(file.getAbsolutePath(),fileName);
}else{
System.out.println(file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file.getAbsoluteFile());
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
String tmp = null;
raf.seek(raf.length());
raf.write((file.getName()+"\r\n").getBytes());
while ((tmp = br.readLine())!=null) {
raf.seek(raf.length());
raf.write((tmp+"\r\n").getBytes());
}
}
}
}
}