有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java代码无法写入文本文件,

我试图运行一个模拟器,但有几个问题,主要是。。。。 -代码没有在程序结束时打印出值 -代码实际上并没有创建文件
-我很累,所以请原谅我犯的任何愚蠢的错误或遗漏的细节

我搜索了这个网站,找到了这个

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

how to write to text file outside of netbeans

我以为我可以编辑第一个链接中的代码来为我工作,但那不起作用(你在这里看到的是什么)

第二个页面看起来更简单,但是没有周围的代码,所以我不确定上下文是什么,以及如何实现它

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();
        }   

    }

}

共 (2) 个答案

  1. # 1 楼答案

    我认为由于文件路径错误,代码无法执行

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

    C:驱动器中没有名为“Temp”的文件夹。如果您手动创建临时文件夹,那么代码将成功执行

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

    我已在D:驱动器中创建了“Temp”文件夹,代码已成功执行

  2. # 2 楼答案

    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块

    try (BufferedWriter bw = new BufferedWriter(fw)) 
    

    应该这样定义

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

    修改后的代码如下所示:

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

    其次,您必须先创建目录,然后才能在目录中创建文件。因此,只需修改代码即可创建目录和文件

    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();         
    

    希望它能澄清你的疑问