Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:10:53+00:00 2026-05-23T13:10:53+00:00

This is what I finally ended up doing. It works great but could probably

  • 0

This is what I finally ended up doing. It works great but could probably use some fine tuning.

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
FileInputStream inStream = null;
FileOutputStream outStream = null;
Properties config = new Properties();

        try{
            if (file.exists()){//Checks if it exists.
            inStream = new FileInputStream(file);
                if (inStream.available() >= 0){//Cheacks if it has anything in it.
                    config.load(inStream);
                    System.out.println(config);
                }
            }

            config.setProperty(property , score);//New property
            outStream = new FileOutputStream(file);
            config.store(outStream, "Property");//Names the Properties that are in the file Property
            config.list(System.out);//Prints out all the properties. 

        } catch (IOException ioe){//Handles any problems
            System.out.println("You just pooped the pants");
        } finally{//Closes both input and output Streams if they are open
            try {
                if(inStream != null)
                    inStream.close();
                if (outStream != null)
                    outStream.close();
            } catch (IOException e) {
            }
        }
}

I have two sets of code. One that just writes a property to a file and one that is a bit more in depth. I think they should both be writing the fill if it doesn’t exist but only one of them does. Here is each code with some extras. I’m just using the console right now and I’m just messing around so its not flashy at all.

private void properties() {
    System.out.println("What would you like to name the .properties file?");
    sTest.stringReader();
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What property would you like to change?");
    sTest.stringReader();   
    String property= sTest.getString();
    System.out.println("What would you like to change the " + property + " to?");
    sTest.stringReader();
    String score = sTest.getString();

        try {
            File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
            FileInputStream inStream = new FileInputStream(file);
            Properties config = new Properties();
            config.load(inStream);
            // Create a new property
            config.setProperty(property , score);
            FileOutputStream outStream = new FileOutputStream(file);
            config.store(outStream, "Property");
            inStream.close();
            outStream.close();
            config.list(System.out);
        } catch (IOException ioe){
            System.out.println("Chould not write file.");
        }
}

And here is the one that just writes a property without adding any thing to it. This method is the one that creates the file but I feel that “File file = new File” should do it for both. But then again feelings don’t account for much when programming. I’d love an explanation. Thanks.

private void myWrite() {
    System.out.println("What would you like to name your file?");
    sTest.stringReader();
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What would you like to put in you file?");
    sTest.stringReader();
    String letsWrite = sTest.getString();
        try {
            File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
            FileOutputStream fileStream = new FileOutputStream(file);
            write(fileStream, letsWrite);
        } catch (IOException ioe){
            System.out.println("Could not write file" + ioe);
        }
    }

Redo on the code with your help:

System.out.println("What would you like to change the " + property + " to?");
    sTest.stringReader();
    String score = sTest.getString();

    try {
        File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
        FileInputStream inStream = new FileInputStream(file);
        Properties config = new Properties();
        config.load(inStream);
        inStream.close();
    } catch (IOException ioe){
        System.out.println("Chould not read file.");
    }

    try{
        File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
        FileOutputStream outStream = new FileOutputStream(file);
        Properties config = new Properties();
        config.setProperty(property , score);
        config.store(outStream, "Property");
        outStream.close();
    } catch (IOException ioe){
        System.out.println("Chould not write file.");
    }

It throws the IOException but it does write the file. How would I close it in a finally block? I haven’t used those yet. How do you just load the file first? I’m still working on it I just wanted to get this to you while you were still on. Thanks for your help.

I’m still not getting this very well. I have put in notes of what I think everything is doing. I’ll be asking my friend later tonight what is going on but he is more of a MatLab guy.

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");//new instance of these file
    FileInputStream inStream = null; //Creates variable out side my try block
    FileOutputStream outStream = null;//Creates variable out side my try block
    Properties config = new Properties();//Creates variable out side my try block
    try {
        inStream = new FileInputStream(file); //Loads the file to the Input Stream
        config.load(inStream); //Loads the config with what was in the inStream.

    } catch (IOException ioe){//Handles any problems
        System.out.println("Chould not read file.");
    }

    try{
        //inStream = new FileInputStream(file);//Do I need to do this again? 
        //config.load(inStream);// Do I need to do this again? 

        //Creates a new property
        config.setProperty(property , score);

        outStream = new FileOutputStream(file); // Preps the outPut stream to write to the file
        config.store(outStream, "Property");//Names the Properties that are in the file Property
        config.list(System.out);//Prints out all the properties. 
    } catch (IOException ioe){//Handles any problems
        System.out.println("Chould not write file.");
    } finally{//Closes both input and output Streams
        try {
            inStream.close();//It says these need to be in a try/catch block also. 
        } catch (IOException e) {
        }
        try {
            outStream.close();
        } catch (IOException e) {
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:10:53+00:00Added an answer on May 23, 2026 at 1:10 pm

    A File object represents a file path. Creating a File instance doesn’t create a file on the file system. Opening an FileOutputStream and writing to it is the operation which creates the file on the file system.

    Your first code snippet tries to read and write from/to a file at the same time. You should read from the file, close the input stream, and then open an output stream, write and close the output stream. Reading from a file if it doesn’t exist will throw an IOException, so you have to handle this possibility. And any input/output stream should always be closed in a finally block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Finally I have ended up with this. But there is a minor issue not
So I finally got my boss to approve the use of perl for this
I`m processing HTML page and finally ended up with lines like this: <td class=border>AAA</td><td
I obviously don't know what I'm doing. I have finally got this PowerShell command
This morning I finally made my mind and decided to ask you for help.
I am finally asking this question as I have never actually found an answer.
Alright, I finally got this code to work after hours of toiling: Dim path
I've searched around quite a bit for ways to do this and I finally
This was working..and I moved the disposal code to the finally block, and now
This is a question I have frequently, and finally want to hear people's opinions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.