从文件中检索的字符串内容在连接或相等性检查期间消失

问题描述 投票:0回答:1

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

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()来生成组件无效。到底是怎么回事?为什么级联会导致原始字符串在结果中消失,但有时会消失?为什么平等检查不起作用?

java string concatenation filereader fileinputstream
1个回答
1
投票

好的,即使没有必要的繁琐方式,此处也不会消失,并且程序可以正常工作。仅需离开此行即可检查:

 System.out.println(s2);

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

"*here\r" + "4"

这被打印为

*here
4

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

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

© www.soinside.com 2019 - 2024. All rights reserved.