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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:05:54+00:00 2026-06-09T20:05:54+00:00

I am trying to revise a java code to write something into a txt

  • 0

I am trying to revise a java code to write something into a txt file. The original code is:

try {
    out = new PrintStream(system.out, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I use FileOutputStream to do this, and revise the code to:

try {
    FileOutputStream os = new FileOutputStream("wiki.txt", true);
    out = new PrintStream(os, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

But it doesn’t work, the error is:

Wikipedia2Txt.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    FileOutputStream os = new FileOutputStream("wiki.txt");
                          ^
1 error

I try two ways: 1, I make a wiki.txt file manually on disk; 2, no wiki.txt exist before run the code.
But either doesn’t work. It just stopped when compiled.

So what is going on?
Thanks.

  • 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-09T20:05:55+00:00Added an answer on June 9, 2026 at 8:05 pm

    Java is not telling you that the file is not found, just that it may not be found at runtime, and your program is not ready to handle it.

    Here is one way to address this:

    try {
        FileOutputStream os = new FileOutputStream(file, true);
        out = new PrintStream(os, true, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException fnf) {
        // TODO Auto-generated catch block
        fnf.printStackTrace();
    }
    

    Here is another way:

    try {
        FileOutputStream os = new FileOutputStream(file, true);
        out = new PrintStream(os, true, "UTF-8");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        fnf.printStackTrace();
    }
    

    The first way ensures the compiler that your code is prepared to handle both exceptions separately; the second way ensures the compiler that your code is prepared to handle a superclass of both exceptions. The two ways are not the same, because the second one covers more exceptions that the first one.

    Finally, there is an alternative to silence the compiler by declaring your function with a throws block (either a common superclass or the two individual classes would do). This is a way to tell the compiler that your function has no idea of how to handle these exceptions, and that they should be handled by a caller. The consequence of this approach is that every caller of your function must put a try/catch around the call, or declare the exceptions using throws.

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
I'm trying out the code in this example and basically I want to limit
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
I am now trying to add a simple PE into my app. This PE
I'm trying to use git-p4 to clone perforce depot. I keep running into a
I am currently attempting to revise a Java Homework program for my Data Structures
I am trying to revise my build process to use ant with apache ivy
The following is the program, which I am trying to send e-mail. The code

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.