You have to iterate the files array and call getName() for each file:

String[] names = new String[files.length];
for (int i = 0; i < files.length; i++) {
   names[i] = files[i].getName();
}
Answer from AlexR on Stack Overflow
Top answer
1 of 7
16

Stored as strings:

public class ReadTemps {

    public static void main(String[] args) throws IOException {
    // TODO code application logic here

    // // read KeyWestTemp.txt

    // create token1
    String token1 = "";

    // for-each loop for calculating heat index of May - October

    // create Scanner inFile1
    Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");

    // Original answer used LinkedList, but probably preferable to use ArrayList in most cases
    // List<String> temps = new LinkedList<String>();
    List<String> temps = new ArrayList<String>();

    // while loop
    while (inFile1.hasNext()) {
      // find next line
      token1 = inFile1.next();
      temps.add(token1);
    }
    inFile1.close();

    String[] tempsArray = temps.toArray(new String[0]);

    for (String s : tempsArray) {
      System.out.println(s);
    }
  }
}

For floats:

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class ReadTemps {

  public static void main(String[] args) throws IOException {
    // TODO code application logic here

    // // read KeyWestTemp.txt

    // create token1

    // for-each loop for calculating heat index of May - October

    // create Scanner inFile1
    Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");


    // Original answer used LinkedList, but probably preferable to use ArrayList in most cases
    // List<Float> temps = new LinkedList<Float>();
    List<Float> temps = new ArrayList<Float>();

    // while loop
    while (inFile1.hasNext()) {
      // find next line
      float token1 = inFile1.nextFloat();
      temps.add(token1);
    }
    inFile1.close();

    Float[] tempsArray = temps.toArray(new Float[0]);

    for (Float s : tempsArray) {
      System.out.println(s);
    }
  }
}
2 of 7
2

If you don't know the number of lines in your file, you don't have a size with which to init an array. In this case, it makes more sense to use a List :

List<String> tokens = new ArrayList<String>();
while (inFile1.hasNext()) {
    tokens.add(inFile1.nextLine());
}

After that, if you need to, you can copy to an array :

String[] tokenArray = tokens.toArray(new String[0]);
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ read a file into an arraylist
Read a File into an ArrayList | Baeldung
April 4, 2025 - In a nutshell, the method reads the content of the specified file line by line into a list of strings. As shown above, we can also add a charset to specify the character encoding to be used when reading the file. Please note that we donโ€™t need to use a try-with-resources here as the file is always automatically closed. Alternatively, we can use the Guava library to convert a file into an ArrayList.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ read-file-into-an-array-in-java
Read File Into an Array in Java - GeeksforGeeks
February 21, 2022 - BufferedReader also provides a method called readLine that takes the entire line in a file as a string. Below is the example code to store the content of the file in an array. ... // import necessary packages import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GFG { // to handle exceptions include throws public static void main(String[] args) throws IOException { // list that holds strings of a file List<String> listOfStrings = new ArrayList<String>(); // load data from file BufferedReader bf = ne
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-file-to-array-java
Convert File to Array in Java โ€” javathinking.com
To read the contents of a file, we usually rely on classes like FileReader, BufferedReader, or the Files utility class from the java.nio.file package. An array is a fixed-size collection of elements of the same type. In the context of converting a file to an array, we often deal with arrays of strings, where each element of the array represents a line from the file.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ array to string conversions
Array to String Conversions | Baeldung
September 16, 2026 - In this article, we illustrated how to convert an array to string and back again using core Java and popular utility libraries.
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ converting-arrays-and-strings-in-java
Java Array to String and String to Array Conversion Guide
In Java, strings are treated as objects, and the language furnishes a robust set of methods for the manipulation of textual information. This tutorial provides a comprehensive guide on how to convert arrays to strings and strings to arrays in Java.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-read-a-file-into-an-arraylist
Java: Read a File into an ArrayList
April 11, 2019 - If, for an example, our file contains a long list that we want to sort, we'll have to read it into an adequate data structure, perform operations, and then persist it once again - in this case an ArrayList. This can be achieved with several different approaches: ... try { ArrayList<String> lines = new ArrayList<>(Files.readAllLines(Paths.get(fileName))); } catch (IOException e) { // Handle a potential exception }
๐ŸŒ
Physics Forums
physicsforums.com โ€บ homework help โ€บ engineering and comp sci homework help
Java: Reading a file into an array โ€ข Physics Forums
March 21, 2015 - I'm showing 2 ways to do this: 1) reading in the file contents into a list of Strings and 2) reading in the file contents into a list of String and then creating an array of Strings based on the size of the List and populating that array with the List of String This is part of a much larger article about how to read files in Java.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ array to string in java
Array to String in Java
November 27, 2023 - Sometimes, you might need a bit more control over how you convert the array to string. Maybe you're not a fan of those square brackets, or you need a specific separator between elements. In such cases, Java's String.join() and good old loops come to the rescue.