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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:55:21+00:00 2026-06-01T03:55:21+00:00

Edit1: Updated my method. I think it helped… I wont be able to be

  • 0

Edit1: Updated my method. I think it helped… I wont be able to be sure until I can test it later.

Edit2: Reverted back to previous version to show the change from the original to the answer.

This is part of a bigger problem I’m having trying to distribute my program to other computers. I have a method which reads a file (specifically an XML file, but I think that’s irrelevant) into a String. This is a GUI application so the user selects a file and that file is read by this method. I also use this method to read resources, which is why I have the catch for a FileNotFoundException. If that’s caught then it tries to read it as a resource instead. If that doesn’t work then too bad I guess… Haha.

So there are 4 different kinds of I’m trying on this:

  • Work computer (development computer) in NetBeans (my IDE): works
  • Work computer from the jar (compiled by NetBeans): works
  • Personal computer in NetBeans (I’m using Dropbox so the files are synced up pretty well and all my references are correct): works
  • Personal computer from the jar (compiled by NetBeans): DOES NOT WORK

As far as I can tell, what happens for that last case is for some reason fileScanner.hasNext() returns false on the first go of the loop so nothing is appended to the fileString. I just don’t know what would cause it to behave this way! Any help would be appreciated! (Note, there is no error thrown, it all “works” fine as far as the computer things).

Here are my methods. Any help improving it would be appreciated as well!

  /**
   * This method reads a file into a string. If you have an file in the resources folder for example, you can say
   * "/resources/exampleFile.txt".
   *
   * @param location location of the resource in the resources folder
   * @return String of the file
   */
  public static String fileToString(String location) throws FileNotFoundException {
    Scanner fileScanner;
    try {
      InputStream is = StaticClass.class.getResourceAsStream(location);
      fileScanner = new Scanner(is);
    } catch (NullPointerException e) {
      fileScanner = new Scanner(new File(location));
    }
    StringBuilder fileString = new StringBuilder();
    while (fileScanner.hasNext()) {
      fileString.append(fileScanner.nextLine()).append(newline);
    }
    return fileString.toString();
  }
  • 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-01T03:55:23+00:00Added an answer on June 1, 2026 at 3:55 am

    I’m still not sure why, but I think it had to do with the Scanner. For some reason it wasn’t working quite right, but I just changed it to the following code:

      /**
       * Takes the file and returns it in a string
       *
       * @param location
       * @return
       * @throws IOException
       */
      public static String fileToString(String location) throws IOException {
        FileReader fr = new FileReader(new File(location));
        return readerToString(fr);
      }
    
      /**
       * Takes the given resource (based on the given class) and returns that as a string.
       *
       * @param location
       * @param c
       * @return
       */
      public static String resourceToString(String location, Class c) throws IOException {
        InputStream is = c.getResourceAsStream(location);
        InputStreamReader r = new InputStreamReader(is);
        return readerToString(r);
      }
    
      /**
       * Returns all the lines in the scanner's stream as a String
       *
       * @param r 
       * @return
       * @throws IOException  
       */
      public static String readerToString(InputStreamReader r) throws IOException {
        StringWriter sw = new StringWriter();
        char[] buf = new char[1024];
        int len;
        while ((len = r.read(buf)) > 0) {
          sw.write(buf, 0, len);
        }
        r.close();
        sw.close();
        return sw.toString();
      }
    

    The biggest change was using FileReaders and StringWriters instead of Scanners. I also separated my resourceToString and fileToString methods. I think that’s better anyway. So, anyway, there you go! I hope this helps someone in the future!

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

Sidebar

Related Questions

Im not sure about what authentification method I should use for my webservice. I've
I think I've found something which C# and Ruby can do but Python can
EDIT: Updated with suggestions from Bill Karwin below. Still very slow. I'm trying to
See updated input and output data at Edit-1. What I am trying to accomplish
A useful feature in Java is the option to declare a member method as
Been searching for the answer to this for a while now and I think
Trying to sort an array by writing my own sort method using recursion (Pine's
Last Updated: 2009-08-11 2:30pm EDT A few days ago I posted this question about
I'm looking for a way to create a simple HTML table that can be
I have a JavaScript object those properties are static, mostly. They can be determined

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.