简体   繁体   中英

Java Code failing to write to text file,

im trying to run a simulator and there are several problems, primarily.... -the code isn't printing out the values at the end of the program - the code does not actually create the file
-I'm pretty tired so forgive any foolish mistakes I made or details I have left out.

I've searched the website and I found this

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

and

how to write to text file outside of netbeans

I thought i could edit code from the first link to work for me, but that did not work( whcih is what you see here)

the second page looks more simple but there's no surrounding code so im not sure what the context is and how I would implement it

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

    }

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

}

Corrected the code check this and change the file directory and folder directory according to your requirement.

Correction for details:

First you can't define a try block like below

try (BufferedWriter bw = new BufferedWriter(fw)) 

It should be defined like

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

Modified code for that is like below:

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

Second you have to create the directory first post that you can create the file inside the directory. so just modified the code to get the directory created and file created.

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

Hope it clarifies your doubt.

I think Code not executing due to file path error.

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

There is no folder called "Temp" in the C: drive. If u create Temp folder manually then the code will execute successfully.

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

I have created "Temp" folder in D: drive and code executed successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM