2

次のコードでは、ケース2はテキストファイルを通常の順序で印刷しますが、正常に機能します。ただし、ケース3の場合はまったく同じですが、メソッド.reverse()を使用すると、どこからともなく改行が追加されます。(私は多くの繰り返しを切り取ることができることを知っています、私はそれを最初に機能させようとしています。)

例:

マーカス
1244

のように見えます

4421

scuraM

line.separatorの代わりに\rまたは\nを使用すると、まったく同じことがわかります。もちろん、それを取り除くと、1本の線が壊されてしまいます。

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);
}
}
4

1 に答える 1

4

これ\r\nは、が新しい行であるが、\n\r2つの新しい行であるためです。

于 2012-09-20T20:28:23.990 に答える