`
lxtc2014
  • 浏览: 10702 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

网易2013校招笔试题--关于IO

阅读更多
此题大意为:读出文件夹下的所有txt文件,并且,将各个文件的内容输出到另外一个txt文件中去。
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());
				}
			}
		}
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics