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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:24:49+00:00 2026-06-01T08:24:49+00:00

The following code does not always create the file. As far as I noticed,

  • 0

The following code does not always create the file.
As far as I noticed, the first time this code has run, though no exception is thrown and createdFileSucceeded == true, the file doesn’t get created.

I run the code on Windows, java 6.

Any input could be helpful

File file = new File(tmpDir, fileName);

try {
if (tmpDir == null) {
  String environmentHomePath // = somePath;
  tmpDir = new File(environmentHomePath, "SampleDumps");

  if (! tmpDir.exists() || ! tmpDir.isDirectory()) {
    boolean mkdirSucceeded = tmpDir.mkdir();
    if (! mkdirSucceeded) {
      throw new IOException("Failed creating directory: '" + tmpDir.getAbsolutePath() + "'");
    }
  }
}

if (file.exists()) {
  boolean deleteFileSucceeded = file.delete(); 
  if (! deleteFileSucceeded) {
    throw new IOException("Unable to delete pre existing sample file: '" + fileName + "'");
  }
}

boolean createFileSucceeded = file.createNewFile();
if (! createFileSucceeded) {
  throw new IOException("Unable to create sample file: '" + fileName + "'");
}

fw = new FileWriter(file);
bw = new BufferedWriter(fw);

StringBuilder sb = new StringBuilder("something...");

bw.write(sb.toString());
bw.flush();
}
catch (IOException ioe) {
log.warn("Unable to file invalid sample to file: '" + fileName + "'", ioe);
}
finally {
if (bw != null) {
  try {
    bw.close();
  } catch (IOException e) {
    log.warn("Unable to close Writer to file: '" + fileName + "'", e);
  }
}
else if (fw != null) {
  try {
    fw.close();
  } catch (IOException e) {
    log.warn("Unable to close Writer to file: '" + fileName + "'", 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-06-01T08:24:50+00:00Added an answer on June 1, 2026 at 8:24 am

    If file.createNewFile() returns true then a file was created.

    The most likely explanation that the file is being created, but not in the place where you are expecting. I expect that you are using a relative pathname for the file …


    Looking more carefully at your code and your comment, I think that is exactly what is happening. Take a look at the way that you create the temporary directory. You first construct the file using tmpdir as the parent directory. Then you test to see is tmpdir is null and create a directory. But you then proceed to use the File object which STILL has a null parent directory!!

    You need to create the File object AFTER checking tmpdir and creating it if required.

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

Sidebar

Related Questions

DataAnnotations does not work with buddy class. The following code always validate true. Why
The following code does not run as rootNode is null when retrieved by name
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
The following code does not want to compile. See the included error message. Code:
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not give a warning with g++ 4.1.1 and -Wall .
The following code does not submit the input values: $('.button').live('click', function() { var date
The following code does not compile: //int a = ... int? b = (int?)

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.