java 读取txt文件的内容再输出到txt文件中

蔚落 2023-02-18 01:58 101阅读 0赞

读取文件格式为txt 文件类容为:

  1. P0001-0001-0001 <TAXON_KEY ID="P0001-0001-0001"><Node num="1" id="0" parentnum="0"><num>1</num><text>茎、叶细胞薄壁;叶裂瓣横切面中央为1-2个大形细胞</text><taxon ID="P0001-0001-0001-0001"><zh>藻苔</zh><sci>T. lepidozioides</sci></taxon></Node><Node num="1" id="1" parentnum=""><num>1</num><text>茎、叶细胞壁明显加厚;叶裂瓣横切面中央有多数细胞</text><taxon ID="P0001-0001-0001-0002"><zh>角叶藻苔</zh><sci>T. ceratophylla</sci></taxon></Node></TAXON_KEY>
  2. P0001-0002-0001 <TAXON_KEY ID="P0001-0002-0001"><Node num="1" id="0" parentnum="0"><num>1</num><text>植物体粗短;叶扁椭圆形,长度短于宽度</text><taxon ID="P0001-0002-0001-0001"><zh>裸蒴苔</zh><sci>H. blumii</sci></taxon></Node><Node num="1" id="1" parentnum=""><num>1</num><text>植物体纤长;叶长椭圆形,长度长于宽度</text><taxon ID="P0001-0002-0001-0002"><zh>圆叶裸蒴苔</zh><sci>H. mnioides</sci></taxon></Node></TAXON_KEY> ....

输入文件名为空格左边的值 内容为空格右边值

  1. public void test() {
  2. try {
  3. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("F:/desktop/aaa.txt")), "UTF-8"));
  4. String lineTxt;
  5. while ((lineTxt = br.readLine()) != null) {
  6. String line = lineTxt.trim();
  7. String[] text = line.split("\t");
  8. createFile("F:/desktop/txt/" + text[0] + ".txt");
  9. writeFile("F:/desktop/txt/" + text[0] + ".txt", text[1]);
  10. }
  11. } catch (Exception e) {
  12. System.err.println("read errors :" + e);
  13. }
  14. }
  15. private static void createFile(String filePath) {
  16. try {
  17. File file = new File(filePath);
  18. if (!file.exists()) {
  19. file.createNewFile();
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. private static void writeFile(String filePath, String Content) {
  26. FileWriter fw = null;
  27. try {
  28. fw = new FileWriter(filePath, true);
  29. fw.write(Content);
  30. fw.flush();
  31. fw.close();
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. } finally {
  35. try {
  36. if (fw != null) {
  37. fw.flush();
  38. fw.close();
  39. }
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }

发表评论

表情:
评论列表 (有 0 条评论,101人围观)

还没有评论,来说两句吧...

相关阅读