2016-05-06 6 views
-2

私はシミュレータを実行しようとしていますが、主にいくつかの問題があります。 - コードはプログラムの最後に値を表示しません - コード実際にファイルを作成していません
- 私はかなり疲れていますので、私が作った愚かな間違いや私が残した細部を許してください。Javaコードがテキストファイルへの書き込みに失敗しました

私はウェブサイトを探索していると私は私が私が私のために動作するように最初のリンクからコードを編集することができると思ったが、それこの

What is the simplest way to write a text file in java

how to write to text file outside of netbeans

を見つけましたうまくいきませんでした(ここで見ているものはどれですか)

2番目のページはもっとシンプルに見えますが無い周囲のコードはそうイムない状況が何であるかを確認してくださいと私は

import java.util.Random; 
import java.util.Scanner; 

import java.io.*; 
/* 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
*/ 



public class SimClass { 

    public static void main (String[] args) 
    { 

     Scanner keyboard = new Scanner(System.in) ; 
     Random randomNumbers = new Random(); 
     //create object from random class 

     //create self explanaroty input parameters 
     int pkt_in_q = 0 ; 
     int pkt_dropped = 0;    
     int input ; 
     int output ; 
     int buffer ; 
     int x ; 

     double y ; 
     //ask for values for buffer size. input rate, output rate 

     y = randomNumbers.nextDouble(); 
     //attempt to assign a random number to the variable and 

     /*here you should get user input. 
     buffer size, # of repitions , if 
     */ 



     //fix this 
     System.out.print("Enter an integer for the input rate "); 
      input = keyboard.nextInt(); 


     System.out.print("Enter an integer for the output rate "); 
      output = keyboard.nextInt(); 

     System.out.print("What is the buffer size "); 
      buffer = keyboard.nextInt(); 




     for (x = 1000000; x >=0 ; x--) 
     { /* 
      simulate # of packets dropped/in the que, 
      create file, write results to file, output results, 
      */   

      if (y > input/(output/buffer)) 
       { 
       if (pkt_in_q < buffer) 
        {  
         pkt_in_q++ ; 
        } 
       else 
       { 
        pkt_dropped++ ; 
       } 
       //if statement should terminate here 
       } 
      else 
      if (pkt_in_q > 0) 
      { 
       pkt_in_q -- ;  
      } 


     } 


     /* 
      create file, write results to file, output results, 
      */ 

     try { /*this seeems to be the problem, the program is either not doing 
       anything with this or not making the results visible 
         */ 

      String content =(" pkt_in_q is " + pkt_in_q + 
        "pkt_dropped is " + pkt_dropped); 


       File file = new File("C:/Temp/inputFile.txt"); 

        // if file doesnt exists, then create it 
        if (!file.exists()) 
        { 
        file.createNewFile(); 
        } 

        FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
        try (BufferedWriter bw = new BufferedWriter(fw)) 
        { 
        bw.write(content); 
        bw.close(); 
        } 

        System.out.println("packets dropped value is = " + 
          pkt_dropped + "packets in q value is = " + pkt_in_q); 

      } 
     catch (IOException e) 
     { 
     //e.printStackTrace(); 
     } 

    } 

} 
+3

コードを_debugging_しようとしましたか?これは、Stack Overflow IMHOへの投稿よりも役に立ちます。 –

+1

なぜあなたは例外を抑制していますか?彼らは間違っていることをあなたに示します。 – tak3shi

+0

ティム私はnetbeansを使用していますが、コードはエラーを投げていません。また、 "実行"ボタンの横にある "デバッグ"オプションはグレー表示されていますが、suggegstingはエラーがなく、エラーは論理的または組織的です。 武生、あなたが何を指しているのかよくわかりません。詳しく教えてください。私は、コードについて約80%の理解があると言います。上記。私は他の場所からコードをcoppiedと言ったように。 – Anonymous

答えて

0
public static void main(String[] args) { 

    Scanner keyboard = new Scanner(System.in); 
    Random randomNumbers = new Random(); 
    //create object from random class 

    //create self explanaroty input parameters 
    int pkt_in_q = 0; 
    int pkt_dropped = 0; 
    int input; 
    int output; 
    int buffer; 
    int x; 

    double y; 
    //ask for values for buffer size. input rate, output rate 

    y = randomNumbers.nextDouble() * 10; 
    System.out.println("Y++++++" + y); 
    //attempt to assign a random number to the variable and 

    /*here you should get user input. 
    buffer size, # of repitions , if 
    */ 

    //fix this 
    System.out.print("Enter an integer for the input rate "); 
    input = keyboard.nextInt(); 

    System.out.print("Enter an integer for the output rate "); 
    output = keyboard.nextInt(); 

    System.out.print("What is the buffer size "); 
    buffer = keyboard.nextInt(); 

    for (x = 1000000; x >= 0; x--) { /* 
        simulate # of packets dropped/in the que, 
        create file, write results to file, output results, 
    */ 

     if (y > input/(output/buffer)) { 
      if (pkt_in_q < buffer) { 
       pkt_in_q++; 
      } else { 
       pkt_dropped++; 
      } 
      //if statement should terminate here 
     } else if (pkt_in_q > 0) { 
      pkt_in_q--; 
     } 

    } 

    /* 
     create file, write results to file, output results, 
    */ 

    try { /*this seeems to be the problem, the program is either not doing 
        anything with this or not making the results visible 
    */ 

     String content = (" pkt_in_q is " + pkt_in_q + "pkt_dropped is " + pkt_dropped); 

     String folderPath = "D:" + File.separator + "Temp"; 
     String fileName = folderPath + File.separator + "inputFile.txt"; 
     File folder = new File(folderPath); 
     File file = new File(fileName); 
     folder.mkdir(); 
     // if file doesnt exists, then create it 
     if (!file.exists()) { 
      //file.mkdir(); 
      file.createNewFile(); 
      System.out.println("File created"); 

     } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     try { 
      bw.write(content); 
      bw.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("packets dropped value is = " + pkt_dropped 
       + "packets in q value is = " + pkt_in_q); 

    } catch (IOException e) { 
     //e.printStackTrace(); 
    } 

} 

それを実装する方法を、コードがこれをチェックし、あなたの条件に応じて、ファイルのディレクトリとフォルダのディレクトリを変更修正しました。詳細については、

訂正:

まずそれは、そのための

try{ 
} 
catch(IOException e) 
{ 
    //do something 
} 

修正コードのように定義されるべき

try (BufferedWriter bw = new BufferedWriter(fw)) 

以下のようにあなたがtryブロックを定義することはできませんが、以下のようなものです:

FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
         BufferedWriter bw = new BufferedWriter(fw); 
         try 
         { 
         bw.write(content); 
         bw.close(); 
         } 
         catch (IOException e) 
         { 
         //e.printStackTrace(); 
         } 

2番目に、ディレクトリ内にファイルを作成できるディレクトリを最初に作成する必要があります。ディレクトリを作成してファイルを作成するコードを変更しました。

String folderPath = "D:" + File.separator + "Temp"; 
     String fileName = folderPath + File.separator + "inputFile.txt"; 
     File folder = new File(folderPath); 
     File file = new File(fileName); 
     folder.mkdir();   

あなたの疑いが明確になることを願っています。

+0

あなたはあなたの変更を完全に説明してくれますし、私が間違っていたことは簡単にお願いします。私はいくつかの変更が行われていることを確認しています。あなたが何をしたのかを理解するのが好きです。なぜ – Anonymous

+0

"File.separator"はファイルパスを定義するのにスラッシュです。 folder.mkdir()メソッドを使用してフォルダを作成します。 –

+0

これは役に立ちます。コードは実行されますが、ファイルはnetbeans内でプログラムを実行した後にcreateadではありません。希望の結果を得るためにcmdから実行する必要がありますか? 具体的には、私の計算結果を含むファイルを生成することができます。 – Anonymous

0

ファイルパスエラーのためにコードが実行されないと思います。ドライブ:

File file = new File("C:/Temp/inputFile.txt"); 

Cで"温度" と呼ばれるなしフォルダはありません。 Tempフォルダを手動で作成すると、コードは正常に実行されます。

File file = new File("D:/Temp/inputFile.txt"); 

D:ドライブに「Temp」フォルダが作成され、コードが正常に実行されました。

+0

私はCドライブに書き込むときでも、プログラムはまだエラーがあります(それはjavaが書き込むファイルを作成しません) – Anonymous

+0

Cドライブに "Temp"という名前の新しいフォルダを作成しましたか? – Leks

関連する問題