控制台:
G:\Study---Sky\Program_Platform\test>java Property < PropsDemo.txt
-- listing properties --
Nippon=Kogaku=Japan
Adobe=Mountain View, CA
Ericsson=Sweden
Acorn=favorite=United Kingdom
Sony=Japan
G:\Study---Sky\Program_Platform\test>
在學習Java Cookbook時,有這樣一個例子,原碼如下,其在命令行讀入的方式如上
所示.一般情況下 “java Property < PropDemo.txt”中 “< PropDemo.txt”應該是命令行參數,可是在本例中好像並不是這樣,其原理到底為何我百思不得其解,煩請高手賜教,到底 props.load(System.in);是怎樣執行的?< PropsDemo.txt是命令行參數嗎?
import java.util.Properties;/**
 * Demonstrate Properties reading/writing.
 * @see HashTableDemo, for the older Hashtable.
 */
public class Property{ public static void main(String[] argv) throws java.io.IOException { Properties props = new Properties(); // Get my data
props.put("Adobe", "Mountain View, CA");
// Now load additional properties
props.load(System.in); // List merged properties, using System.out
props.list(System.out);
}
}

解决方案 »

  1.   

    Properties类是继承HashTable的。
    Java API的解释如下:
    The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
    我的理解:
    在本例中“<PropsDemo.txt”的确是命令行参数。你如果改成java Property <PropsDemo.txt >Text.txt 这个意思是把PropsDemo.txt中的文件内容读出来然后在写入 Text.txt.
    如果是这样java Property >Text.txt然后系统等待你输入。最后在你输入的内容保存在Text.txt文件中。 
      

  2.   

    非常感謝這麼及時的回覆,可是我還是有兩個疑問:
    1.java Property < PropsDemo.txt中如果 "< PropsDemo.txt" 是
    命令行參數的話,那麼具體是由那個類來處理這兩個參數的,好像程式中並沒有把這兩個參數傳給 類java.util.Properties,參數
    只有System.in呀,而且文件打開後是應該關閉的,那關閉的動作又是谁來執行呢
    2.如果執行java  Property    >Text.txt
    那麼應該在控制台中輸入那個符號來終止輸入呢
    我現在只能用關閉控制台的方法來終止輸入,當然這樣的話,雖然Text.txt生成了,可是裡面的內容為空
      

  3.   

    呵呵,讨论讨论,共同进步。
    按我的理解是:System.in来做为输入流,对参数进行处理的。System.out作为输出流。
    如果执行java  Property    >Text.txt 你可以在程序中进行判断,还可以按ctrl+c来终止输入。在我这里的执行结果是正确的,既Text.txt文件中有内容。
    加我的MSN:[email protected]
    或者给我发邮件讨论:[email protected]
      

  4.   

    不好意思,我是在公司上的網,我們這裡不能上MSN和郵箱的,受你的啟發我又簡單寫了一個程式,用與測試我的想法。
    import java.io.*;
    import java.io.BufferedReader;public class hello {
      public hello() {
      }
      public static void main(String[] args) throws Exception{
        hello hello1 = new hello();
        System.out.println("hello");
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while((str=buf.readLine())!=null){
          System.out.println(str);
        }
      }
    }執行以上程序,會發現其與上一個程序的原理一樣,所以現在的問題只有一個 即命令行中輸入"
    <" 或">"後,java.exe 是如何處理的.真的好想知道這個問題的答案。
      

  5.   

    java hello <test.txt //这句话首先执行java,由java调用jvm,然后jvm装载hello类,最后是输入参数,就是把test.txt文件作为输入流,然后读出文件内容并且输出。
    java hello >test.txt //这句话首先执行java,由java调用jvm,然后jvm装载hello类,最后是输出参数,这test.txt文件是输出对象,就是把由System.in获得的输入流写入test.txt中。
      

  6.   

    感覺上確實是這樣的,可是Java的那些文檔記載了這樣的操作呢!