亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

非拉丁字符顯示為“?”

非拉丁字符顯示為“?”

莫回無 2022-07-20 12:20:26
我有一個像name.label=名我的java代碼就像Properties properties = new Properties();try (FileInputStream inputStream = new FileInputStream(path)) {    Reader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));    properties.load(reader);    System.out.println("Name label: " + properties.getProperty("name.label"));    reader.close();} catch (FileNotFoundException e) {    log.debug("Couldn't find properties file. ", e);} catch (IOException e) {    log.debug("Couldn't close input stream. ", e);}但它打印姓名標簽: ?我正在使用java 8。
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

替換字符可能表示文件未使用指定的CharSet.


根據您構建閱讀器的方式,您將獲得有關格式錯誤輸入的不同默認行為。


當你使用


Properties properties = new Properties();

try(FileInputStream inputStream = new FileInputStream(path);

    Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {


    properties.load(reader);

    System.out.println("Name label: " + properties.getProperty("name.label"));

} catch(FileNotFoundException e) {

    log.debug("Couldn't find properties file. ", e);

} catch(IOException e) {

    log.debug("Couldn't read properties file. ", e);

}

你得到一個Reader配置CharsetDecoder為替換無效輸入的。相反,當您使用


Properties properties = new Properties();

try(Reader reader = Files.newBufferedReader(Paths.get(path))) { // default to UTF-8

    properties.load(reader);

    System.out.println("Name label: " + properties.getProperty("name.label"));

} catch(FileNotFoundException e) {

    log.debug("Couldn't find properties file. ", e);

} catch(IOException e) {

    log.debug("Couldn't read properties file. ", e);

}

將CharsetDecoder被配置為在格式錯誤的輸入上引發異常。


為了完整起見,如果兩個默認值都不符合您的需求,您可以通過以下方式配置行為:


Properties properties = new Properties();

CharsetDecoder dec = StandardCharsets.UTF_8.newDecoder()

    .onMalformedInput(CodingErrorAction.REPLACE)

    .replaceWith("!");

try(FileInputStream inputStream = new FileInputStream(path);

    Reader reader = new InputStreamReader(inputStream, dec)) {


    properties.load(reader);

    System.out.println("Name label: " + properties.getProperty("name.label"));

} catch(FileNotFoundException e) {

    log.debug("Couldn't find properties file. ", e);

} catch(IOException e) {

    log.debug("Couldn't read properties file. ", e);

}

另見CharsetDecoderCodingErrorAction。



查看完整回答
反對 回復 2022-07-20
  • 1 回答
  • 0 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號