# Load a text file (all at once) in an array?

**URL:** <https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087>\
**Category:** Java\
**Created:** [19 August 2024 12:04 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087 "2024-08-19T12:04:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [19 August 2024 12:04 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/1 "2024-08-19T12:04:13Z")

</div>

Hi,

I have a text file, columns are tab delimited and lines are Return delimited.

I want to load it and store its contents into an array.

I have this code to load the file contents, but it use a While loop to do so (.RealAll will be nice instead).

Here’s the code:  
import java.io.File; // Import the File class  
import java.io.FileNotFoundException; // Import this class to handle errors  
import java.util.Scanner; // Import the Scanner class to read text files

```auto
public class ReadFile {
  public static void main(String[] args) {
    try {
      File myObj = new File("Cisco_Kid.txt");
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {
        String data = myReader.nextLine();
        System.out.println(data);
      }
      myReader.close();
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}

```

I checked [w3schools.com](http://w3schools.com), oracle java, Stack Overflow… I am near to send my computer thru the window (fortunately, these windows cannot be opened…)

Help !

---

<div class="post-metadata">

**Author:** ![Alwaysbusy](https://ifnotnil.com/user_avatar/ifnotnil.com/alwaysbusy/32/894_2.png) [@Alwaysbusy](https://ifnotnil.com/u/Alwaysbusy)\
**Post date:** [19 August 2024 13:44 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/2 "2024-08-19T13:44:35Z")

</div>

```auto
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

String[] myArray = Files.readAllLines(Paths.get("Cisco_Kid.txt")).toArray(new String[0]);
```

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [19 August 2024 14:15 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/3 "2024-08-19T14:15:50Z")

</div>

Thank you !

---

<div class="post-metadata">

**Author:** ![Tomar](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@Tomar](https://ifnotnil.com/u/Tomar)\
**Post date:** [20 August 2024 14:43 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/4 "2024-08-20T14:43:23Z")

</div>

I personally like the explanations on [bealdung.com](http://bealdung.com). You can simply add the keyword bealdung to a Google search.

> **[Introduction to Java NIO2 File API | Baeldung](https://www.baeldung.com/java-nio-2-file-api)**
>
> A quick and practical guide to Java NIO2 File API

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [20 August 2024 15:37 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/5 "2024-08-20T15:37:49Z")

</div>

I love what’s in the shared link: nearly everything excepted… _Reading File_ !

We are far, far away from Xojo (near C…).

---

<div class="post-metadata">

**Author:** ![TomasJ](https://ifnotnil.com/user_avatar/ifnotnil.com/tomasj/32/4998_2.png) [@TomasJ](https://ifnotnil.com/u/TomasJ)\
**Post date:** [21 August 2024 12:23 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/6 "2024-08-21T12:23:36Z")

</div>

…that’s why I love freebasic and developed a kind of fetish for small (often less than 100 KB) Executables 😉

But still missing a working RAD like Xojo…

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [21 August 2024 13:39 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/7 "2024-08-21T13:39:13Z")

</div>

```auto
package javaapplication7;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 *
 * @author thorstenstueker
 */
public class JavaApplication7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        
       String Filecontent= Files.readString(Path.of("/home/thorstenstueker/user.reg"));
       String[] contentarray = Filecontent.split("\n");
        System.out.println("Length:"+contentarray.length);
        // TODO code application logic here
    }
    
}

```

Terminal output:  
Length:1489

Here you can see how I would do this in a two line Action.

---

<div class="post-metadata">

**Author:** ![TomasJ](https://ifnotnil.com/user_avatar/ifnotnil.com/tomasj/32/4998_2.png) [@TomasJ](https://ifnotnil.com/u/TomasJ)\
**Post date:** [21 August 2024 15:47 UTC](https://ifnotnil.com/t/load-a-text-file-all-at-once-in-an-array/4087/8 "2024-08-21T15:47:44Z")

</div>

In FreeBASIC, there are 4 possible ways to perform file I/O:

1. Using the built-in BASIC commands like [Open](https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpen) , [Get](https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgGetfileio), [Put](https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPutfileio), and [Close](https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgClose). This way is mostly portable across all platforms supported by FreeBASIC. Open files are identified by “file numbers”, that are specific to FreeBASIC and can’t be passed into functions from below.

2. Using the C stream I/O functions like fopen, fread, ftell, fclose (see Stream I/O in [C Standard Library Functions](https://www.freebasic.net/wiki/wikka.php?wakka=ProPgCruntime)) of the C library FreeBASIC relies on. This is slightly faster than and adds a few features beyond method above, and still well portable. Open files are identified by file pointers, as in the C language, again unique to this access method. The [FileAttr](https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFileattr) function can be used to return a stream I/O pointer from a file number as in 1. above.

3. Using the C low-level I/O functions like \_open, \_read, \_write, \_close (see Low Level I/O in [C Standard Library Functions](https://www.freebasic.net/wiki/wikka.php?wakka=ProPgCruntime)). Those functions should be portable, but so far headers are available for Win32 only, so code using them will not compile to any other platform by now.

4. Talk directly to the OS kernel (DOS: use DOS and DPMI INT’s , Win32: use API calls like CreateFile, WriteFile). This is no longer portable. Files are identified by handles generated by and specific to the OS kernel.

😛

```auto
Dim as String inhalt
Dim as String zeilen() '<-- The Array

if Open("text.txt" For Binary Access Read As #1) = 0 Then
  inhalt = space(lof(1)) '<--- allocates var based on Lenght of File 
  Get #1, , inhalt '<--- read once all into var
  zeilen = split(inhalt, Chr(10)) '<--- Split on LF into array 
  Close #1  
else
   Print "cannot open file"
End If

```
