代码之家  ›  专栏  ›  技术社区  ›  Shubham Jain

从Java中删除文件的最后换行符

  •  2
  • Shubham Jain  · 技术社区  · 7 年前

    我在网上尝试过很多解决方案,但都不管用。

    trimm.replace("\n", "").replace("\r", "");
    

    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    

    http://www.avajava.com/tutorials/lessons/how-do-i-remove-a-newline-from-the-end-of-a-string.html

    https://www.java-forums.org/new-java/22655-removing-last-blank-line-txt-file.html

    我不能用 split() \n 因为很多RAW都有相同的词

        String actual ="ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n" + 
                "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
        try {
              File fout = new File("I:\\demo\\S2.txt");
              FileOutputStream fos = new FileOutputStream(fout);
              BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
              String trimm= actual;
    /*            StringBuilder sb = new StringBuilder(trimm);
                  int lastEnterPosition = trimm.lastIndexOf("\r\n");
                  sb.replace(lastEnterPosition, lastEnterPosition + 1, "");
                  trimm = sb.toString();*/
                  trimm = trimm.replaceAll("[\n\r]+$", "");
                  bw.write(trimm);
                  bw.newLine();
                  bw.close();
                } catch (FileNotFoundException e){
                  // File was not found
                  e.printStackTrace();
                } catch (IOException e) {
                  // Problem when writing to the file
                  e.printStackTrace();
                }
    

    2 回复  |  直到 7 年前
        1
  •  2
  •   Youcef LAIDANI    7 年前

    完整代码

    “ABC 123 1 2 ABC‘123 1 2”2018年1月30日2:34:13:000 AM“DD1 1”2018年1月30日2:56:08:000 AM“EST‘ABC 20180821\r\n”; system.out.println(“替换后开始输入-------------------------------------------------------------------------------”); 系统.out.println(trimm); } ABC 123 1 2 ABC'123 1 2 2018年1月30日2:34:13:000 AM“DD1”2018年1月30日2:56:08:000 AM“EST”ABC 20180821 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

    public static void main(string[]args)throws exception{
    “ABC 123 1 2 ABC‘123 1 2”2018年1月30日2:34:13:000 AM“DD1 1”2018年1月30日2:56:08:000 AM“EST‘ABC 20180821\r\n”;
    
    fileoutputstream fos=新的fileoutputstream(fout);
    bw.写入(trimm);
    }catch(fileNotFoundException E){
    }捕获(IOException E){
    }
    
    
    
    public static void main(string[]args)throws exception{
    
    路径路径=path.get(“path”);
    
    {
    }
    
    
    
    string trimm=“abc 123 1 2 abc”123 1 2“2018年1月30日2:34:13:000am\”dd1 1“2018年1月30日2:56:08:000am\”est“abc 20180821\n”+
    
    files.write(paths.get(“path”),trimm.replacefirst(“[\n\r]+$”,“”).getbytes());
    }
    
    

    这三个密码都给了我:

    完整代码

    public static void main(String[] args) throws Exception {
        String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
                "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
    
        System.out.println("---------------------------------------------------Before replace Start of the input---------------------------------------------------");
        System.out.println(trimm);
        System.out.println("---------------------------------------------------Before replace End of the input---------------------------------------------------");
    
        System.out.println("---------------------------------------------------After replace Start of the input---------------------------------------------------");
        trimm = trimm.replaceFirst("[\n\r]+$", "");
        System.out.println(trimm);
        System.out.println("---------------------------------------------------After replace End of the input---------------------------------------------------");
    
    }
    

    输出:

    ---------------------------------------------------Before replace Start of the input---------------------------------------------------
    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    
    ---------------------------------------------------Before replace End of the input---------------------------------------------------
    ---------------------------------------------------After replace Start of the input---------------------------------------------------
    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    ABC 123|1|2 ABC '123|1|2|"Jan 30 2018  2:34:13:000AM"|dd1|1|"Jan 30 2018  2:56:08:000AM"|EST' ABC 20180821
    ---------------------------------------------------After replace End of the input---------------------------------------------------
    

    Ideone demo


    我尝试了这三种解决方案:

    public static void main(String[] args) throws Exception {
        String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
                "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
    
        try {
            File fout = new File("path");
            FileOutputStream fos = new FileOutputStream(fout);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
            trimm = trimm.replaceAll("[\n\r]+$", "");
            bw.write(trimm);
            //bw.newLine();//<-----------------------note this
            bw.close();
        } catch (FileNotFoundException e) {
            // File was not found
            e.printStackTrace();
        } catch (IOException e) {
            // Problem when writing to the file
            e.printStackTrace();
        }
    }
    

    public static void main(String[] args) throws Exception {
        String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
                "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
    
        Path path = Paths.get("path");
    
        try (BufferedWriter writer = Files.newBufferedWriter(path))
        {
            writer.write(trimm.replaceFirst("[\n\r]+$", ""));
        }
    }
    

    public static void main(String[] args) {
        String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\n" +
                "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018  2:34:13:000AM\"|dd1|1|\"Jan 30 2018  2:56:08:000AM\"|EST' ABC 20180821\r\n";
    
        try {
            Files.write(Paths.get("path"), trimm.replaceFirst("[\n\r]+$", "").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    result

        2
  •  1
  •   geco17    7 年前

    如果知道字符串总是以 \r\n

    String test = "your string that ends with a newline\r\n";
    

    String eol = "\r\n";
    int eolLen = eol.length();
    String tmp = test.substring(0, test.length()-eolLen);
    

    String check = test.substring(test.length() - eolLen);
    if (!eol.equals(check)) {
        throw new Exception(String.format("Expected newline, found %s", check));
    }