【问题标题】:Java StringBuffer works fine, reverse() adds linebreaksJava StringBuffer 工作正常,reverse() 添加换行符
【发布时间】:2012-09-13 06:37:24
【问题描述】:

在下面的代码中,案例 2 以正常顺序打印文本文件,它工作正常。但在案例 3 中,完全相同,但使用方法 .reverse(),它突然添加了换行符。 (我知道我可以减少很多重复,我正在努力让它先发挥作用。)

例子:

马库斯
第1244章

看起来像

4421

scuram

使用 \r 或 \n 而不是 line.separator 给了我同样的东西。把它拿走,当然会给我一条被打碎的线。

import java.lang.*;
import java.util.*;
import java.io.*;

public class H5 {
public static void main(String args[]) {
    Scanner stdin = new Scanner(System.in);
    Scanner stdin2 = new Scanner(System.in);
    String filePath = null;
    int selection;
    boolean repeat = true;
    FileInputStream f = null;
    
    do {  
        System.out.println("\n0 - Exit\n1 - Select file\n2 - Display\n3 - Reverse\nSelect option: ");
        selection = stdin.nextInt();
        switch (selection) {
        case 0:  System.out.println("\nThank you. Goodbye.");
                 repeat = false;
                 break;
        case 1:  System.out.println("\nFile path: ");
                 filePath = stdin2.nextLine();
                 
                 try {f = new FileInputStream(filePath);}
                 catch (Exception d) { System.out.println(d);}
                 
                 break;
        case 2:  try {
                    f = new FileInputStream(filePath);
                    DataInputStream d = new DataInputStream(f);
                    BufferedReader b = new BufferedReader(new InputStreamReader(d));
                    StringBuffer strbuf = new StringBuffer(200000);

                    String strLine;
                    while ((strLine = b.readLine()) != null) {
                         strbuf.append(strLine).append(System.getProperty("line.separator"));
                     }
                    System.out.println(strbuf);
                 }
                 catch(NullPointerException npe) {
                    System.out.println("\nPlease select a file first.");
                 }
                 catch(Exception e) {
                    System.out.println(e);
                 }
                 break;
       case 3:  try {
                    f = new FileInputStream(filePath);
                    DataInputStream d = new DataInputStream(f);
                    BufferedReader b = new BufferedReader(new InputStreamReader(d));
                    StringBuffer strbuf = new StringBuffer(200000);

                    String strLine;
                    while ((strLine = b.readLine()) != null) {
                         strbuf.append(strLine).append(System.getProperty("line.separator"));
                     }
                    strbuf.reverse();
                    System.out.println(strbuf);
                 }
                 catch(Exception k) {
                     System.out.println(k);
                 }
                 break;
        default: System.out.println("\nInvalid input. Please select from the following: ");
                 break;
        }
    } while(repeat);
}
}

【问题讨论】:

  • 可以使用StringBuilder时请不要使用StringBuffer。
  • 检查字符串中的实际字符。您可能会发现第 1 行的末尾有两个行尾字符,当您将它们反转时,它会发生意想不到的事情。
  • @PeterLawrey 这是一项任务。我别无选择。

标签: java stream buffer stringbuffer


【解决方案1】:

那是因为\r\n 是一个新行,而\n\r 是两个新行。

【讨论】:

  • 我试过 strbuf.append(strLine).append("\b\b").append(System.getProperty("line.separator"));看看我是否可以在新衬里之前删除回车,但它不起作用。
  • 不能添加字符来删除字符。你想做什么?
  • 我正试图让它反向写出完全相同的东西。
  • 尝试使用\n的单字符换行符,向前和向后相同。如果您也需要,您可以在反转后将其转换为\r\n
  • 我在您的第一条评论后尝试过,认为它不起作用,但我刚刚发现我一直在运行我的程序而没有在运行之间点击 NetBeans 中的保存...尴尬,感谢您的帮助.
猜你喜欢
  • 2018-02-28
  • 1970-01-01
  • 2021-11-07
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
相关资源
最近更新 更多