【问题标题】:how to print hebrew charecters in Netbeans's console如何在 Netbeans 控制台中打印希伯来语字符
【发布时间】:2020-04-28 15:44:21
【问题描述】:

我正在使用 Netbeans IDE 11.0,我正在尝试打印一些像这样的希伯来语文本:

System.out.println("שלום");

我尝试将“-J-Dfile.encoding=UTF-8”添加到 ../etc/netbeans.conf 中,并检查了项目设置并将编码设置为 UTF-8,但我仍然看不到控制台窗口内的文本。 我只是得到一个空行。

我做错了什么?

编辑:

我正在做的示例是从此处显示的 google 表格中提取一些内容:https://developers.google.com/sheets/api/guides/values

我在 Netbeans 中创建了一个带有 Gradle 的 Java 项目并粘贴了示例。 我将电子表格 ID 更改为我的谷歌工作表 ID,并尝试获取一些希伯来语内容。如果有英文内容,则打印出来,但没有希伯来文内容。

主程序如下所示:

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class Main {

    private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart. If modifying
     * these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

    /**
     * Creates an authorized Credential object.
     *
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = Main.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    /**
     * Prints the names and majors of students in a sample spreadsheet:
     * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
     */
    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final String spreadsheetId = "my_google_sheet_id";
        final String range = "A2:E";
        Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        ValueRange response = service.spreadsheets().values()
                .get(spreadsheetId, range)
                .execute();
        List<List<Object>> values = response.getValues();
        if (values == null || values.isEmpty()) {
            System.out.println("No data found.");
        } else {
            System.out.println("Name, Major");
            for (List row : values) {
                // Print columns A and E, which correspond to indices 0 and 4.
                System.out.printf("%s, %s\n", row.get(0), row.get(4));
            }
        }
    }
}

这是来自谷歌表的表格:google sheet's table ,这就是我得到的:console

为什么它没有显示希伯来语测试?

【问题讨论】:

  • 默认 console font 缺少希伯来语字形。选择包含它们的字体。
  • 我换了字体还是不行
  • 代码没问题。您需要一种支持希伯来语的字体。如果您将字体更改为具有希伯来字符的字体,它将起作用。由this answer证明

标签: java encoding utf-8 hebrew netbeans-11


【解决方案1】:

更新:这已被澄清为 NetBeans-Gradle 问题。错误报告是here

这对我有用(在 NetBeans 11.1 中):

PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8);
out.println("שלום");

输出:

我使用的是等宽字体:

【讨论】:

  • 我打开了一个新的 Java 项目并且它正在工作,但是在我的 Java 和 Gradle 中它不起作用。它与它有某种联系?
  • 明白了——我碰巧在我的例子中使用了 Maven。我没有太多使用 Gradle。但是当我刚才尝试一个新的 Gradle 项目时,我看到了你的问题。即使在我的 build.gradle 中有以下内容:compileJava.options.encoding = "UTF-8"。即使这样:tasks.withType(JavaCompile) { options.encoding = "UTF-8" },希伯来语、中文等也不会显示在控制台中。
  • 好的,你知道怎么解决吗?我编辑问题并添加我想做的事情..
  • @NadavRosenberg 我认为你应该首先report the bug。然后你可能会尝试不同的 IDE。或者只使用 Maven。
  • 我的 Eclipse (v. 2019-12) 工作正常 - 我只需要将 compileJava.options.encoding = "UTF-8" 添加到 build.gradle。
猜你喜欢
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 2016-03-16
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多