リダイレクトを実行できないため,「 ls -l 」コマンドを実行し,その結果をファイル temp.txt に出力し,かつ,作成したファイルの内容を画面に出力します.
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
String line;
Process process = Runtime.getRuntime().exec("ls -l");
InputStream i_s = process.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(i_s));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("temp.txt")));
while ((line = in.readLine()) != null)
out.println(line);
in.close();
out.close();
process = Runtime.getRuntime().exec("cat temp.txt");
i_s = process.getInputStream();
in = new BufferedReader(new InputStreamReader(i_s));
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
}
}
(出力) −ファイルの内容−
-a--rw- 114 Sep 8 2001 11:41 file1
-a--rw- 28 Sep 8 2001 11:05 file2
-a--rw- 30 Sep 8 2001 11:05 file3
-a--rwx 30 Sep 7 2001 10:16 jc1.bat
-a--rwx 11 Sep 7 2001 10:16 je1.bat
-a--rw- 854 Sep 8 2001 11:41 member~1.cla
-a--rw- 0 Sep 9 2001 09:08 temp
-a--rw- 0 Sep 9 2001 09:08 temp.txt
-a--rw- 127 Sep 9 2001 08:30 test.cpp
-a--rwx 299710 Sep 9 2001 08:30 test.exe
-a--rw- 1209 Sep 9 2001 09:04 test~1.cla
-a--rw- 712 Sep 9 2001 09:04 test~1.jav