Recommended way to open a text file included in a jar?

Forgive me if this seems like an ignorant question, but I keep reading two different things about loading resources in the javadocs and forums, and can't seem to connect them...
Say you want to open a text file and read the contents into a String or a List. Apparently, the preferred way to do this is something like:
fileName = "myFile.txt";
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
String line;
while ((line = reader.readLine()) != null) {...}
Now say you want to distribute your app in a jar file, with the text file included in the jar file. The above method won't work, because a FileReader operates on a File, and as I understand it, once myFile.txt is packed into the jar file, it's no longer a "file".
So what do you do? I keep reading that the best way to open the jarred text file is to use something like this:
InputStream is = getClass().getResourceAsStream(fileName);
Here's where I get confused: InputStream, and all of its subclasses, are now supposedly dispreferred for reading character data (in preference to Reader, as above). But no subclass of Reader can wrap around an InputStream, which would facilitate the reading of character data greatly, with methods like readLine().
So my question is: What's the best way to read a text file from within a .jar?
Thanks,
Gregory

great! glad it's working
here is a statement from the javadoc
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStreamReader.html
For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
kind regards
Waken16

Similar Messages

  • Opening a Text File?

    I am having no luck opening a text file that contains comma delimited data. I have tried some different ways and am having no luck at all. I can't even get a open file dialog to appear when I click a button.
    I did not see any example of this in the sdk sample plugins.
    Anyone able to help me out.
    The file I am trying to load is "D:\Test\TestFile.txt".

    If you did not escape the path's backslashes that may be part of the problem.
    Some example code:
    local LrDialogs = import 'LrDialogs'
    local LrFileUtils = import 'LrFileUtils'
    local LrPathUtils = import 'LrPathUtils'
    local file = 'C:\\temp\\Testfile1.txt'
    if not LrFileUtils.isReadable(file) then
    local dialogresult = LrDialogs.runOpenPanel {
      title = 'Select input text file',
      allowsMultipleSelection = false,
      initialDirectory = LrPathUtils.parent(file)
    file = dialogresult and dialogresult[1] or nil
    end
    if file and LrFileUtils.isReadable(file) then
    local collectedLines = {}
    for line in io.lines(file) do table.insert(collectedLines, line) end
    LrDialogs.message(table.concat(collectedLines, '\n'))
    end
    Herb

  • Opening a text file in a bundle with C++

    Hi folks! I'm working with bundles for the first time, and I'd like to know how to open a text file inside one with C++. What I have now is this:
    ifstream file = new ifstream(fileName.c_str());
    if (!file->is_open())
    return false;
    Where fileName is a std::string. This is the way I'm accustomed to opening files (I'm from a Windows development background -- don't hate me! ). Unfortunately, the is_open() test always fails.
    I know how to get the path of the bundle. Once I add on my data subdirectory and file name, the fileName variable ends up as this:
    /Users/sb/...project location.../Debug/MyProg.app/data/Dev Options.ini
    Should the path be different somehow? Thanks for any help!

    Your data folder most likely isn't directly inside the .app bundle. The first directory inside an .app bundle is named Contents. The article at the following URL should help you:
    http://www.meandmark.com/bundlespart1.html
    If you're going to use C++ streams to open files, you'll want to ignore the section on opening the file in Part 3 of the article. You'll want to read up on the Core Foundation CFURL functions. You'll want to use one of the functions that gives you a path to the text file that you can pass to a C++ file stream.

  • Is there a way to open a zip file received via email on the iPad? I tried and a message popped up saying mail cannot open the attachment.

    Is there a way to open a zip file received via email on the iPad? I tried and a message popped up saying mail cannot open the attachment.

    You need a third party app. Take a look at some of these on this Google page.
    Open zip files on iPad

  • HT4399 I have a Mac Book Air and have purchased individually numbers and pages.  I was given an excel file on a thumb drive and cannot open it.  Is there a way to open the xcl file in numbers?

    I have a MacBook Air, and have purchased individually Numbers and Pages.  I was sent an Excel file on a thumb drive but cannot open the file.  The message states that the Excel file is encryeted.  Is ther any way to open a PC file on my Mac Book Air?
    Pat and Bob

    Excel does not use the .xcl extension for any of its native file formats?  The only files I've ever seen with xcl as the file extension were associated with embedded application code projects.
    If this is in fact an Excel file, but an encrypted one, you cannot open it (in excel or any compatible application) without the decryption password.

  • Opening a text file in server from an applet running in the client

    Friends,
    I want to open a text file in the server(The server machine uses tomcat 4.1.12 server)from an applet running in the client machine;
    The applet invokes a servlet that opens the text file;this text file is opened in the server machine; but I want it to be opened in the client machine where the applet is running.
    Plese help me to get around this.

    You can open the textfile on the servlet and then send the information to the client (applet) as stirngs. The must then applet convert the stirngs into some object or simply display the information in someway. But then the text file that you are opening must be stored in some relevant tomcat directory e.g. on the server. If you want to open a file on the clients computer, you get into signed applets.

  • How to open a text file using button click event

    hi, How can i open a text file in a textpad or notepad on the click event of a button.?
    Thanks
    Jay

    Pnt,
    this will not work LV 8.0.1 and LV 8.6 will give back error 193.
    Attached is a VI to use the ShellExecute WinAPI. The VI is LV 7.1.1.
    Message Edited by waldemar.hersacher on 10-09-2008 10:48 PM
    Waldemar
    Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
    Don't forget to give Kudos to good answers and/or questions
    Attachments:
    ShellExecute.zip ‏27 KB

  • How do I open a text file to sort using bubble sort?

    Hi,
    I have a text file, which is a list of #'s.. ex)
    0.001121
    0.313313
    0.001334
    how do you open a text file and read a list? I have the bubble sort code which sorts.. thanks

    I wrote this, but am getting a couple errors:
    import java.util.*;
    import java.io.*;
    public class sort
         public static void main(String[] args)
              String fileName = "numsRandom1024.txt";
              Scanner fromFile = null;
              int index = 0;
              double[] a = new double[1024];Don't use double. You should use something that implements Comparable, like java.lang.Double.
              try
                   fromFile = new Scanner(new File(fileName));
              catch (FileNotFoundException e)
    System.out.println("Error opening the file " +
    " + fileName);
                   System.exit(0);
              while (fromFile.hasNextLine())
                   String line = fromFile.nextLine();
                   a[index] = Double.parseDouble(line);Don't parse to a double, create a java.lang.Double instead.
                   System.out.println(a[index]);
                   index++;
              fromFile.close();
              bubbleSort( a, 0, a.length-1 );No. Don't use a.length-1: from index to a.length-1 all values will be null (if you use Double's). Call it like this:bubbleSort(a, 0, index);
    public static <T extends Comparable<? super T>> void
    d bubbleSort( T[] a, int first, int last )
              for(int counter = 0 ; counter < 1024 ; counter++)
    for(int counter2 = 1 ; counter2 < (1024-counter) ;
    ) ; counter2++)Don't let those counter loop to 1024. The max should be last .
                        if(a[counter2-1] > a[counter2])No. Use the compareTo(...) method:if(a[counter2-1].compareTo(a[counter2]) > 0) {
                             double temp = a[counter2];No, your array contains Comparable's of type T, use that type then:T temp = a[counter2];
    // ...Good luck.

  • How to open a text file

    hello
    if so have an idea on how to open a text file
    thanks

    File file=new File("[path to the text file]");
    String encoding="[encoding of the file]";
    BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
    // Do whatever you like with the reader
    // For example write the text file to the output
    String line;
    while ((line=reader.readLine())!=null)
    System.out.println(line);
    reader.close();Hope it helps,
    Marcin
    PS. This is not the write forum category to post this question.

  • Can Muse open existing text files authored in GoLive?

    Can Muse open existing text files authored in GoLive and or Dreamweaver?

    Unfortunately not, for now, Muse can only open .muse extension files, created within Muse.

  • Hi - is there a way of opening XL Publisher files on Mac?

    Hi - does anyone know if there is a way of opening XL Publisher file on Mac?

    Malras wrote:
    Hi - does anyone know if there is a way of opening XL Publisher file on Mac?
    No there is not unless you install Windows on your Mac using Bootcamp or a virtual program such as Parallels.
    Cheers
    Pete

  • Can Quickword open a text file 10K?

    I have a couple of simple, notepad text files.
    6K opens (after about 5 seconds)
    90K, well, it doesn't
    480K, neither does this.
    I can open the files using notepad, direct from the phone.
    Can anyone else open simple text files of this range of size.
    ...Lyall

    Well, I have a Tab Separated variable file (output from Excel) with about 20 columns.
    I edited it down from 5000 lines to 1000, 500 and 250 lines.
    I then copied all 4 files to the E90.
    QuickWord will open the 250 line file after about 5-10 seconds.
    It does NOT open (at least within my lifetime) the 500+ line files.
    So, how do I look at text files on the E90?
    ...Lyall

  • How to make an applet to read the Text file present inside a jar

    Hi All,
    I have writen one applet named ReadFile.java which reads a text file present in the same directory and does some manipulation of text file contents.
    The applet code runs successfully when i run the applet in command prompt like
    {color:#ff0000}*java ReadFile*{color}
    And i am getting the needed results.
    Then i made a jar file with the applet code and text file as
    {color:#ff0000}*jar cvf rf.jar ReadFile.class File1.txt*{color}
    Then i have inlcuded this applet inside a html file as
    {color:#ff0000}*<applet code= "ReadFile.class" width= "500" height= "300" archive = "rf.jar" ></applet>*{color}
    after this when i load the html file, the applet code is not executed fully. Its throwing FileNotFoundException: File1.txt.
    Applet is not recognizing trhe text file present inside the jar file.
    Can any body explain me how to overcome this problem. Any setting needs to be done for making the applet indicate the presence of Text file inside the jar file.

    what code in your applet gets the text file and reads it? are you using getResource or something similar?

  • Writing to and Opening a Text File from an Apex Application

    Hi there
    I would like to open a text document from apex.
    I have a screen with various parameters. Based on what the user selects, the result must open a text document with the parameters and other text.
    Does anyone know a good way to do this?
    Thanks
    Norah

    Hi there
    I would like to open a text document from apex.
    I have a screen with various parameters. Based on
    what the user selects, the result must open a text
    document with the parameters and other text.
    Does anyone know a good way to do this?
    Thanks
    NorahUnless you start a Java widget, I do not believe you can modify a file on the local users machine through the browser. Now if the file is on the server you are running from, this can be done.
    Thank you,
    Tony Miller
    UTMB/EHN

  • Any way to embed a text file and retain the link?

    Hi everyone,
    I am embedding the contents of a text file using "Insert > Choose...". This works wonderfully, but it appears to just insert the contents and gives me no way to update the text if the original text file changes. In Word, I can insert the text file and have it retain the link, then I have the ability to update the field (or all files in the document) with updated wording from my source files.
    In case you're wondering, I'm exploring using Pages for authoring the user guides for my company's software. We include command usage statements in our documentation and we have a workflow that runs each command, capturing it's help output into a text file, then we include this in the user guide document. Obviously, having the ability to update the user guide document with updated usage syntax is a required piece of functionality. I would like to use Pages instead of Word, but I'm not sure it can do this.
    Thanks in advance!
    Brian

    No, Pages can't do it. It doesn't keep a link to the original text document. You have to replace the text when you have made changes.

Maybe you are looking for