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

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

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, oracle java, Stack Overflow… I am near to send my computer thru the window (fortunately, these windows cannot be opened…)

Help !

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]);

Thank you !

I personally like the explanations on bealdung.com. You can simply add the keyword bealdung to a Google search.

I love what’s in the shared link: nearly everything excepted… Reading File !

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

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

But still missing a working RAD like Xojo…

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.

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

  1. Using the built-in BASIC commands like Open , Get, Put, and Close. 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) 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 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). 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.

:stuck_out_tongue:

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