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 8818933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:13:42+00:00 2026-06-14T05:13:42+00:00

Here is my issue: I need to write the Interator to the file random.txt

  • 0

Here is my issue: I need to write the Interator to the file “random.txt” I am able to see all the data. The data is Store where suppose to, I can see it with the System.out.print but my file is in blank. I am able to create the file but not to writer to it.

I am reading a file from my comp. store in the Treemap and trying to write to the text file. ( I am able to doit with Array with not problem) But this Map with Iterator is bouncing my head.

If some one can help me a litter I will appreciated.

I need to use the TreeMap and the Iterator.

public static void main(String[] args) throws FileNotFoundException, IOException {
    Map<String, String> Store = new TreeMap<>();

    Scanner text=new Scanner(System.in);

    System.out.println("enter file:  ");


    //C:\Users\Alex\Desktop\Fruits\fruits.txt           

    String rap=text.next();

    File into = new File(rap);

    try (Scanner in = new Scanner(into)) {                

        while (in.hasNext()){
            String name=in.next();
            String fruta = in.next();
            Integer num= Integer.parseInt(in.next());
            System.out.println(fruta+"\t"+name+"\t"+num);

            if (Store.containsKey(fruta)){
                Store.put(name,Store.get(name)+fruta );
            }
            else{
                Store.put(name,fruta);
            }
        }

        in.close();

    }                                                                

    System.out.println();

    // insert data to store MAP 
    Set top=Store.entrySet();
    Iterator it = top.iterator();  

    // debugging 

    System.out.println(top);

    // Creating file???????
    FileWriter fstream = new FileWriter("random.txt");  

    //identify File to be write?????? 
    BufferedWriter out = new BufferedWriter(fstream);

    //iterator Loop       
    while(it.hasNext()) {

        Map.Entry m = (Map.Entry)it.next();
        String key = (String)m.getKey();
        String value = (String)m.getValue();

        //writing to file?????
        out.write("\t "+key+"\t "+value);

        // debugging 
        System.out.println(key +"\t"+ value);
    }       

    System.out.println("File created successfully.");
}
  • 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-14T05:13:43+00:00Added an answer on June 14, 2026 at 5:13 am

    After your while loop (the one writing to file) is finished, do:

    out.flush();
    out.close();
    

    Short explanation: BufferedWriter buffers in memory what you write. It doesn’t immediately write to file when you call the write method. Once the while loop is finished and the data to write to file is prepared in buffer memory, you should call flush to do the actual writing to hard disk. And finally you should always close the BufferedWriter that will no longer be used, to avoid memory leaks.

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

Sidebar

Related Questions

Here's my issue - I need to dynamically download several scripts using jQuery.getScript() and
So here's my issue. I have an image that I need to shrink. The
I need help with LINQ syntax or methodology, not sure which. Here's my issue:
Having an issue here that I have tried everything I can think of but
I need your suggestions in solving this issue. Here is the requirement. We have
Here's my issue: I need to close a process, already running, from a C#
My issue here is quite simple : i'm trying to use the jQuery validate
I have a really weird issue here. I'm using my local development server right
Im having a weird issue here. Its probably just something stupid but I dont
I am facing a issue here in reading the values set by a stored

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.