如何使用BufferedInputStreamReader读取.txt文件

时间:2014-07-29 16:32:26

标签: java utf-8 io inputstream bufferedinputstream

问题,我认为,该文件是 jar中,这似乎会让它变得更难。

我不明白如何在从本地文件(jar内)读取时实例化InputStreamReader,如下所示:

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);
        }
    }

}

编译错误(语法错误):

-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$ 

对于hovereel,结构:

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$ 

2 个答案:

答案 0 :(得分:2)

我觉得你想要像

这样的东西
try (BufferedInputStream input = new BufferedInputStream(
    getClass().getClassLoader().getResourceAsStream("template.txt"))) {

}

因为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.

并且都不需要InputStreamReader。你当然可以用上面的BufferedInputStream包装

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

答案 1 :(得分:1)

啊,我知道,您正在尝试将Reader传递给BufferedInputStream,并且不允许这样做,因为BufferedInputStream只接受InputStream参数,而不是Reader。您还没有向我们显示编译错误消息。相反,要么去Elliott的路线(1+),要么将InputStream传递给BufferedReader而不是BufferedInputStream。