简体   繁体   中英

Loading an Array from a File

I am working on an assignment with the following instructions,

Write a method that will load an array of floating point numbers from a file named floats.txt that you create, and then return that array. Assume the first value in the file holds the size of the array. Be sure to call your method.

I have created the following file with the title floats.txt

5
  4.3
  2.4
  4.2
  1.5
  7.3

I have never written a method that will return an array, nor have I ever created an array that reads from a file. Not asking anyone to write the program for me, by any means, but would appreciate some suggestions to get me started. I have written the method header as follows,

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

Thanks.

For Java 7 I would use 1 row code:

List<String> lines = Files.readAllLines(Paths.get("floats.txt"), StandardCharsets.UTF_8);

Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

See Doc here

You need to read the file into memory, you can do that line by line following what is here:

How to read a large text file line by line using Java?

Once you have a line of the file you can add it to your array.

Have a look at the javadoc for Scanner: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

There are methods such as Scanner.next() that will return the next value in the file.

You can open the file and read the elements one by one while storing them in each array element till it reaches null. Are you familiar with ArrayList?

I would pass the path of the file to your constructor (as follows), you still need to add one or two things... see here for example.

/**
 * 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[] {};
}
 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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM