2011-07-01 23 views
0

これは私が最終的にやったことです。それは素晴らしいですが、おそらく微調整を使用することができます。プロパティファイルの作成に関する問題

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
FileInputStream inStream = null; 
FileOutputStream outStream = null; 
Properties config = new Properties(); 

     try{ 
      if (file.exists()){//Checks if it exists. 
      inStream = new FileInputStream(file); 
       if (inStream.available() >= 0){//Cheacks if it has anything in it. 
        config.load(inStream); 
        System.out.println(config); 
       } 
      } 

      config.setProperty(property , score);//New property 
      outStream = new FileOutputStream(file); 
      config.store(outStream, "Property");//Names the Properties that are in the file Property 
      config.list(System.out);//Prints out all the properties. 

     } catch (IOException ioe){//Handles any problems 
      System.out.println("You just pooped the pants"); 
     } finally{//Closes both input and output Streams if they are open 
      try { 
       if(inStream != null) 
        inStream.close(); 
       if (outStream != null) 
        outStream.close(); 
      } catch (IOException e) { 
      } 
     } 
} 

私は2組のコードを持っています。ファイルにプロパティを書き込むものと、もう少し深いものを書き込むもの。私は彼らが存在していない場合は両方を満たす必要がありますが、それらのうちの1つだけが行う必要がありますと思う。ここに各コードにいくつかの補足があります。私はちょうどコンソールを使用しているだけで、私はちょうどそんなに派手ではないので、まったく混乱しています。

private void properties() { 
    System.out.println("What would you like to name the .properties file?"); 
    sTest.stringReader(); 
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What property would you like to change?"); 
    sTest.stringReader(); 
    String property= sTest.getString(); 
    System.out.println("What would you like to change the " + property + " to?"); 
    sTest.stringReader(); 
    String score = sTest.getString(); 

     try { 
      File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
      FileInputStream inStream = new FileInputStream(file); 
      Properties config = new Properties(); 
      config.load(inStream); 
      // Create a new property 
      config.setProperty(property , score); 
      FileOutputStream outStream = new FileOutputStream(file); 
      config.store(outStream, "Property"); 
      inStream.close(); 
      outStream.close(); 
      config.list(System.out); 
     } catch (IOException ioe){ 
      System.out.println("Chould not write file."); 
     } 
} 

ここには何も追加せずにプロパティを書き込むだけのものがあります。このメソッドは、ファイルを作成するものですが、私は "File file = new File"は両方のためにそれを行うべきだと感じます。しかし、プログラミングでは、再び気持ちがあまり説明されません。私は説明が大好きです。ありがとう。あなたの助けを借りて、コードの

private void myWrite() { 
    System.out.println("What would you like to name your file?"); 
    sTest.stringReader(); 
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What would you like to put in you file?"); 
    sTest.stringReader(); 
    String letsWrite = sTest.getString(); 
     try { 
      File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
      FileOutputStream fileStream = new FileOutputStream(file); 
      write(fileStream, letsWrite); 
     } catch (IOException ioe){ 
      System.out.println("Could not write file" + ioe); 
     } 
    } 

やり直し:

System.out.println("What would you like to change the " + property + " to?"); 
    sTest.stringReader(); 
    String score = sTest.getString(); 

    try { 
     File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
     FileInputStream inStream = new FileInputStream(file); 
     Properties config = new Properties(); 
     config.load(inStream); 
     inStream.close(); 
    } catch (IOException ioe){ 
     System.out.println("Chould not read file."); 
    } 

    try{ 
     File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
     FileOutputStream outStream = new FileOutputStream(file); 
     Properties config = new Properties(); 
     config.setProperty(property , score); 
     config.store(outStream, "Property"); 
     outStream.close(); 
    } catch (IOException ioe){ 
     System.out.println("Chould not write file."); 
    } 

これは、IOExceptionがスローされますが、それは、ファイルを書きません。どのように私は最終的にブロックでそれを閉じますか?私はまだそれらを使用していない。どのようにファイルをロードするだけですか?私はまだそれに取り組んでいる私はあなたがまだいている間にあなたにこれを手に入れたいと思っていました。ご協力いただきありがとうございます。

私はまだそれほどうまくいきません。私はすべてが何をしていると思っているのかを書き留めました。私は今夜​​後で私の友人に尋ねるつもりですが、彼はMatLabの男の子です。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");//new instance of these file 
    FileInputStream inStream = null; //Creates variable out side my try block 
    FileOutputStream outStream = null;//Creates variable out side my try block 
    Properties config = new Properties();//Creates variable out side my try block 
    try { 
     inStream = new FileInputStream(file); //Loads the file to the Input Stream 
     config.load(inStream); //Loads the config with what was in the inStream. 

    } catch (IOException ioe){//Handles any problems 
     System.out.println("Chould not read file."); 
    } 

    try{ 
     //inStream = new FileInputStream(file);//Do I need to do this again? 
     //config.load(inStream);// Do I need to do this again? 

     //Creates a new property 
     config.setProperty(property , score); 

     outStream = new FileOutputStream(file); // Preps the outPut stream to write to the file 
     config.store(outStream, "Property");//Names the Properties that are in the file Property 
     config.list(System.out);//Prints out all the properties. 
    } catch (IOException ioe){//Handles any problems 
     System.out.println("Chould not write file."); 
    } finally{//Closes both input and output Streams 
     try { 
      inStream.close();//It says these need to be in a try/catch block also. 
     } catch (IOException e) { 
     } 
     try { 
      outStream.close(); 
     } catch (IOException e) { 
     } 
    } 

答えて

2

Fileオブジェクトはファイルパスを表します。ファイルインスタンスを作成しても、ファイルシステム上にファイルは作成されません。 FileOutputStreamを開き、ファイルシステムにファイルを作成する操作です。

最初のコードスニペットは、同時にファイルの読み取りと書き込みを試みます。ファイルから読み込み、入力ストリームを閉じた後、出力ストリームを開き、出力ストリームを書き込み、閉じなければなりません。ファイルが存在しない場合は、それを読み取るとIOExceptionがスローされるため、この可能性を処理する必要があります。また、すべての入出力ストリームはfinallyブロック内で常に閉じる必要があります。

+0

をスローしない場合にのみ、上記のコードは読みます。私はそれをロードしようとしますが、私はそれを間違っています。私は質問の最後に新しいコードを作成しました。それが仕事になるためには何を追加する必要がありますか?プロパティを書くだけでなくプロパティに追加する。 – Funlamb

+0

更新されたコードには2つの問題があります。読み込みフェーズで読み込まれたものを再利用するのではなく、常に新しいPropertiesオブジェクトを使用してファイルに書き込みます。これがファイルを常に上書きする理由です。そして、それはfinallyブロックのストリームを閉じません。ストリームのオープン/クローズの例は、http://download.oracle.com/javase/tutorial/essential/io/bytestreams.htmlを参照してください。 –

+0

私はこれを手に入れません。私は自分のコードを破壊して、私が何をする必要があるのか​​を知ることができました。今は認識できません。私はもう少し研究をします。 – Funlamb

1

config.load()がファイルから読み込むため、コードでIOExceptionがスローされます。しかし、例外を処理しているので、コードはその時点で失敗せず、ファイルに書き出し続けます。最初にファイルに書き込んでから読み込むと、例外は発生しません。ファイルにデータがある場合は、ファイルを最初にロードできます。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
    FileInputStream inStream = new FileInputStream(file); 
    if(inStream.available() > 0){ 
     Properties config = new Properties(); 
     config.load(inStream); 
    } 
    inStream.close(); 

そこにデータをストリームにあるので、それはそれが機能するようになりましたが、それは唯一のオーバーファイルを書き込み例外

+0

これは、コードが失敗する理由ではありません。理由は、最初のPropertiesインスタンスでファイルをロードしてから、書き込む前にPropertiesインスタンスを新しいインスタンスに置き換えるためです。 –

+0

なぜ例外をスローする必要がありますか?危険ですが、入力と出力のストリームを同じファイルに同時に開くことができます。あなたの事件に関する書類を指摘できますか? Googleで失敗した – Jiraiya

関連する問題