java.nio.charset.Charset.forName()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(759)

本文整理了Java中java.nio.charset.Charset.forName()方法的一些代码示例,展示了Charset.forName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Charset.forName()方法的具体详情如下:
包路径:java.nio.charset.Charset
类名称:Charset
方法名:forName

Charset.forName介绍

[英]Returns a Charset instance for the named charset.
[中]返回命名字符集的字符集实例。

代码示例

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Creates a new scanner.
 * There is also java.io.Reader version of this constructor.
 *
 * @param   in  the java.io.Inputstream to read input from.
 */
NegraPennLexer(java.io.InputStream in) {
 this(new java.io.InputStreamReader
      (in, java.nio.charset.Charset.forName("UTF-8")));
}

代码示例来源:origin: stackoverflow.com

String line;
try (
  InputStream fis = new FileInputStream("the_file_name");
  InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
  BufferedReader br = new BufferedReader(isr);
) {
  while ((line = br.readLine()) != null) {
    // Deal with the line
  }
}

代码示例来源:origin: stackoverflow.com

List<String> lines=Files.readAllLines(Paths.get("/tmp/test.csv"), Charset.forName("UTF-8"));
for(String line:lines){
 System.out.println(line);
}

代码示例来源:origin: stackoverflow.com

BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("ISO-8859-1")),1024);
 // ...
    // inside some iteration / processing logic:
    if (br.ready()) {
      int readCount = br.read(inputData, bufferOffset, inputData.length-bufferOffset);
    }

代码示例来源:origin: stanfordnlp/CoreNLP

System.out.println("Usage : java NegraPennLexer [ --encoding <name> ] <inputfile(s)>");
 encodingName = argv[1];
 try {
  java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid?
 } catch (Exception e) {
  System.out.println("Invalid encoding '" + encodingName + "'");
  return;
 NegraPennLexer scanner = null;
 try {
  java.io.FileInputStream stream = new java.io.FileInputStream(argv[i]);
  java.io.Reader reader = new java.io.InputStreamReader(stream, encodingName);
  scanner = new NegraPennLexer(reader);
  while ( !scanner.zzAtEOF ) scanner.yylex();
  System.out.println("File not found : \""+argv[i]+"\"");

代码示例来源:origin: stackoverflow.com

OutputStreamWriter char_output = new OutputStreamWriter(
  new FileOutputStream("some_output.utf8"),
  Charset.forName("UTF-8").newEncoder() 
);
InputStreamReader char_input = new InputStreamReader(
  new FileInputStream("some_input.utf8"),
  Charset.forName("UTF-8").newDecoder() 
);

代码示例来源:origin: checkstyle/checkstyle

charset = Charset.forName(charsetName);
  decoder = charset.newDecoder();
  decoder.onMalformedInput(CodingErrorAction.REPLACE);
try (BufferedReader reader = new BufferedReader(new StringReader(fullText))) {
  final ArrayList<String> textLines = new ArrayList<>();
  while (true) {
    final String line = reader.readLine();
    if (line == null) {
      break;

代码示例来源:origin: cbeust/testng

protected static boolean containsRegularExpressions(Path f, String[] strRegexps) {
 Pattern[] matchers = new Pattern[strRegexps.length];
 boolean[] results = new boolean[strRegexps.length];
 for (int i = 0; i < strRegexps.length; i++) {
  matchers[i] = Pattern.compile(".*" + strRegexps[i] + ".*");
  results[i] = false;
 }
 try (BufferedReader br = Files.newBufferedReader(f, Charset.forName("UTF-8"))) {
  String line = br.readLine();
  while (line != null) {
   for (int i = 0; i < strRegexps.length; i++) {
    if (matchers[i].matcher(line).matches()) {
     results[i] = true;
    }
   }
   line = br.readLine();
  }
 } catch (IOException e) {
  e.printStackTrace();
  return false;
 }
 for (int i = 0; i < results.length; i++) {
  if (!results[i]) {
   throw new AssertionError("Couldn't find " + strRegexps[i]);
  }
 }
 return true;
}

代码示例来源:origin: stackoverflow.com

URLConnection connection = new URL("https://www.google.com/search?q=" + query).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();

BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
  sb.append(line);
}
System.out.println(sb.toString());

代码示例来源:origin: alibaba/canal

public static void main(String[] args) {
    for (int i = 0; i < entries.length; i++) {
      Entry entry = entries[i];

      System.out.print(i);
      System.out.print(',');
      System.out.print(' ');
      if (entry != null) {
        System.out.print(entry.mysqlCharset);
        System.out.print(',');
        System.out.print(' ');
        System.out.print(entry.javaCharset);
        if (entry.javaCharset != null) {
          System.out.print(',');
          System.out.print(' ');
          System.out.print(Charset.forName(entry.javaCharset).name());
        }
      } else {
        System.out.print("null");
      }
      System.out.println();
    }
  }
}

代码示例来源:origin: ehcache/ehcache3

public static String urlToText(URL url, String encoding) throws IOException {
 Charset charset = encoding == null ? StandardCharsets.UTF_8 : Charset.forName(encoding);
 try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), charset))) {
  return reader.lines().collect(joining(System.lineSeparator()));
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

System.out.println("Usage : java NegraLexer [ --encoding <name> ] <inputfile(s)>");
 encodingName = argv[1];
 try {
  java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid?
 } catch (Exception e) {
  System.out.println("Invalid encoding '" + encodingName + "'");
  return;
 NegraLexer scanner = null;
 try {
  java.io.FileInputStream stream = new java.io.FileInputStream(argv[i]);
  java.io.Reader reader = new java.io.InputStreamReader(stream, encodingName);
  scanner = new NegraLexer(reader);
  while ( !scanner.zzAtEOF ) scanner.yylex();
  System.out.println("File not found : \""+argv[i]+"\"");

代码示例来源:origin: spotbugs/spotbugs

/**
 * These are all fine and should not be flagged.
 */
@NoWarning(value="DM_DEFAULT_ENCODING")
public void notBugs() throws IOException {
  String a = "foobar";
  a.getBytes(Charset.forName("UTF-8"));
  a.getBytes("UTF-8");
  new String(new byte[] {}, "UTF-8");
  new String(new byte[] {}, 0, 0, "UTF-8");
  (new ByteArrayOutputStream()).toString("UTF-8");
  new InputStreamReader(new FileInputStream(""), "UTF-8");
  new OutputStreamWriter(new FileOutputStream(""), "UTF-8");
  new PrintStream(new File(""), "UTF-8");
  new PrintStream(new FileOutputStream(""), true, "UTF-8");
  new PrintStream("", "UTF-8");
  new PrintWriter(new File(""), "UTF-8");
  new PrintWriter("", "UTF-8");
  new Scanner(new FileInputStream(""), "UTF-8");
  new Formatter("", "UTF-8");
  new Formatter(new File(""), "UTF-8");
  new Formatter(new FileOutputStream(""), "UTF-8");
  new StringBuilder().toString();
  new ArrayList<Object>().toString();
  List<String> failures = new ArrayList<String>();
  failures.toString();
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Creates a new scanner.
 * There is also java.io.Reader version of this constructor.
 *
 * @param   in  the java.io.Inputstream to read input from.
 */
CHTBLexer(java.io.InputStream in) {
 this(new java.io.InputStreamReader
      (in, java.nio.charset.Charset.forName("UTF-8")));
}

代码示例来源:origin: atlassian/commonmark-java

public static String readAsString(URL url) {
    StringBuilder sb = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName("UTF-8")))) {
      String line;
      while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
      }
      return sb.toString();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: cmusphinx/sphinx4

public static void save(ConfigurationManager cm, File cmLocation) {
  if (!cmLocation.getName().endsWith(CM_FILE_SUFFIX))
    System.err.println("WARNING: Serialized s4-configuration should have the suffix '" + CM_FILE_SUFFIX + '\'');
  assert cm != null;
  try {
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cmLocation), Charset.forName("UTF-8")));
    String configXML = ConfigurationManagerUtils.toXML(cm);
    pw.print(configXML);
    pw.flush();
    pw.close();
  } catch (FileNotFoundException e1) {
    e1.printStackTrace();
  }
}

代码示例来源:origin: loklak/loklak_server

public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
  InputStream is = new URL(url).openStream();
  try {
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String jsonText = readAll(rd);
    JSONArray json = new JSONArray(jsonText);
    return json;
  } finally {
    is.close();
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
*  Returns a new Reader on the underlying source object.  
*/
public Reader getReader() throws IOException {
  // we want to remove the BOM windows adds from a file if the encoding is UTF-8
  // in other cases we depend on the charsets 
  Charset cs = Charset.forName(configuration.getSourceEncoding());
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  if (UTF8.name().equalsIgnoreCase(cs.name())) {
    in.mark(3);
    boolean hasBOM = true;
    try {
      int i = in.read();
      hasBOM &= i == 0xEF;
      i = in.read();
      hasBOM &= i == 0xBB;
      i = in.read();
      hasBOM &= i == 0xFF;
    } catch (IOException ioe) {
      hasBOM= false;
    }
    if (!hasBOM) in.reset();
  }
  return new InputStreamReader( in, cs );
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Creates a new scanner.
 * There is also java.io.Reader version of this constructor.
 *
 * @param   in  the java.io.Inputstream to read input from.
 */
ArabicLexer(java.io.InputStream in) {
 this(new java.io.InputStreamReader
      (in, java.nio.charset.Charset.forName("UTF-8")));
}

代码示例来源:origin: wiztools/rest-client

private static void appendHttpEntity(StringBuilder sb, HttpEntity e) {
  try {
    InputStream is = e.getContent();
    String encoding = e.getContentEncoding().getValue();
    System.out.println(encoding);
    BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName(encoding)));
    String str = null;
    while ((str = br.readLine()) != null) {
      sb.append(str);
    }
    br.close();
  } catch (IOException ex) {
    LOG.severe(ex.getMessage());
  }
}

相关文章