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

  • SEARCH
  • Home
  • 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 7719101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:19:47+00:00 2026-06-01T03:19:47+00:00

I’m working on a project for a game I play, and I can’t seem

  • 0

I’m working on a project for a game I play, and I can’t seem to get it to write to a file correctly. I had it working at one point, but changed several things around, and moved the write method into a different class. Somewhere in the process, I must have broken something.

public static void write(item a) {
    //Variable declaration. outp is private instance String array
    outp[0] = "<" + a.getID() + ">\n";
    outp[1] = "<name>" + a.getName() + "</name>";
    outp[2] = "<description>" + a.getDesc() + "</description>\n";
    outp[3] = "<type>" + a.getType() + "</type>\n";
    outp[4] = a.getOtherCode() + "\n";
    outp[5] = "</" + a.getID() + ">\n";
    try{
    //Create/Append data to items.xml located in variable folder.
    FileWriter writeItem = new FileWriter(modTest.modName + File.separator +"items.xml", true); 
    BufferedWriter out = new BufferedWriter(writeItem);

    //Loop through array and write everything
    for(int i = 0; i < outp.length; i++) {
        System.out.println("outp[" + i + "] = " + outp[i]);
        System.out.println("Writing line " + i + " of item "+  a.getID());
        out.write(outp[i]); 
    }

    }catch (Exception e) { System.err.println("Erro: " + e.getMessage()); 
    }
}

//The File.seperator and ,true) I got from other questions here. I thought those might be the issue, but after commenting them out and just writing items.xml straight to my projects folder, it’s still empty.
I tried to add some output for debugging purposes, and I got exactly what I expected. All variable outputs match what they should.

The file is created in the correct folder, but nothing is being written to it.

Any ideas on what I did wrong?

  • 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-06-01T03:19:48+00:00Added an answer on June 1, 2026 at 3:19 am

    You need to close the file descriptor so the contents are flushed to disk.

    Add:

    out.close();
    

    so that your method becomes:

    public static void write(item a) {
      //Variable declaration. outp is private instance String array
      outp[0] = "<" + a.getID() + ">\n";
      outp[1] = "<name>" + a.getName() + "</name>";
      outp[2] = "<description>" + a.getDesc() + "</description>\n";
      outp[3] = "<type>" + a.getType() + "</type>\n";
      outp[4] = a.getOtherCode() + "\n";
      outp[5] = "</" + a.getID() + ">\n";
    
      try {
        //Create/Append data to items.xml located in variable folder.
    
        FileWriter writeItem = new FileWriter(modTest.modName + File.separator +"items.xml", true); 
        BufferedWriter out = new BufferedWriter(writeItem);
    
        //Loop through array and write everything 
    
        for(int i = 0; i < outp.length; i++) {
          System.out.println("outp[" + i + "] = " + outp[i]);
          System.out.println("Writing line " + i + " of item "+  a.getID());
          out.write(outp[i]); 
        }
    
        out.close();
      }
      catch (Exception e) { System.err.println("Erro: " + e.getMessage()); }
    }
    

    Without the call to close(), you’re leaking file descriptors and, after a long enough time, you’d be unable to open any more files for writing).

    Also note that you’re appending to the file each time you’re writing to it (rather than truncating it and starting from scratch each time). Since you’re writing XML to it, you’re unlikely to end up with only one root element.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.