gpt4 book ai didi

java - 空格作为第一个字符读取文件java

转载 作者:行者123 更新时间:2023-12-02 11:25:56 25 4
gpt4 key购买 nike

我在工作中得到了一个config.txt,如下所示:

#test
@Email1
Vorname;Vorname:
Nachname;Nachname:
Anrede;Anrede:
Titel;Titel:
Firma;Firma:
Abteilung;Abteilung:
EMail;E-Mail:
Strasse;Strasse:
PLZ;PLZ:
Ort;Ort:
Land;Land:
Telefon;Telefon:
Fax;Fax:
Bemerkung;Bemerkung:
Stichwort1;Stichwort1:

@Email2
#Format: sqlSpaltenname;EmailFeldName
Suchfeld2;Suchfeld2:
Firma;FIRMA1:
Abteilung;ABTEILUNG:
Anrede;ANREDE:
Nachname;NAME:
Vorname;VORNAME:
Strasse;STRASSE:
PLZ;PLZ:
Ort;ORT:
Land;LAND:
Telefon;TELEFON:
EMail;EMAIL:
Stichwort1;STICHWORT1:
Stichwort2;STIcHWORT2:

使用以下代码读取文件时:

public Config createConfig(String filename){
config = new Config();
String contentType = "";
String[] temp;
File fileDir = new File(filename);
int counter = 0;
specialConfigs = new ArrayList<String>(Arrays.asList("Betreff", "Sender", "Type", "Startbalken", "Endbalken"));

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"));
String line;
while ((line = reader.readLine()) != null) {
counter++;
if(line.startsWith("#")){//auskommentiert
continue;
}
if(line.trim().length()==0){//leer
continue;
}
if(line.startsWith("@")){//Abschnittswechsel, @Email1, @Email2, @Einstellungen
contentType = line;
continue;
}

temp = line.split(";");
if(temp.length != 2){
if(specialConfigs.contains(temp[0]) && contentType.equals("@Email_Eigenschaften")){
parseSpecialContent(line);
} else {
Main.logger.warning("Fehler in der Konfigurationsdatei in Zeile: "+counter+"\nProgramm wird abgebrochen");
reader.close();
System.exit(0);
}
} else {
if(contentType.equals("@Email1")){
config.addEmailField(1, temp[0], temp[1]);
} else if(contentType.equals("@Email2")){
config.addEmailField(2, temp[0], temp[1]);
} else {
config.setParameter(temp[0], temp[1]);
}
}

}
reader.close();

} catch (FileNotFoundException e) {
Main.logger.severe("Fehler beim Einlesen der Konfigurationsdatei: "+filename+" Datei nicht gefunden."+"\nProgramm wird abgebrochen");
e.printStackTrace();
System.exit(0);
} catch (IOException e) {
Main.logger.severe("Fehler beim Einlesen der Konfigurationsdatei: "+filename+" Datei kann nicht gelesen werden."+"\nProgramm wird abgebrochen");
e.printStackTrace();
System.exit(0);
}

return config;
}

我总是遇到错误,因为读入时第一行#test始终在内存中,因为:“#test”它是一个前导空格。我尝试删除第一行再次重写整个文件。但无论我做了什么改变,它总是读作“#test”。在 Debug模式下,我手动将变量行的值更改为正确的值#test,一切工作正常。该程序早些时候也运行良好。该文件刚刚被修改为包含值电子邮件,而不是早期版本的电子邮件。早期版本仍然有效...有人可以帮忙吗?

最佳答案

它可能是一个UTF-8 BOM(文件开始标记),\uFEFF,一个零宽度的空格,用作检测某些Unicode格式的标记: UTF-8、UTF-16LE、UTF-16BE 等。

它是多余的(不需要),但允许Windows记事本区分本地编码和UTF-8。也许该文件是用记事本制作的,并保存为 Unicode(带 BOM)。

解决方案是

            while ((line = reader.readLine()) != null) {
line = line.replaceFirst("^\uFEFF", "");

这有点太多了,因为假设只涉及第一行。

关于java - 空格作为第一个字符读取文件java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com