Created
July 2, 2013 14:10
-
-
Save wuyexiong/5909624 to your computer and use it in GitHub Desktop.
把乱码的GBK编码的文件放到当前data目录下,然后就。。。嘿嘿
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
public class GBK2UTF8 | |
{ | |
static File srcDir = new File("data/"); | |
static File outputDir = new File("ok/"); | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) | |
{ | |
if (!srcDir.isDirectory()) | |
{ | |
return; | |
} | |
File[] fs = srcDir.listFiles(); | |
if (!outputDir.exists()) | |
{ | |
outputDir.mkdirs(); | |
} | |
try | |
{ | |
new GBK2UTF8().parse(fs); | |
} catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
private void parse2UTF_8(File file, File targetFile) throws IOException | |
{ | |
StringBuffer msg = new StringBuffer(); | |
PrintWriter ps = new PrintWriter(new OutputStreamWriter(new FileOutputStream(targetFile, false), "utf8")); | |
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk")); | |
String line = br.readLine(); | |
while (line != null) | |
{ | |
msg.append(line).append("\r\n"); | |
line = br.readLine(); | |
} | |
ps.write(msg.toString()); | |
br.close(); | |
ps.flush(); | |
ps.close(); | |
} | |
private void parse(File[] fs) throws IOException | |
{ | |
for (File file : fs) | |
{ | |
if (!file.isDirectory()) | |
{ | |
File destFile = new File(outputDir, file.getName()); | |
parse2UTF_8(file, destFile); | |
} else | |
{ | |
parse(file.listFiles()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment