0

// 我必须在这个问题中实现排队论。我必须生成一个随机值并将这些值存储在一个文件中。我正在尝试创建多个文件来存储不同范围的值。但我做不到。我只能创建第一个文件是 VariableInputRate.txt 而不是其他文件。我也需要帮助来创建其他文件。否则有什么方法可以组合这两个值并将其保存在一个文件中。谢谢

    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

将它们组合在一个文件中应该很容易:

// 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 回答
0

首先,您可以尝试将/斜杠更改为\or\\吗?其次,为什么您总是在循环中创建新文件?因为首先只有一条路径。在循环中多次创建File file = new File(path1);

我认为最好声明 new File a 在循环外创建它。你怎么看?

于 2013-09-23T18:08:57.017 回答