import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FindAllTxtFile {
private static String content;
public static void main(String[] args) throws Exception {
// 递归显示C盘下所有文件夹及其中文件
File root = new File("c:/app");
showAllFiles(root);
}
public static String PrintOut(String context, String encodeFormat) {
if (encodeFormat == null || encodeFormat.equals(""))
encodeFormat = "UTF-8";
try {
byte[] content = context.getBytes();
String result = new String(content, encodeFormat);
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
public static String readFile(String path, String encodeFormat) {
try {
String context = "";
InputStreamReader is;
is = new InputStreamReader(new FileInputStream(path), encodeFormat);
BufferedReader br = new BufferedReader(is);
String line;
while ((line = br.readLine()) != null) {
context += line + "\r\n";
// System.out.println(line);
}
br.close();
return context;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 将内容输出为指定格式的文件保存到磁盘上
*
* @param context
* @param path
* @param destEncode
*/
public static void writeFile(String context, String path, String destEncode) {
File file = new File(path);
if (file.exists())
file.delete();
BufferedWriter writer;
try {
FileOutputStream fos = new FileOutputStream(path);
writer = new BufferedWriter(new OutputStreamWriter(fos, destEncode));
writer.append(context);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
final static void showAllFiles(File dir) throws Exception {
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
if (fs[i].isDirectory()) {
try {
showAllFiles(fs[i]);
} catch (Exception e) {
}
} else {
if (fs[i].getPath().endsWith(".java") || fs[i].getAbsolutePath().endsWith(".xml")) {
String filePath;
filePath = fs[i].getAbsolutePath();
System.out.println(fs[i].getName());
//System.out.println(filePath);
content = readFile(filePath, "UTF-8");
// System.out.println(content);
writeFile(content, "c:/test/" + fs[i].getName(), "GBK");
}
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
public class TxtFormatTransfer {
public static String DefaultSrcEncodeFormat = "UTF-8";
public static String DefaultDestEncodeFormat = "GBK";
public static String UnsupportedEncodingExceptionError = "编码格式错误!";
public static String FileNotFoundExceptionError = "文件不存在!";
public static String IOExceptionError = "文件读写错误!";
public static String IsUtf8File = "文件是UTF-8编码格式!";
public static String IsNotUtf8File = "文件不是UTF-8编码格式!";
public static String readFile(String path, String encodeFormat) {
if ((encodeFormat == null || encodeFormat.equals(""))) {
if (isUTF8File(path)) {
encodeFormat = DefaultDestEncodeFormat;
} else {
encodeFormat = DefaultSrcEncodeFormat;
}
}
try {
String context = "";
InputStreamReader isr;
isr = new InputStreamReader(new FileInputStream(path), encodeFormat);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
context += line + "\r\n";
System.out.println(line);
}
br.close();
return context;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(UnsupportedEncodingExceptionError);
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(FileNotFoundExceptionError);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(IOExceptionError);
e.printStackTrace();
}
;
return "";
}
/**
* Unicode、Unicode big endian和UTF-8编码的txt文件的开头会多出几个字节,
* 分别是FF、FE(Unicode),FE、FF(Unicode bigendian),EF、BB、BF(UTF-8)
*
* @param path
* @return
*/
public static boolean isUTF8File(String path) {
File file = new File(path);
byte[] buf = new byte[3];
FileInputStream is = null;
try {
is = new FileInputStream(file);
int read = is.read(buf, 0, 3);
System.out.println("" + buf[0] + " " + buf[1] + " " + buf[2]);
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static String transfer(String context, String encodeFormat) {
if (encodeFormat == null || encodeFormat.equals(""))
encodeFormat = DefaultDestEncodeFormat;
try {
byte[] content = context.getBytes();
String result = new String(content, encodeFormat);
return result;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(UnsupportedEncodingExceptionError);
e.printStackTrace();
}
return "";
}
public static void writeFile(String context, String path, String destEncode) {
File file = new File(path);
if (file.exists())
file.delete();
BufferedWriter writer;
try {
FileOutputStream fos = new FileOutputStream(path, true);
writer = new BufferedWriter(new OutputStreamWriter(fos, destEncode));
writer.append(context);
writer.close();
} catch (IOException e) {
System.out.println(IOExceptionError);
e.printStackTrace();
}
}
public static void writeFile(String context, String path) {
File file = new File(path);
if (file.exists())
file.delete();
Writer writer;
try {
writer = new FileWriter(file, true);
writer.append(context);
writer.close();
} catch (IOException e) {
System.out.println(IOExceptionError);
e.printStackTrace();
}
}
public static void transfer(String srcPath, String destPath, String srcEncode, String destEncode) {
if (destPath == null || destPath.equals(""))
destPath = srcPath;
String context = readFile(srcPath, srcEncode);
context = transfer(context, destEncode);
writeFile(context, destPath, destEncode);
}
public static void transfer(String srcPath, String destPath, String destEncode) {
//if (isUTF8File(srcPath) == false) {
transfer(srcPath, destPath, DefaultSrcEncodeFormat, "GBK");
//}
}
public static void main(String args[]) {
String path1 = "RetrofitSingleton.java";
transfer(path1, path1, "GBK");
}
}