0

// この問題では待ち行列理論を実装する必要があります。ランダムな値を生成し、その値をファイルに保存する必要があります。異なる範囲の値を保存するために複数のファイルを作成しようとしています。しかし、私はできません。VariableInputRate.txt である最初のファイルのみを作成でき、他のファイルは作成できません。他のファイルも作成するのに助けが必要です。そうでなければ、2 つの値を結合して 1 つのファイルに保存する方法はありません。ありがとう

    String path1 = "c://VariableInputRate(0-10).txt";  

     for(int i = 0 ; i < x/10 ; i ++ )
     {     
         y= Math.random();                  //call a random number generator  
                                            //to generate a random number between 0 and 1
         if(y <= in_rate1 /(in_rate1 + out_rate))
         {
             if(pkt_in_q < n)
                 pkt_in_q ++;
             else
                 pkt_dropped ++;
         }
         else
         {
             if(pkt_in_q > 0)
                  pkt_in_q --;
         }    



         File file = new File(path1);
         try {
             file.createNewFile();
             } 
         catch (IOException e){
             e.printStackTrace();
             }

         try {
             FileWriter fw = new FileWriter(file, true);
             BufferedWriter bw = new BufferedWriter(fw);
             bw.write("For Event " + (i+1) +" we get: ");
             //bw.newLine();
             bw.write("No. of packets in the queue = " + pkt_in_q +" and no. of packets dropped = "+ pkt_dropped);
             bw.newLine();
             bw.flush();
             bw.close();
             fw.close();
             } 
         catch (IOException e) 
         {
             e.printStackTrace();
             }
         }
     System.out.println("Please take at output 1.");

     String path2 = "c://VariableInputRate(10-70).txt";  //Depends on which disk you are using, e.g. C,D,E,F disk

     for( int i=j/10 ; i < k/70 ; i ++ )
     {     
         y= Math.random();                  //call a random number generator  
                                            //to generate a random number between 0 and 1
         if(y <= in_rate2 /(in_rate2 + out_rate))
         {
             if(pkt_in_q < n)
                 pkt_in_q ++;
             else
                 pkt_dropped ++;
         }
         else
         {
             if(pkt_in_q > 0)
                  pkt_in_q --;
         }    



         File file = new File(path2);
         try {
             file.createNewFile();
             } 
         catch (IOException e){
             e.printStackTrace();
             }

         try {
             FileWriter fw2 = new FileWriter(file, true);
             BufferedWriter bw2 = new BufferedWriter(fw2);
             bw2.write("For Event " + (i+1) +" we get: ");
             //bw.newLine();
             bw2.write("No. of packets in the queue = " + pkt_in_q +" and no. of packets dropped = "+ pkt_dropped);
             bw2.newLine();
             bw2.flush();
             bw2.close();
             fw2.close();
             } 
         catch (IOException e) 
         {
             e.printStackTrace();
             }
         }
     System.out.println("Please take at output 2.");
4

2 に答える 2

0

それらを 1 つのファイルに結合するのは簡単です。

// Warning: code written of my mind, might not compile!
// imports left out

public class Whatever {

    public static void main(String[] args) throws IOException {
        String path=System.getProperty("user.home")+"\\output.txt");
        File f=new File(path);
        OutputStream os=new BufferedOutputStream(new FileOutputStream(f));

        // perform whatever computations you want here
        String s1=Foo.bar();
        os.write(s1.getBytes("UTF-8");

        String s2=Bar.foo();
        os.write(s2.getBytes("UTF-8");

        os.close();
    }

}
于 2013-09-23T19:01:46.750 に答える