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…)
In FreeBASIC, there are 4 possible ways to perform file I/O:
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.
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.
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.
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.
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