Search code examples
javautf-8ioinputstreambufferedinputstream

how to read a .txt file with BufferedInputStreamReader


The problem, I think, is that the file is in the jar, which seems to make it a bit harder.

I don't understand how to instantiate an InputStreamReader when reading from a local file (inside the jar) as below:

package net.bounceme.dur.client;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Template {

    private static final Logger log = Logger.getLogger(Template.class.getName());
    StringBuffer buffer = new StringBuffer();

    public Template() {
    }

    public String getTemplate() {
        return buffer.toString();
    }

    public void load() {
//        try (BufferedInputStream input = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("template.txt"))) {
        try (BufferedInputStream input = new BufferedInputStream(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("template.txt")),"UTF-8")) {
            int data = input.read();
            while (data != -1) {
                buffer.append((char) data);
                data = input.read();

            }
        } catch (IOException ex) {
            Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

compile error (bad syntax):

-do-compile:
    [mkdir] Created dir: /home/thufir/NetBeansProjects/Mail/build/empty
    [mkdir] Created dir: /home/thufir/NetBeansProjects/Mail/build/generated-sources/ap-source-output
    [javac] Compiling 14 source files to /home/thufir/NetBeansProjects/Mail/build/classes
    [javac] /home/thufir/NetBeansProjects/Mail/src/net/bounceme/dur/client/Template.java:23: error: no suitable constructor found for BufferedInputStream(InputStreamReader,String)
    [javac]         try (BufferedInputStream input = new BufferedInputStream(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("template.txt")),"UTF-8")) {
    [javac]                                          ^
    [javac]     constructor BufferedInputStream.BufferedInputStream(InputStream,int) is not applicable
    [javac]       (actual argument InputStreamReader cannot be converted to InputStream by method invocation conversion)
    [javac]     constructor BufferedInputStream.BufferedInputStream(InputStream) is not applicable
    [javac]       (actual and formal argument lists differ in length)
    [javac] Note: /home/thufir/NetBeansProjects/Mail/src/net/bounceme/dur/client/gui/MailGUI.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error

BUILD FAILED
/home/thufir/NetBeansProjects/Mail/nbproject/build-impl.xml:924: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/Mail/nbproject/build-impl.xml:264: Compile failed; see the compiler error output for details.

Total time: 3 seconds
thufir@dur:~/NetBeansProjects/Mail$ 

for hovereel, the structure:

thufir@dur:~/NetBeansProjects/Mail$ 
thufir@dur:~/NetBeansProjects/Mail$ tree src/
src/
├── client.properties
├── logging.properties
├── META-INF
│   └── persistence.xml
├── net
│   └── bounceme
│       └── dur
│           └── client
│               ├── gui
│               │   ├── ApplicationDriver.java
│               │   ├── Goof.form
│               │   ├── Goof.java
│               │   ├── MailGUI.form
│               │   ├── MailGUI.java
│               │   └── TitleBuilder.java
│               ├── jdbc
│               │   ├── Agents.java
│               │   ├── Provinces.java
│               │   ├── State.java
│               │   ├── TitleException.java
│               │   ├── TitleFactory.java
│               │   └── Title.java
│               ├── MyProps.java
│               ├── NewJFrame.form
│               ├── NewJFrame.java
│               ├── SendTLS.java
│               └── Template.java
└── template.txt

7 directories, 21 files
thufir@dur:~/NetBeansProjects/Mail$ 

Solution

  • I think you wanted something like

    try (BufferedInputStream input = new BufferedInputStream(
        getClass().getClassLoader().getResourceAsStream("template.txt"))) {
    
    }
    

    Because there are only two constructors, per the BufferedInputStream Javadoc,

    BufferedInputStream(InputStream in)
         Creates a BufferedInputStream and saves its argument, the input stream in, 
         for later use.
    BufferedInputStream(InputStream in, int size)
         Creates a BufferedInputStream with the specified buffer size, and saves 
         its argument, the input stream in, for later use.
    

    And neither takes an InputStreamReader. You can of course wrap the above BufferedInputStream with one,

    try (BufferedInputStream bis = new BufferedInputStream(
        getClass().getClassLoader().getResourceAsStream("template.txt"))) {
      InputStreamReader input = new InputStreamReader(bis, "UTF-8");
    }