查看目录下的所有文件,并输出至指定文件

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

public class Translate {

	public static void main(String[] args) {
		Translate.multiFile();
	}
 
	/**
	 * 输出文件
	 * @param path
	 * @param content
	 */
	public static void write(String path, String content) {
		String s = new String();
		String s1 = new String();
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("文件存在");
			} else {
				System.out.println("文件不存在,正在创建...");
				if (f.createNewFile()) {
					System.out.println("文件创建成功!");
				} else {
					System.out.println("文件创建失败!");
				}
			}
			// 先读取原文件,再追加至末尾
			BufferedReader input = new BufferedReader(new FileReader(f));
			while ((s = input.readLine()) != null) {
				s1 += (s + "\n");
			}
			System.out.println("文件内容:" + s1);
			input.close();
			s1 += content;
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(s1);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取目录下的所有文件
	 */
	public static void multiFile() {
		System.out.println("执行multiFile");
		// 读取文件内容
		String path = "F:/veeasygenerate/src/";
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			File file = new File(path);
			File[] tempList = file.listFiles();
			System.out.println("该目录下对象个数:" + tempList.length);
			for (int i = 0; i < tempList.length; i++) {
				if (tempList[i].isFile()) {
					// 输出至文件,记录相关信息
					Properties pp = System.getProperties();
					String newLine = pp.getProperty("line.separator");
					StringBuffer str_format = new StringBuffer();
					str_format.append("<filename=\"");
					str_format.append(tempList[i].getName());
					str_format.append("\"></file>");
					str_format.append(newLine);
					write("F:/veeasygenerate/result.xml", str_format.toString());
					// 
					System.out.println("文 件:" + tempList[i]);
					fos = new FileOutputStream("F:/veeasygenerate/dst/" + tempList[i].getName());
					osw = new OutputStreamWriter(fos, "GB2312");
					// read file
					if (tempList[i].exists()) {
						FileInputStream fi = new FileInputStream(tempList[i]);
						InputStreamReader isr = new InputStreamReader(fi, "utf-8");
						BufferedReader bfin = new BufferedReader(isr);
						String rLine = "";
						while ((rLine = bfin.readLine()) != null) {
							// write file
							osw.write(rLine + "\n");
							osw.flush();
						}
						bfin.close();
						isr.close();
					}
					osw.close();
					fos.close();
				}
				if (tempList[i].isDirectory()) {
					System.out.println("文件夹:" + tempList[i]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值