简体   繁体   English

如何处理Java中不可见的换行符?

[英]How can I handle invisible new line characters in Java?

I am currently working on the code for a game in Java. 我目前正在为Java游戏编写代码。 I am supposed to fill an array with characters from a .txt file. 我应该用.txt文件中的字符填充数组。 The problem is that my programm also stores the invisible new line characters from the file and as a result the array is not filled correctly. 问题是我的程序还存储了文件中不可见的换行符,结果数组未正确填充。 I have already tried to replace the invisible characters but the result was disastrous (my array would only be filled for the first line and the rest would remain empty) here is my block of code used for the array: 我已经尝试过替换不可见的字符,但是结果是灾难性的(我的数组将只填充第一行,其余的将保留为空),这是我用于该数组的代码块:

    import java.util.*;
    import java.io.*;

    public class Test {
        public static void main(String[] args) throws IOException{
        char background[][] = new char [14][20];

    try {

        FileInputStream fileinput = new FileInputStream("background.txt");
            int r;
            for(int i=0;i<14;i++){
                for(int j=0;j<20;j++){//<<THIS LINE WAS CHANGED
                    while((r = fileinput.read()) != -1){
                        char c = (char) r;
                        background[i][j] = c;
                        break;
                    }
                }
            }
            fileinput.close();
        }

        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i=0;i<14;i++){
            for(int j=0;j<20;j++){
                System.out.print(background[i][j]);
            }
        }
}




}

Also the format of the .txt file can be found here: http://pastebin.com/NynJGkFk . .txt文件的格式也可以在这里找到: http : //pastebin.com/NynJGkFk Thanks a lot in advance! 在此先多谢!

以字符串形式读取该行,在该行上调用.trim()方法,然后使用.toCharArray()将其拆分为char。

如果您在Windows上运行,请使用“ \\ r \\ n”检查新行或从系统属性中获取特定于平台的行

String lineSeparator = System.getProperty("line.separator");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM