This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
func readLines(path string) ([][]byte, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
lines := bytes.Split(b, []byte{'\n'})
return lines, nil
}
This is an alternative solution, in case you prefer each line as a byte slice (instead of a string)
func readLines(path string) ([]string, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
lines := slices.Collect(strings.Lines(string(b)))
return lines, nil
}
strings.Lines returns an iterator.
Warning: each line contains its original terminating newline.
Warning: each line contains its original terminating newline.
FileReader R = new FileReader(path);
BufferedReader r = new BufferedReader(R);
List<String> lines = r.lines().toList();
r.close();
Scanner s = new Scanner(new File(path));
s.useDelimiter("\\R");
List<String> lines = s.tokens().toList();
s.close();
File f = new File(path);
List<String> lines = readAllLines(f.toPath());
$lines = file($path);
if ($lines === false)
die("Can't open file $path");
Function returns an array of lines in the file, or false.
We compare with === for type and value equality.
We compare with === for type and value equality.