Java 字符串到达时打印行的替代方法\n,同时仍使用fileinputstream

Java 字符串到达时打印行的替代方法\n,同时仍使用fileinputstream,java,string,count,split,Java,String,Count,Split,代码逐字符读入文件并将其追加到字符串中。一旦到达新行,它会将字符串拆分为一个数组,然后检查该数组中的值是否匹配,并在拆分前打印出字符串 这段代码的问题是,即使它完成了大部分工作,因为它是如何在while循环中检查它是否到达末尾的,如果它到达末尾,它将返回-1。这使得我无法打印文件的最后一行,因为文件末尾没有新行,所以它会在打印最后一行之前终止循环 正在读入的几行的示例 这个世界是棕色的,肮脏的。 24小时是白天,你好 无法将整个文件存储到数组中,无法使用缓冲读取器,我尝试过计算“|”的数量,但我

代码逐字符读入文件并将其追加到字符串中。一旦到达新行,它会将字符串拆分为一个数组,然后检查该数组中的值是否匹配,并在拆分前打印出字符串

这段代码的问题是,即使它完成了大部分工作,因为它是如何在while循环中检查它是否到达末尾的,如果它到达末尾,它将返回-1。这使得我无法打印文件的最后一行,因为文件末尾没有新行,所以它会在打印最后一行之前终止循环

正在读入的几行的示例

这个世界是棕色的,肮脏的。
24小时是白天,你好

无法将整个文件存储到数组中,无法使用缓冲读取器,我尝试过计算“|”的数量,但我似乎无法实现。因此,在这种情况下,如果它计数到6根管道,它将分裂,然后在打印前检查。我认为这可以解决不打印最后一行的问题。下面是我如何尝试实现|的计数

    File tempFile = new File(loadedFileName);
    FileInputStream datStream = new FileInputStream(tempFile);
    InputStreamReader readDat = new InputStreamReader(datStream);
    int data = readDat.read();
    String temp = "";

    // keeps reading in one character at a time and returns -1 if there are no more          
    //  characters
    while(data != -1){  
        char datChar = (char)data;
        if(temp.length() > 2){
            if((temp.substring(temp.length()-1)).equals("\n")){
                String[] arrayTemp = temp.split("\\|");

                if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
                    System.out.print(temp); 
                }
                temp = "";
            }
        }
        temp = temp+datChar;
        data = readDat.read();
    }

你最好使用and。这将在IO方面更加有效,意味着您无需担心自己处理断线:

    while(data != -1){  
        char datChar = (char)data;
        // checks to make sure that temp isn't an empty string first
        if(temp.length() > 2){
            // checks to see if a new line started and if it did splits  the current string into an array.
            if((temp.substring(temp.length()-1)).equals("\\|")){
                if(count == 6){

                String[] arrayTemp = temp.split("\\|");

                //then checks the variable in the array col and compares it with a value and prints if it is greater.
                if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
                    System.out.print(temp); 
                }
                temp = "";
                count = 0;
                }
            }
        }
        temp = temp+datChar;
        data = readDat.read();
    }
这将为您提供文件中的所有行,无论是否有终止换行符

编辑:如果您真的不能使用
BufferedReader
,我将通过检查当前字符是
\n
还是文件的结尾,并处理“到目前为止的行”,使您的代码更加简单:

请注意使用
StringBuilder
而不是重复连接。

除了检查换行符外,您还可以尝试检查这一点


此外,您可以重写代码以读取整行并拆分该行,而不是逐个字符读取文件。如果您这样做,您就不必检查换行符(我认为您甚至不必检查当前的读取行是否不是文件的最后一行。我不确定它是否更快(但我打赌是),但它肯定会导致更好的可读性(因此,更好的可维护性)代码。

我可能误解了你的问题,但在我看来,在这种情况下,使用基本的
扫描仪进行基于线的扫描要容易得多。以下内容是否可以简化您的问题

StringBuilder line = new StringBuilder();
while (true) {
    int next = readDat.read();
    if (next == '\n' || next == -1) {
        // Handle line here

        line = new StringBuilder();
        if (next == -1) {
            break;
        }
    } else {
        line.append((char) next);
    }
}

哈哈,谢谢,但我不能使用bufferedreader和readline,这是一种任务,实际上实现bufferedreader是它的一个不同部分,我已经完成了这一部分,是的,它确实让它变得容易多了。@user2677821:你应该先提到这一点。它完全改变了情况。啊,是的,很抱歉。之后看着你的编辑,我忘了我可以在if语句中使用| |来添加另一个参数。Nvm感谢你再次阅读代码,它在我的脑海中点击了!
StringBuilder line = new StringBuilder();
while (true) {
    int next = readDat.read();
    if (next == '\n' || next == -1) {
        // Handle line here

        line = new StringBuilder();
        if (next == -1) {
            break;
        }
    } else {
        line.append((char) next);
    }
}
Scanner input = new Scanner(new File("FileName.txt"));
while (input.hasNextLine()) {
    String[] arrayTemp = input.nextLine().split("\\|");
    //etc etc.
}