Read a specific line from a file: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 1,202:
===Using Java 11===
<syntaxhighlight lang="java">▼
▲<syntaxhighlight>
import java.io.IOException;
import java.nio.file.Files;
Line 1,212 ⟶ 1,210:
public static void main(String[] aArgs) throws IOException {
String fileName = "
Path filePath = Path.of(fileName);
Line 1,221 ⟶ 1,219:
}
private static final String ERROR_TOO_FEW_LINES = "File has
private static final String ERROR_EMPTY_LINE = "Line 7 is empty";
Line 1,268 ⟶ 1,266:
<pre>julia> open(line -> read_nth_lines(line, 7), "path/to/file")
"Hi, I am the content of the seventh line\n"</pre>
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>(0:"135-0.txt")7
"will have to check the laws of the country where you are located before\r"</syntaxhighlight>
Note that line 0 is the first line of the file. If you dislike the concept of a "zeroth line", you should use 7-1 rather than 7 to retrieve the seventh line:
<syntaxhighlight lang=K>(0:"135-0.txt")7-1
"www.gutenberg.org. If you are not located in the United States, you\r"</syntaxhighlight>
=={{header|Kotlin}}==
Line 2,103 ⟶ 2,110:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func getNthLine(filename, n) {
var file = File
file.open_r.each { |line|
Num($.) == n && return line
}
warn "file #{file} does not have #{n} lines, only #{$.}\n"
return nil
}
var line = getNthLine("/etc/passwd", 7)
=={{header|Smalltalk}}==
Line 2,305 ⟶ 2,312:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="
var lines = File.read("input.txt").replace("\r", "").split("\n")
|