51,411
社区成员
发帖
与我相关
我的任务
分享




public class Test {
public static void main(String[] args) {
String filePath = "D:/1.txt"; // txt文件路径
String writePath = "D:/txt/"; // 要存入的文件夹路径
write(filePath, writePath);
}
private static List<String> txtTransFormChineseStrs(String txtPath) {
List<String> content = new ArrayList<String>();
BufferedReader br = null;
try {
File file = new File(txtPath);
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
br = new BufferedReader(isr);
String lineTxt = null;
while ((lineTxt = br.readLine()) != null)
content.add(lineTxt);
} catch (Exception e) {
System.out.println("文件读取错误!");
return null;
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
System.out.println("无法关闭");
}
}
List<String> chineseStrs = new ArrayList<String>();
for (String str : content) {
for (int i = 0; i < str.length(); i++)
chineseStrs.add(str.substring(i, i + 1));
}
return chineseStrs;
}
private static void write(String txtPath, String writePath) {
List<String> source = txtTransFormChineseStrs(txtPath);
File file = null;
BufferedWriter out = null;
try {
for (int i = 0; i < source.size(); i++) {
file = new File(writePath + source.get(i) + ".txt");
if (!file.exists())
file.createNewFile();
out = new BufferedWriter(new FileWriter(file));
out.write(source.get(i));
out.flush();
}
} catch (IOException e) {
System.out.println("文件写入出错");
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
System.out.println("无法关闭");
}
}
}
}