我无法创建多个文件来存储这些值

时间:2013-09-23 00:30:40

标签: java

//我必须在这个问题上实现排队理论。我必须生成一个随机值并将值存储在一个文件中。我正在尝试创建多个文件来存储不同范围的值。但我无法做到。我只能创建第一个文件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.");

2 个答案:

答案 0 :(得分:0)

首先,您可以尝试将/斜杠更改为\\\吗?为什么要在循环中创建新的File?因为首先只有一条路径。在循环中,许多时间创建File file = new File(path1);

我认为更好地声明新文件a在循环外创建它。你觉得怎么样?

答案 1 :(得分: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();
    }

}