Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件到数组-Java_Java - Fatal编程技术网

文件到数组-Java

文件到数组-Java,java,Java,我已经创建了以下代码来将文件内容显示到文本区域,并且成功了: BufferedReader in=null; 试一试{ in=new BufferedReader(新文件阅读器(“C:\\Users\\emily\\Documents\\Parts.txt”); 字符串str; 而((str=in.readLine())!=null){ jTextArea1.追加(“\n”+str); } }捕获(IOE异常){ }最后{ 尝试{in.close();}catch(Exception ex){

我已经创建了以下代码来将文件内容显示到文本区域,并且成功了:

BufferedReader in=null;
试一试{
in=new BufferedReader(新文件阅读器(“C:\\Users\\emily\\Documents\\Parts.txt”);
字符串str;
而((str=in.readLine())!=null){
jTextArea1.追加(“\n”+str);
}
}捕获(IOE异常){
}最后{
尝试{in.close();}catch(Exception ex){}
}
您可以尝试以下方法:

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("C:\\Users\\emily\\Documents\\Parts.txt"));
    String str;
    ArrayList<String> temp = new ArrayList<String>();
    String[] output;
    while ((str = in.readLine()) != null) {
        temp.add(str);
    }
    output = temp.toArray(new String[temp.size()]);
} catch (IOException e) {
} finally {
    try { in.close(); } catch (Exception ex) { }
}
BufferedReader in=null;
试一试{
in=new BufferedReader(新文件阅读器(“C:\\Users\\emily\\Documents\\Parts.txt”);
字符串str;
ArrayList temp=新的ArrayList();
字符串[]输出;
而((str=in.readLine())!=null){
临时添加(str);
}
输出=临时数组(新字符串[temp.size()]);
}捕获(IOE异常){
}最后{
尝试{in.close();}catch(Exception ex){}
}

更新的类路径和文件就可以了

Path path = Paths.get("C:\\Users\\emily\\Documents\\Parts.txt");
try {
    List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
    String[] array = lines.toArray(new String[lines.size()]);
    ...
} catch (IOException e) {
    //Logger.getLogger(getClass().getName()).log(Level.SEVERE,
    //                                           path.toString(), e);
    System.err.println("Could not read file: " + e.getMessage());
}
Path Path=Path.get(“C:\\Users\\emily\\Documents\\Parts.txt”);
试一试{
列表行=Files.readAllLines(路径,Charset.defaultCharset());
字符串[]数组=lines.toArray(新字符串[lines.size()]);
...
}捕获(IOE异常){
//Logger.getLogger(getClass().getName()).log(Level.SEVERE,
//path.toString(),e);
System.err.println(“无法读取文件:+e.getMessage());
}
最好只使用列表



添加对可能的IOException的处理,可能是在文件无法读取时。

您是否愿意添加OpenCSV或Commons IO之类的库?这是我第一次使用Java—当工程人员退出时,有人会加入到其中,所以我不确定这意味着什么,但如果它有帮助,我对任何事情都持开放态度…这似乎是最简单的,但当我尝试编译时,我得到了以下错误:“不可编译的源代码-类型java.awt.List不接受参数”您必须导入java.util.List-您得到了java.awt.List。请参阅不可编译源代码-未报告的异常java.io.IOException;必须被抓住或宣布被扔掉???(抱歉-这是我第一次在办公室使用Java…)我明白了!我试图显示字符串…而不是数组:)工作正常!非常感谢。那么如何在jtext区域中显示数组呢?我尝试过:jTextArea2=Arrays.toString(输出);但它不起作用。IDE建议创建局部变量“output”…@user3508197
output
在您试图访问它的范围内可能无法访问…请将其设置为全局变量。