Anonymous user
Read a specific line from a file: Difference between revisions
→{{header|Java}}: adjusted code
(Added Haskell implementation) |
(→{{header|Java}}: adjusted code) |
||
Line 401:
=={{header|Java}}==
<tt>example: java -cp . LineNbr7 LineNbr7.java</tt><br>
<tt>output : line 7: public static void main(String[] args) throws Exception
<lang java>
import java.io.
public class LineNbr7 {
public static void main(String[] args) throws Exception {▼
BufferedReader br = new BufferedReader(fr);▼
String line = null;▼
while ((line = br.readLine()) != null && lineNbr++ < 7);▼
File f = new File(args[0]);
System.out.println("the file has zero length");▼
if (!f.isFile() || !f.canRead())
throw new IOException("can't read " + args[0]);
System.out.println("line 7: " + line);▼
System.out.println("the file has only " + (lineNbr - 1) + " line(s)");▼
try (LineNumberReader lnr = new LineNumberReader(br)) {
int lnum = 0;
&& (lnum = lnr.getLineNumber()) < 7) {
}
switch (lnum) {
case 0:
break;
case 7:
boolean empty = "".equals(line);
break;
default:
}
}
}
}</lang>
|