Java 中的 readAllLines 字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19468918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:32:44  来源:igfitidea点击:

readAllLines Charset in Java

java

提问by blimpse

Update: Thanks for the quick responses, everyone. I've resolved the Charset issue, but now something else is happening that I don't understand at all. Here's my code:

更新:感谢大家的快速回复。我已经解决了字符集问题,但现在发生了一些我根本不明白的事情。这是我的代码:

import java.io.*;
import java.nio.file.*;
import java.nio.charset.*;
public class readConvertSeq{
    private static String[] getFile(Path file){
        String[] fileArray = (String[])Files.readAllLines(file, StandardCharsets.US_ASCII).toArray();
        return fileArray;
    }   
    public static void main(String[] args){
        String[] test = readConvertSeq.getFile(Paths.get(args[0]));
        int i;
        for(i = 0; i < test.length; i++){
            System.out.println(test[i]);
        }   
    }   
}  

And here's the error:

这是错误:

readConvertSeq.java:6: error: unreported exception IOException; must be caught or declared to be thrown
    String[] fileArray = (String[])Files.readAllLines(file, StandardCharsets.US_ASCII).toArray();

I'm just trying to get an array of strings from a file, and I'm getting really frustrated by Java's pedantry. Here's my code:

我只是想从一个文件中获取一个字符串数组,我对 Java 的迂腐感到非常沮丧。这是我的代码:

import java.io.*;
import java.nio.file.*;
import java.nio.charset.*;
public class readConvertSeq{
    private static String[] getFile(Path file){
        String[] fileArray = Files.readAllLines(file, Charset("US-ASCII")).toArray();
        return fileArray;
    }   
    public static void main(String[] args){
        String[] test = readConvertSeq.getFile(Paths.get(args[0]));
        int i;
        for(i = 0; i < test.length; i++){
            System.out.println(test[i]);
        }   
    }   
}   

It gives me this:

它给了我这个:

readConvertSeq.java:6: error: cannot find symbol
    String[] fileArray = Files.readAllLines(file, Charset("US-ASCII")).toArray();
                                                  ^
  symbol:   method Charset(String)
  location: class readConvertSeq

I'm sure I've made some other mistakes as well, so feel free to give me any advice you can.

我确定我也犯了一些其他错误,所以请随时给我任何建议。

采纳答案by Mateusz Dymczyk

Charset is an abstract class therefore you cannot instantiate it with the newkeyword.

Charset 是一个抽象类,因此您不能使用new关键字实例化它。

To get a charset in Java 1.7 use StandardCharsets.US_ASCII

要在 Java 1.7 中获取字符集,请使用 StandardCharsets.US_ASCII

回答by SudoRahul

You need to make the below changes

您需要进行以下更改

String[] fileArray = (String[]) Files.readAllLines(file.toPath(), Charset.forName("US-ASCII")).toArray();
                     ^^^^^ - Cast required                        ^^^^ - Get Charset using forName            

See the docs of Files.readAllLines(Path, Charset).

请参阅Files.readAllLines(Path, Charset).

回答by Mureinik

Constructors in Java are called with the newoperator, so Charset("US-ASCII")is not a valid statement. Moreover, Charset's constructor is protected, so you'll have to use the static factory method to create it: Charset.forName("US-ASCII").

Java 中的构造函数是使用new运算符调用的,因此Charset("US-ASCII")不是有效的语句。此外,Charset的构造函数是受保护的,因此您必须使用静态工厂方法来创建它:Charset.forName("US-ASCII")

回答by A4L

Charsetdoes not have a public constructor so you have to use the static factory method Charset#forName

Charset没有公共构造函数,因此您必须使用静态工厂方法Charset#forName