首页 > 解决方案 > 从文件中检索的字符串内容在连接或相等检查期间消失

问题描述

我正在制作一个必须读/写纯文本文件的程序。我从文件中读取文本,然后对其进行处理以将字符串分成几个组件(即行,然后是变量/值)。当我尝试处理其中一些组件时,字符串的内容似乎会暂时更改或消失。此代码重现了该问题:

package spriteModder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ErrorThing {
    public static void main(String[] args) throws IOException {
        loadFile(new File("settings.config"));
    }
    public static void loadFile(File f) throws IOException {
        FileInputStream fis=new FileInputStream(f);
        int numread=1;
        StringBuilder contentBuilder= new StringBuilder();
        byte[] buffer=new byte[8];
        while(numread>0) {
            numread=fis.read(buffer);
            if(numread==-1){break;}
            contentBuilder.append(new String(buffer, 0, numread));
        }
        String source=contentBuilder.toString();
        System.out.println(source);
        String[] lines=source.split("\n");
        for(String s:lines){
            System.out.println(s);
            String[] a=s.split("=");
            for(String s2:a){
                System.out.println(s2);

                System.out.println(s2+"4");
                System.out.println(s2.equals("true"));
            }

        }
    }
}

这是输出:

 //these three lines are to verify the contents of the settings.config file, which works as expected
outputDir=*here 
lookInSubDirectories=true
inputDir=*here
outputDir=*here //the first line of the file
outputDir //the first component of the line
outputDir4 //the first component of the line after concatenation.
false //checks if the component (w/o concatenation) is equal to "true"
*here //second component of the line
4 //second component of the line after concatenation, which is missing the entire original component
false //checks if the second component of the line is equal to "true"
lookInSubDirectories=true
lookInSubDirectories //the first component of the second line
lookInSubDirectories4
false
true // the second component
4 //this component also disappears during concatenation
false // but also fails the equality check, even though the content is "true"
inputDir=*here
inputDir
inputDir4
false
*here
*here4 //you can see concatenation working on the third line's second component here for some reason
false

settings.config 的内容是:

outputDir=*here
lookInSubDirectories=true
inputDir=*here

我尝试更改字节缓冲区的大小,使用 FileReader 而不是 FileInputStream,并使用临时变量但没有成功。我还绕过了读取文件,只是将相同的内容硬编码到一个字符串中,从而避免了这个问题。但是,必须从文件中读取内容。当仅连接一个空字符串 ("") 时,连接始终有效,但其他任何内容都会导致原始内容在这两行中消失。无论使用 .equals 还是 ==,相等性检查都会失败。使用 substring() 而不是 split() 来生成组件没有效果。到底是怎么回事?为什么连接会导致原始字符串在结果中消失,但只是有时?为什么平等检查不起作用?

标签: javastringconcatenationfilereaderfileinputstream

解决方案


好的,所以这里没有任何内容,并且您的程序可以正常工作,即使以不必要的繁琐方式编写也是如此。离开这一行最容易检查:

 System.out.println(s2);

String作为拆分迭代的唯一输出。唯一的错误是得出的结论是某些东西“消失了”。我将大胆猜测并说您在 Windows 上运行此代码,或者将settings.config文件的换行符设置为\r\n而不是\n因此,仅在\n上拆分会留下悬挂的\r s,因此您有:

"*here\r" + "4"

这被打印为

*here
4

最好使用调试器或if语句检查挂起的\r 。

因此,您的程序与拆分abc=123"abc""123"关注(或者,更确切地说是"abc""123\r")一样多。但是请考虑使用 Java 的内置方法Properties从文件加载,或者至少使用 usea BufferedReader,我认为它的readLine方法会同时消耗回车符和换行符。


推荐阅读