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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:53:29+00:00 2026-05-30T03:53:29+00:00

I am trying to write some content into file inside my spring controller. Before

  • 0

I am trying to write some content into file inside my spring controller. Before writing i am creating the directory. But the file is not getting written. I am really confused. here is the code

public String storeText(String title, String description) {

    String randomName = null;
    try {

        String baseDir = "C:/MyProjects/eclipse/DreamFolder/";
        randomName = Long.toHexString(Double.doubleToLongBits(Math.random()));
        String folderName = baseDir + randomName;
        String fileName = folderName + "/textCon.txt";

        File fileFolder = new File(folderName);
        fileFolder.mkdir();
        boolean exists = fileFolder.exists();
      if (!exists) {
        System.out.println("storeText folder does not exist");
      }
        System.out.println("storeText folderName - " + folderName);
        System.out.println("storeText fileName - " + fileName);

        File file = new File(fileName);
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(title);
        out.newLine();
        out.write(randomName);
        out.newLine();
        out.write(description);
        out.close();
    } catch (Exception ex) {
    }
    return randomName;
}

The last portion where i have this File file = new File(fileName); is what is having issues.

  • 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-30T03:53:30+00:00Added an answer on May 30, 2026 at 3:53 am

    Start by not swallowing the exception and also flushing the buffered writer, here’s the code you should be using:

    public String storeText(String title, String description) {
    
        String randomName = null;
        try {
    
            String baseDir = "C:/MyProjects/eclipse/DreamFolder/";
            randomName = Long.toHexString(Double.doubleToLongBits(Math.random()));
            String folderName = baseDir + randomName;
            String fileName = folderName + "/textCon.txt";
    
            File fileFolder = new File(folderName);
            fileFolder.mkdir();
            boolean exists = fileFolder.exists();
          if (!exists) {
            System.out.println("storeText folder does not exist");
          }
            System.out.println("storeText folderName - " + folderName);
            System.out.println("storeText fileName - " + fileName);
    
            File file = new File(fileName);
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(title);
            out.newLine();
            out.write(randomName);
            out.newLine();
            out.write(description);
            out.flush();
            out.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return randomName;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some content into an XML file, yet I do
I'm trying to write some PHP to upload a file to a folder on
I'm trying to write some code to work with an htdigest password file. The
I am trying to write some code that will open a file, read its
I am trying to write a .bat file to automate some shell commands. Most
I'm trying to write some tests around some code previously written before I start
I am trying to insert some data into the middle of a file. I
I'm trying to write some C# code to get to a specific folder in
I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some C# code that calls a method from an unmanaged

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.