gpt4 book ai didi

java - 在Java中将货币符号读取为字符串

转载 作者:行者123 更新时间:2023-12-01 16:46:55 25 4
gpt4 key购买 nike

我实际上需要在我的 pgAdmin 数据库中导入货币的 csv 文件。我的文件如下;

enter image description here

我的代码如下,使用 ISO-8859-1 格式。

        File fileDir = new File(fileName);       
FileInputStream fis= new FileInputStream(fileDir);
InputStreamReader isr= new InputStreamReader(fis, "ISO-8859-1");
BufferedReader br = new BufferedReader(isr);
String line;
line = br.readLine();
String[] arrayHeader = line.split(";");
size = arrayHeader.length;

if(size==expectedSize){
//check header
String header[] = {"Currency_ISO", "Currency_Label", "Currency_Symbol"};
for(int i=0;i<header.length;i++){
if(arrayHeader[i].trim().equals(header[i])){
fileStatus = true;
sResult = "File Status: Valid <br/> ";
}
else{
fileStatus = false;
sResult = "File Status: Invalid <br/> ";
break;
}
}
}else{
fileStatus = false;
sResult = "File Status: Invalid <br/> ";
}

if(fileStatus){

while ((line = br.readLine()) != null) {
String[] arrayData = line.split(";", -1);

//create object
objCurrency = new clsCurrency();

if(arrayData[0] != "" && !arrayData[0].isEmpty() && arrayData[0].trim() != ""){

objCurrency.setsCurrencyIso(arrayData[0].trim());
}else{
objCurrency.setsCurrencyIso(null);
}

if(arrayData[1] != "" && !arrayData[1].isEmpty() && arrayData[1].trim() != ""){
objCurrency.setsCurrencyLabel(arrayData[1].trim());
}else{
objCurrency.setsCurrencyLabel(null);
}

if(arrayData[2] != "" && !arrayData[2].isEmpty() && arrayData[2].trim() != ""){
System.out.println("setsLocalCurrencySymbol: " + arrayData[2]);
objCurrency.setsCurrencySymbol(arrayData[2].trim());
}else{
objCurrency.setsCurrencySymbol(null);
}

lstCurrencyImported.add(objCurrency);

}//end while

如您所见,我有一个 System.out.Println() 来检查正在读取的符号值。但主要问题是符号没有被正确读取。它返回的是 ? 。请参阅下面的 println;

[stdout] (default task-7) setsCurrencySymbol: ?

我尝试将格式更改为 UTF-8UTF-16,但没有成功。我在网上看到Java有currency类,但问题是我需要直接从csv中读取它,而且我认为用户不知道每种货币的代码。

知道我该如何从这里开始吗?

最佳答案

如果您的 CSV 文件包含欧元符号,则它不可能是 ISO-8859-1。 ISO-8859-1 中不存在欧元符号。它被添加到 ISO-8859-15 .

继续的方法是,首先找出您的 CSV 文件实际使用的编码。然后,配置 Java InputStreamReader 以使用该编码。

关于java - 在Java中将货币符号读取为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680224/

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