簡體   English   中英

InputStream/Reader 未正確讀取資產文件

[英]Asset File is not being read correctly by InputStream/Reader

我想構建自己的密碼生成器。 因此,我創建了包含所有符號的文件,這些符號當前位於手動創建的文件夾“資產”中。 我還創建了一個方法,它將以字符串數組的形式返回單個文件內容。 該方法似乎已經識別了給定的文件,因為它沒有捕獲"FileNotFoundException"並且它正確地獲取了文件的行數。 現在我的問題是,返回的 Array 不包含文件的內容,而只包含與文件行數相對應的"null"s數量。 我希望這些信息會起作用。 誰能幫我?

我已經嘗試使用不同的字符集並將文件移動到文件夾"/res/raw/"

public String[] getList(String filename) {

    String[] zeichenliste = new String[0];

    try {
        InputStream is = getAssets().open(filename);
        BufferedReader br = new BufferedReader (new InputStreamReader(is));
        int length = 0;

        while (br.readLine() != null) length++;
        zeichenliste = new String[length];

        br = new BufferedReader (new InputStreamReader(is));
        for (int i = 0; i < length; i++) zeichenliste[i] = br.readLine();
        br.close();
        is.close();

    } catch (IOException e) {
        e.printStackTrace();
        return(null);
    }
    return zeichenliste;
}

這是一個如何做到這一點的示例。 我在代碼中包含了注釋。

// We return a collection to:
//  1: Have a variable-length 'array' returned.
//  2: Abstract the internal implementation of the actual collection used.
// We take an InputStream, so our function is more dynamic in what way it can parse lines (i.e. not just from files)
//  Note: this won't really work on a stream that does not end.
// We renamed the function, to be more representable to what it does
public Collection<String> readAllLines(InputStream stream) 
    throws IOException // We do not handle this ourselves, since that would take away some abstraction. But it is up to you.
{
    // We're not doing try-with-resources now, because the caller owns the stream.
    // We're also passing a charset, so that we support all languages.
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));

    // BufferedReader has a built-in stream for all its lines.
    Stream<String> linesStream = reader.lines();

    // We collect all our lines using a built-in collector that returns it as a list.
    List<String> lines = linesStream.collect(Collectors.toList());

    return lines;
}

然后調用它:

Collection<String> lines;
try(InputStream stream = getAssets().open(filename))
{
    lines = readAllLines(stream);
}

正如我在評論中指出的那樣:在您的方法中,您嘗試讀取文件的末尾,因為您已經對其進行了一次迭代以計算所有行。 集合是解決這個問題的好方法。

PS:如果您確實需要基於索引的集合,那么您可以更改readAllLines的定義以返回一個List

我解決了這個問題:

public String[] getZeichenListe(String dateiname) {
String str = "";
try {
    InputStream is = getAssets().open(dateiname);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String buffer = "";

    while ((buffer = br.readLine()) != null) str = buffer + ";" + str;
    is.close();
    br.close();

} catch (IOException e) {
    e.printStackTrace();
}
return str.split(";");

}

雖然我不太確定我認為問題是我只重新初始化了 BufferedReader 而不是 InputStream。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM