我的代码是错误的,尝试保存更改的txt文件,但没有任何反应

时间:2015-02-02 13:16:18

标签: java save load export

嘿朋友们试图在我的数据库中替换一些逗号,有人可以告诉我我的代码有什么问题,已经完成了所有操作,但没有任何事情发生

try {

  BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
   String zeile; try {
    while ((zeile = br.readLine()) != null) {
        zeile = zeile.replaceAll("\",\"", "\uffff")
              .replaceAll(",", "")
              .replaceAll("\uffff", "\",\"");
        System.out.println(zeile);
                    }
        File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
        FileWriter fw = new FileWriter(newTextFile);
     fw.write(zeile);
     fw.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  }
 }
}

3 个答案:

答案 0 :(得分:0)

试试这个:

BufferedReader br = new BufferedReader(new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
String zeile; 
try {
    while ((zeile = br.readLine()) != null) {
        zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "").replaceAll("\uffff", "\",\"");
        System.out.println(zeile);
        fw.write(zeile);
    }
fw.close();
}

之前你正在做什么,你读到文件直到最后一行,只保存最后一行而不是整个文件。

答案 1 :(得分:0)

您正在撰写null,因为在while循环(zeilenull后终止)之前,您不会写。此外,您应该关闭finally块中的所有内容(或使用try-with-resources。此外,我建议您使用PrintWriter,这样您就可以匹配System.out.println()(s)。有点像,

File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");

try (BufferedReader br = new BufferedReader(new FileReader(
        "C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
        PrintWriter fw = new PrintWriter(newTextFile)) {
    String zeile;
    while ((zeile = br.readLine()) != null) {
        zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "")
                .replaceAll("\uffff", "\",\"");
        System.out.println(zeile);
        fw.println(zeile);
    }
} catch (Exception e) {
    e.printStackTrace();
}

答案 2 :(得分:0)

从循环中出来后你正在写字符串,这样字符串应为null,试试这段代码

    try {

        BufferedReader br = new BufferedReader(
                new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
        String zeile;

        try {
            File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
            FileWriter fw = new FileWriter(newTextFile);
            while ((zeile = br.readLine()) != null) {
                zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",",
                        "").replaceAll("\uffff", "\",\"");
                System.out.println(zeile);
                fw.write(zeile);
            }


            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }