コンソールから入力を受け取り、それをフラットファイルに出力する単純なクラスを作成しようとしています。
現在、コンソールからの入力が終了したことを示すために、特定の文字列を入力する必要があります。非コンソール ストリームから EOF を読み取るときに NULL を返す BufferedRead オブジェクトを使用していますが、同様のことをコンソールから行いたいと考えています。誰でもこれを行う方法を教えてもらえますか。
キーボードから System.in に送信/入力する必要がある単純なエスケープ シーケンスはありますか?
コードを以下に示します。
package writefilestr;
import java.io.*;
public class WriteFileStr
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// Ensure that the we have a command line parameter
if (args.length != 1)
{
System.out.println("UASAGE: java WriteFileStr filename");
}
else
{
String[] inputBuffer = new String[100];
int stCount = 0;
System.out.println("Input Some Strings:");
try(BufferedReader in = new BufferedReader(new InputStreamReader(System.in)))
{
String st = new String();
boolean eof = false;
do
{
if (!(st = in.readLine()).equalsIgnoreCase("EOF"))
{
inputBuffer[stCount++] = st;
}
else
{
eof = true;
}
} while(!eof);
}
catch (IOException e)
{
System.out.println(e);
}
try(BufferedWriter wr = new BufferedWriter(new FileWriter(args[0])))
{
for (int i = 0; i < stCount; i++)
{
wr.write(inputBuffer[i]);
wr.newLine();
}
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException : "+e);
}
catch (IOException e)
{
System.out.println("IOException : "+e);
}
}
}
}