Skip to content

Instantly share code, notes, and snippets.

@ambantis
Last active August 29, 2015 13:58
Show Gist options
  • Save ambantis/9986825 to your computer and use it in GitHub Desktop.
Save ambantis/9986825 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class OpenAFile7 {
public String getLine(String fileName, int n)
throws IOException, FileNotFoundException, NullPointerException {
Path file = Paths.get(".", fileName);
try (BufferedReader buf = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
for (int i = 1; i < n; i++)
buf.readLine();
return buf.readLine();
}
}
}
public class ZMain {
public static void main(String[] args) {
OpenAFile7 worker = new OpenAFile7();
String fileName = "Main.java";
int n = 5;
try {
String result = null;
if (result == null) {
System.out.println("Sorry, the line you requested: " + n +
", doesn't exist");
} else {
System.out.println("Okay, here's the line you requested:\n\t" + result);
}
} catch (FileNotFoundException e) {
System.out.println("Sorry, the file you requested: " + fileName +
" , doesn't exist");
} catch (IOException ioe) {
System.out.println("Sorry, there was a problem with IO, " +
"please try again later");
} catch (NullPointerException npe) {
System.out.println("Sorry, but the file name you provided is null");
} catch (Exception e) {
System.out.println("Sorry, there was an Internal Server Error, please try again later");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment