2017-04-09 74 views
-3

我不知道爲什麼它採取負面投票。我不想把我的代碼給大家使用。和即時通訊不要求代碼即時通訊要求邏輯,所以我可以做到這一點從文件中讀取條件java

我寫這個Java代碼,我想條件從文件中讀取。 有可能嗎?如果你有任何教程。或任何能夠幫助我理解如何去做的事情。 我只是想了解更多的邏輯。

我已經嘗試把每個條件放在一個文件中,並閱讀表格。

if(con == 1){ 
     Scanner myScanner = new Scanner(new File("con.txt")); 
    } 
    else if(con == 2){ 
     Scanner myScanner = new Scanner(new File("con2.txt")); 
    } 
    else if(con == 3){ 
     Scanner myScanner = new Scanner(new File("con3.txt")); 
    } 
    else{ 
     System.out.println("You did not choose one of the 3 con"); 
    } 

但它沒有工作,因爲在每個if語句中有2種情況需要被滿足。

我希望這是有道理的

+1

歡迎來到Stack Overflow!請[參觀](http://stackoverflow.com/tour)瞭解網站的工作原理以及在這裏的主題。另請參閱:[爲什麼「有人可以幫我嗎?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) –

+0

2條件是什麼? –

回答

0

所以我真的不能看到這裏的條件下,只有一個選擇的1,2或3。如果是這樣的話,那麼你最好使用一個開關,然後使用你的別人作爲默認情況。


因此,從屬性文件中讀取以防萬一需要加載屬性。下面是一個例子。

首先創建一個名爲「configuration.properties」的文件,併爲此示例放置以下內容;

  • property1 = myPropertyOne
  • property2 = myPropertyTwo
  • property3 = myPropertyThree

現在下面是讀取屬性文件,將它們放置成Java Properties Object一個例子,然後最終將它們打印出來。

Properties myProperties = new Properties(); 
InputStream fileInput = null; // Here so you can close it in the finally section 

try { 
    fileInput = new FileInputStream("configuration.properties"); 
    myProperties.load(fileInput); // pass the input stream into properties to be read 

    // print out all the properties values for given keys 
    System.out.println(myProperties.getProperty("property1")); 
    System.out.println(myProperties.getProperty("property2")); 
    System.out.println(myProperties.getProperty("property3")); 

} catch (IOException exceptionThrown) { 
     // would be best to handle the exception here 
} finally { 
    if (fileInput != null) { 
     try { 
      fileInput.close(); 
     } catch (IOException e) { 
      // handle exception for attempting to close handler 
     } 
    } 
}