I am attempting to make an interface with textfields where the data entered into the fields is stored in a variable, and then written to an external file. I am trying to use FileWriter fw = new FileWriter() to tranfer the variable to the file, but I cannot find an option available to send a variable, the only options when using fw.write() are strings and ints....
This is the block of code I am talking about:

 try {
            try (FileWriter fw = new FileWriter(f)) {
                fw.write(""); // I need to put a variable in here??

            }
       } catch (Exception ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

Can someone please suggest how I can do this??

What is the type of your variable? Does the output file have to be human-readable?

It is impossible to literally write a variable to a file because files can only contain values, not variables. Normally when someone says "write a variable to a file" that person would mean to write the value of the variable to a file, but in that case the person wouldn't be even slowed by being limited to Strings and ints, since that is exactly what the value of many variables is, and other values can be converted.

If you want to store the value of your variable without converting it to a String then you shouldn't be using a Writer. You should be using a java.io.DataOutputStream. Just look at the javadoc for DataOutputStream and be amazed by the wealth of tools for doing anything you might be trying to do, but then you won't be able to open your file in a text editor to verify it contains the correct result.

On the other hand, if you are using a Writer because you want to get a text file as the result, then just use the Object.toString() method to convert whatever object you have to a String. If you have a number instead of an object then you can use Integer, Float, Double, or one of the other Number classes depending on the type of your number.

Try this class. It will create your file if it does not exist and it will print your variable value to the file. You will need to change the variable type to what you need in the argument. Also your logger is wrong. Level Severe is only for release. Until you are ready to release the code to production you need to change the level to WARNING or better yet FINE

Usage is :

MyClass myclass = new MyClass();
myclass.printvariableToFile(String,String);

    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
     *
     * @author Bill
     */
    public class MyClass {


        public void printvariableToFile (String fileName, String myVariable) {


      File myFile;
      myFile=new File(fileName);
      if(!myFile.exists()){

          try {
      myFile.createNewFile();

                FileWriter fw = new FileWriter(fileName);
                PrintWriter pw = new PrintWriter(fw);


                // Write variable to file
               pw.print("Writing variable to file");
               pw.println("The varible is below: ");
               pw.println(myVariable);

               // Close 
               pw.close();


            } catch (Exception ex){

                 Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);

             }
          }    
       }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.