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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:26:11+00:00 2026-05-27T04:26:11+00:00

try { File file = new File(sample.txt); FileWriter fw = new FileWriter(file,true); fw.append(‘d’); fw.write(100);

  • 0
try {
    File file = new File("sample.txt");
    FileWriter fw = new FileWriter(file,true);
    fw.append('d');
    fw.write(100);
    fw.close();
} catch(IOException exp) {
    exp.printStackTrace();
}

I am unable to append or write anything to the file.

But I could read the content from the file.

Is anything wrong with my code?

  • 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-27T04:26:12+00:00Added an answer on May 27, 2026 at 4:26 am

    It sounds like you probably are writing to a file – but not the file you expect to. If no exceptions have been thrown (and swallowing an exception, just writing it to standard out, is rarely the right approach) then the file will exist somewhere.

    It will be in whatever directory the code is running from – which may well not be the same as the directory containing the sample.txt file you’re reading. I suggest you explore the file system, and also check the Run Configuration in Eclipse to see what the working directory for the app will be.

    As an aside, you should be closing the writer in a finally block so that it gets closed even if there’s an exception, like this:

    File file = new File("sample.txt");
    FileWriter fw = null;
    try {
        fw = new FileWriter(file, true);
        fw.append('d');
        fw.write(100);
    } catch(IOException) {
        // Ideally do something to indicate the failure to the caller
        // - do you need to catch this at all?
        e.printStackTrace();
    } finally {
        // From Guava
        Closeables.closeQuietly(fw);
    }
    

    Obviously you can do this without Guava but it’ll make things a lot simpler – and not just here. If you’re using Java 7 you can make it even simpler with a try-with-resources statement.

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

Sidebar

Related Questions

I tried a simple file write program in Android. try { //File f1=new File(/sdcard/Prag.txt);
New to batch files, first try actually. Trying to make a simple batch file
I try to create a new file inside a JSP and try to save
I have a file in Java FileInputStream in = null; try{ in = new
I have used this code to store Object to a file: try{ FileOutputStream saveFile=new
I try to lock a file and write to it with the code below:
I have a txt file, here is some sample data: Id,PriceOne,Company,PriceTwo,PriceThree 11,15.3599997,Japan ltd,12.23,3.1777777 12,,Koyoto
This is a sample .txt file: item1 item2 myString item3 item4 I created a
Hi I have a file try.SPEC. This file contains a word Version: 1.0.0.1 .
I have the following code to launch a file : try { path =

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.