0

を含む文字列から書き込まれたテキスト ファイルからデータを読み取ろうとしています\n。その結果、これらの文字は改行として書き込まれています。読み返してみると、

FileInputStreamInputStreamReaderおよびを使用していBufferedReaderます。しかし、一行一行読み込んでいくと、読み手は改行に気付き、それを行末と数えてしまいます。私が本当に欲しいのは、改行を維持しながら行末まで読むことです。実際に文字が書けるか、ファイル作成時に試して\nたのですが、どうやっても普通の改行にならずに書けません。

改行を維持しながらこのファイルを読み込むにはどうすればよいですか?

編集:明確にするために、ここに私が読んでいるデータのサンプルがあります。ご覧のとおり、各行はそれぞれの行にありArgsます。これは無視したい改行です。各コマンド (dt、e、b、c、o など) に独自の行が必要です。理想的には次のようになります。

o Desc: Opens a file or folder#Args: [<handle or path>] -- the path of the file or folder#(<file name>) -- the name of the file to open. Omit if you are opening a folder.

無視したい改行文字を#記号に置き換えました。ただし、ファイルをプログラムに読み戻すときにSystem.out.println();、行が置き換えられた改行が#正しく表示されるようにしたいのです。

dt Desc: Toggles the state of debug mode.
e Desc: Exits QuiConsole
b Desc: Goes to the specified webpages.
    Args: [<URLs>] -- the URLs of the webpages to visit, separated by spaces
c Desc: Clears the console window.
o Desc: Opens a file or folder
    Args: [<handle or path>] -- the path of the file or folder
          (<file name>) -- the name of the file to open. Omit if you are opening a folder
l Desc: Searches the web
    Args: [<query>] -- the query to search for
h Desc: Displays a list of all the current handles.
    Args: (<handle names>) -- displays information for only the specified handles.
info Desc: Displays information about QuiConsole
help Desc: Displays a list of all commands, their descriptions and their arguments, if any exist.
    Args: (<command names>) -- Displays information for the specified command only. Ignores any invalid command names.
s Desc: Shuts down, restarts, hibernates or logs off the computer.
    Args: [s,r,h,l] -- shuts down, restarts, hibernates or logs off the computer respectively.
          (<integer value>) -- the number of seconds to delay the action.
dh Desc: Deletes all handles
    Args: (<handle names>) -- deletes only the specified handles
r Desc: Runs the specified program
    Args: [<names>] -- the names of the programs to run. Separate programs by a space.
wa Desc: Displays the answer from Wolfram|Alpha
    Args: [<query>] -- the query to send to Wolfram|Alpha
ch Desc: Modifies a handle
    Args: [<handle names>] -- the handle to change
          [<new value>] -- the new value to assign to the handle
ah Desc: Adds the specified handles.
    Args: [handle] -- name of the handle.
          [value] -- value for the handle
ds Desc: Displays the state of debug mode.
dhist Desc: Deletes command history
4

2 に答える 2

0

実際に\とnの文字が書けるか試してみた

できますが、それは改行を構成しません。改行は、CR (0x0d)、LF (0x0a)、または CRLF (0x0d0a) のいずれかです。文字および文字列リテラルの CR\rの Javaエスケープです。出力ファイルでは発生しません。同様に\n、文字リテラルおよび文字列リテラルの LF の Java エスケープも同様です。

'\r'、'\n'、または '\r\n' に遭遇するまで、を使用せずにファイルを読み取るだけでよく、それらをリテラル文字としてではなく Java エスケープ シーケンスとして使用します。readLine()

しかし、あなたが本当にやりたいことは、行を読み、スペースで始まる行を前の行に追加することであり、それらを新しい行として扱うのではないように思えます。

于 2013-12-11T04:09:10.143 に答える