簡體   English   中英

在 LibGDX(eclipse) 中逐字符讀取文件?

[英]Reading the file character by character in LibGDX(eclipse)?

我有一個 .txt 文件,其中包含以下文本:

111000111001
x00000010001
111110000001

我想將此內容放入字符串中,因此我使用此方法。

public void read() {
        FileHandle file = Gdx.files.internal("map.txt");
        String text = file.readString();

        System.out.println(text.charAt(12));//Here is the problem,it's showing  empty character instead of x


    }

當我想獲取第 12 個元素(第 2 行的 x)時,這是不可能的(我認為傳遞到新行有問題,但我不知道如何解決)。你能幫我嗎?

有一種叫做Carriage Return的東西,它在從文本文件讀取時使額外的字符出現在每行的末尾(准確地說是 3 個額外的字符),以避免以您可以使用的方式獲得這些字符:

text = text.replaceAll("(?:\\n|\\r)", "");

現在當你嘗試打印第 12 個元素時,你會得到你想要的“x”

System.out.println(text.charAt(12)); // Prints x

以下是有關replaceAll()方法的更多信息: Java Api: String.replaceAll()

暫無
暫無

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

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