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();
}
}
}