Java 从文件加载数组

Java 从文件加载数组,java,arrays,methods,Java,Arrays,Methods,我正在做一项作业,作业内容如下: 编写一个方法,从您创建的名为floats.txt的文件中加载浮点数数组,然后返回该数组。假设文件中的第一个值包含数组的大小。一定要调用你的方法 我创建了以下文件,标题为floats.txt 5 4.3 2.4 4.2 1.5 7.3 我从未编写过返回数组的方法,也从未创建过读取文件的数组。不要求任何人以任何方式为我编写程序,但如果能给我一些建议,我将不胜感激。我已经编写了如下的方法头 public static double[] flo

我正在做一项作业,作业内容如下:

编写一个方法,从您创建的名为floats.txt的文件中加载浮点数数组,然后返回该数组。假设文件中的第一个值包含数组的大小。一定要调用你的方法

我创建了以下文件,标题为floats.txt

5
  4.3
  2.4
  4.2
  1.5
  7.3
我从未编写过返回数组的方法,也从未创建过读取文件的数组。不要求任何人以任何方式为我编写程序,但如果能给我一些建议,我将不胜感激。我已经编写了如下的方法头

  public static double[] floatingFile() throws IOException {
  Scanner fin = new Scanner(new File"floats.txt");

谢谢。

您需要将文件读入内存,您可以按照此处的内容逐行执行此操作:


一旦有了一行文件,就可以将其添加到数组中。

查看javadoc for Scanner:


有一些方法,例如Scanner.next()将返回文件中的下一个值。

您可以打开文件并逐个读取元素,同时将它们存储在每个数组元素中,直到它达到null。
您熟悉ArrayList吗?

我会将文件的路径传递给您的构造函数(如下所示),您仍然需要添加一两个内容。。。例如,见

/**
 * Read in a file containing floating point numbers and
 * return them as an array.
 * 
 * @param filePath
 *          The path to the file to read.
 * @return Any double(s) read as an array.
 */
public static double[] floatingFile(String filePath) {
  Scanner fin = null;
  try {
    // Open the file at filePath.
    fin = new Scanner(new File(filePath));
    // first read the size
    int advSize = 0;
    if (fin.hasNextInt()) {
      // read in an int.
      advSize = // DO SOMETHING TO READ AN INT.
    }
    // construct a dynamic list to hold the Double(s).
    List<Double> al = new ArrayList<Double>(advSize);
    // while there are Double(s) to read.
    while (fin.hasNextDouble()) {
      // read in a double.
      double d = // DO SOMETHING TO READ A DOUBLE.
      al.add(d);
    }
    // Construct the return array.
    double[] ret = new double[al.size()];
    for (int i = 0; i < ret.length; i++) {
      ret[i] = al.get(i);
    }
    return ret;
  } catch (FileNotFoundException ignored) {
  } finally {
    if (fin != null) {
      // close our file reader.
      fin.close();
    }
  }
  // Return the empty array.
  return new double[] {};
}
/**
*读入包含浮点数和
*将它们作为数组返回。
* 
*@param文件路径
*要读取的文件的路径。
*@返回读取为数组的任何双精度。
*/
公共静态双[]浮点文件(字符串文件路径){
扫描仪fin=null;
试一试{
//在filePath处打开文件。
fin=新扫描仪(新文件(文件路径));
//先看尺寸
int advSize=0;
if(fin.hasNextInt()){
//读入整数。
advSize=//执行某些操作以读取INT。
}
//构造一个动态列表来保存双精度。
List al=新阵列列表(advSize);
//虽然有两个(s)要读。
while(fin.hasNextDouble()){
//读两遍。
double d=//执行一些操作来读取double。
al.添加(d);
}
//构造返回数组。
double[]ret=新的double[al.size()];
对于(int i=0;i
对于Java 7,我将使用1行代码:

List<String> lines = Files.readAllLines(Paths.get("floats.txt"), StandardCharsets.UTF_8);
List lines=Files.readAllLines(path.get(“floats.txt”)、StandardCharsets.UTF_8);
读取文件中的所有行。此方法确保在读取所有字节或引发I/O错误或其他运行时异常时关闭文件。使用指定的字符集将文件中的字节解码为字符

请参阅try块中的Doc

public float[]readLines(String){//
FileReader FileReader=新的FileReader(文件名);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
List arrFloat=new ArrayList();
字符串行=null;
而((line=bufferedReader.readLine())!=null){
行。添加(行);
}
bufferedReader.close();
返回arrFloat.toArray(新的floatArray[arrFloat]);
}

我对ArrayList不太熟悉,在这方面我还是个新手。您可以使用ArrayList,它是一种使用列表界面的数组实现。使用此方法,您无需首先查找文件大小即可初始化数组。可能会有帮助。若要开始,您需要执行以下操作:对于返回数组的方法,您的方法的格式应为
public arrayList[]readLines(String)
。现在您已经打开了文件,可以使用
FileReader
BufferedReader
对象(检查coder粘贴的链接以了解文件处理)ArrayList包含类似
.add()、remove()
的方法,使用while循环从ArrayList添加/删除元素,您可以读取文件,直到它达到null,然后使用arrayList.add()将元素添加到arrayList中。要将arrayList转换为数组,“使用arrayList.toArray()”请使用上面解释的实现检查我在新答案中发布的代码片段
 public float[] readLines(String <your filename>){ //in the try block
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<float> arrFloat = new ArrayList<float>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    return arrFloat.toArray(new floatArray[arrFloat]);
}