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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:17:55+00:00 2026-06-15T18:17:55+00:00

This is probably a very basic question but I tried various options and it

  • 0

This is probably a very basic question but I tried various options and it didn’t work out and hence requesting help. I want to create a file inside a specified directory. If the file already exists, I want to append data to it. Below is what I tried:

    var DirectoryName: String = "LogFiles"
    var dir: File = new File(DirectoryName);  
    dir.mkdir();

    var ActorID: String = "1"  
    var FileName: String = DirectoryName + File.separator + "Actor_" + ActorID + ".log"
    val FileObj: File = new File(FileName)

//   FileObj.getParentFile().mkdirs();
//   FileObj.createNewFile();

    var FileWriterObj: FileWriter = null
    var FileExistsFlag = 0

    if (!FileObj.exists())
    {    
     FileWriterObj = new FileWriter(FileObj.getName())
    }
    else
    {
      FileWriterObj = new FileWriter(FileObj.getName(), true)
      FileExistsFlag = 1
    }

    var writer = new PrintWriter(FileWriterObj)

    if(FileExistsFlag == 0)
        writer.write("new file")
    else
    writer.append("appending to old file")

Internet search asks to use the below:

FileObj.getParentFile().mkdirs();
FileObj.createNewFile();

But it creates empty files inside the directory and creates new files outside the directory and appends to it. And also few posts suggests that there is no need to use createNewFile() to create a file.

I tried giving various path formats like below:

var DirectoryName: String = "../LogFiles"
var DirectoryName: String = "/home/ms/Desktop/Project/LogFiles"

But still it does not create the files inside the directory.
Can you please point me what I’m missing?

OS: Ubuntu 12.04

  • 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-15T18:17:56+00:00Added an answer on June 15, 2026 at 6:17 pm

    You should be using FileObj.getAbsolutePath, rather than FileObj.getName. getName just returns the name of the file, which explains why it was always being placed in the current directory.

    The empty files issue could be solved by calling writer.close() at the end of your function. I don’t think it’s necessary to use createNewFile

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

Sidebar

Related Questions

Okay, this is probably a very basic question; but, I'm just getting back in
Problem : This is a probably a very basic question, but how do I
This probably is a very very basic question but i can't seem to find
This is probably a very basic question but I haven't found any detailed information
This is probably a very basic R question...and feel a bit bad about asking...but
This is probably a very basic question, but I could not get my way
This is probably a very basic question but after reading documentation I still can't
This is probably a very basic question, but I'm really new to generics in
I know this question is probably very basic, but I am a beginner so
I realize that this is probably a very basic question, but I have spent

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.