java - 基本文件读取到数组存储

标签 java arrays file-io collections methods

我有一个简单的 Java 问题,如果可能的话,我需要一个简单的答案。我需要从文件中输入数据并将数据存储到数组中。为此,我必须让程序打开数据文件,计算文件中元素的数量,关闭文件,初始化数组,重新打开文件并将数据加载到数组中。我主要无法将文件数据存储为数组。这是我拥有的:

要读取的文件在这里:https://www.dropbox.com/s/0ylb3iloj9af7qz/scores.txt

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.text.*;


public class StandardizedScore8
{



//Accounting for a potential exception and exception subclasses
public static void main(String[] args) throws IOException
{
    // TODO a LOT
    String filename;
    int i=0;


    Scanner scan = new Scanner(System.in);
    System.out.println("\nEnter the file name:");
    filename=scan.nextLine();



    File file = new File(filename);


    //File file = new File ("scores.txt");
    Scanner inputFile = new Scanner (file);

    String [] fileArray = new String [filename];
    //Scanner inFile = new Scanner (new File ("scores.txt"));

    //User-input
//  System.out.println("Reading from 'scores.txt'");
//  System.out.println("\nEnter the file name:");
//  filename=scan.nextLine();

    //File-naming/retrieving
//  File file = new File(filename);
//  Scanner inputFile = new Scanner(file);

最佳答案

我建议您使用 Collections 。这样,您不必事先知道文件的大小,您只需读取一次,而不是两次。集合将管理自己的大小。

关于java - 基本文件读取到数组存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11181248/

相关文章:

arrays - 非连续元素的最大总和(来自任意位置的 k 个元素)

android - Json Array Request with volley 不起作用

python - 将 df 保存和检索为 .csv,而不丢失类型信息

android - 多个文件写入线程

java - 如何使用网络驱动程序(Selenium - Java)切换到框架并单击弹出窗口?

java - 无法识别 HttpTransport 类型的 create RequestFactory() 方法

C typedef 结构的类型冲突

regex - 在 Perl 中使用正则表达式对行进行排序

java - 日志数据库连接挂起

java - 使用 MapReduce View 查询 Couchbase Lite 问题