1

でのFileDescriptorの静的メンバ外で使用するのFileInputStream&のFileOutputStreamクラスの予期しない動作、:私はそれを直接使用しています。ここ我々は静的変数次ているにしてFileDescriptor.javaのソースコードではJAVA

/** 
    * A handle to the standard input stream. Usually, this file 
    * descriptor is not used directly, but rather via the input stream 
    * known as <code>System.in</code>. 
    * 
    * @see java.lang.System#in 
    */ 
    public static final FileDescriptor in = new FileDescriptor(0); 

    /** 
    * A handle to the standard output stream. Usually, this file 
    * descriptor is not used directly, but rather via the output stream 
    * known as <code>System.out</code>. 
    * @see java.lang.System#out 
    */ 
    public static final FileDescriptor out = new FileDescriptor(1); 

をSystem.outとしてではなく、私はコンストラクタでFileDescriptor.outを使用していたとしても、それはすべてのエラーを与えていない、ということ

import java.io.*; 
public class First 
{ 
    public static void main(String[] args) throws Exception 
    { 
     FileInputStream fis = new FileInputStream(FileDescriptor.out); 
     byte[] b = new byte[8]; 
     System.out.println(fis.read(b));//6 
     for(byte b1: b) 
     { 
      System.out.println(b1); 
     } 
    } 
} 

入力

hello 

出力

6 
    104 
    101 
    108 
    108 
    111 
    10 
    0 
    0 

お知らせ:今すぐ次のプログラムをチェック標準入力ストリームに対して完全に動作します。

チェック1つの以上のプログラム:私はコンストラクタでFileDescriptor.inを使用していたとしても、それはすべてのエラーを与え、ために完璧に動作していない、ということ

import java.io.*; 
public class First 
{ 
    public static void main(String[] args) throws Exception 
    { 
     FileOutputStream fos = new FileOutputStream(FileDescriptor.in); 
     byte[] b = {65, 66, 67}; 
     fos.write(b); 
    } 
} 

出力

ABC 

お知らせ標準出力ストリーム。

私はJavaのFileDescriptorが不透明であることを知っています。それをLinuxのファイル記述子の概念と比較しないでください。 JAVAでどのように作成されているか知りたいだけです。そして1つの静的変数が読み込みと書き込みの両方を行うことができれば、3つの必要(イン、アウト、エラー)は何ですか?

+0

あなたの両方のコードスニペットを試してみましたが、どちらの場合でもjava.io.IOExceptionがあります:アクセスが拒否されました。あなたの質問は何ですか? – Armine

+0

また、System.in、System.out、およびSystem.errの詳細については、http://tutorials.jenkov.com/java-io/system-in-out-error.htmlを参照してください。 – Armine

+0

Windowsでエラーが表示されていますが、Linuxで実行されています。 –

答えて

3

リダイレクトなしでシェルからテストを実行している場合、ファイル記述子0,1,2はおそらく同じファイルである:/ dev/ttyなど(端末)。

これは、それらの記述子のいずれかから読み書きできる理由を説明します。

関連する問題