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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:36:45+00:00 2026-06-07T16:36:45+00:00

I am trying to understand PrintWriter for a small program I’m making, and I

  • 0

I am trying to understand PrintWriter for a small program I’m making, and I cant seem to get java to make the file and then write on it. When I execute the program below it gives me a Filenotfoundexeption error on line 9. It also fails to make the file in the directory that I specified. I am new to this so please try and keep the answers simple. I am using Eclipse.

import java.io.PrintWriter;
import java.io.File;

public class Testing {

  public static void main(String[] args) {

    File file = new File ("C:/Users/Me/Desktop/directory/file.txt");
    PrintWriter printWriter = new PrintWriter ("file.txt");
    printWriter.println ("hello");
    printWriter.close ();       
  }
}
  • 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-07T16:36:48+00:00Added an answer on June 7, 2026 at 4:36 pm

    If the directory doesn’t exist you need to create it. Java won’t create it by itself since the File class is just a link to an entity that can also not exist at all.

    As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see

    FileNotFoundException – If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

    You should try creating a path for the folder it contains before:

    File file = new File("C:/Users/Me/Desktop/directory/file.txt");
    file.getParentFile().mkdirs();
    
    PrintWriter printWriter = new PrintWriter(file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to understand how class generics work and this bit just doesnt make
Trying to understand resources in java-land. I believe the following is true: Resources loaded
Trying to understand upcasting in Java. Recently observed strange behavior. Example: public class A
Trying to understand this open source app on github, it has a gem file:
I am trying to use PrintWriter.java but I am getting a rather strange problem
trying to understand preg_match, struggling to understand how to write and how to access
Just trying to understand how Java annotations work under the covers. Seeing as spring
I am trying to write a simple mail client in Java with sockets. But
trying to understand and learn how to write packages... testing with something i've always
i am trying to code a small XMPP gtalk client in java. I know

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.