Created
September 18, 2016 20:54
-
-
Save trplll/9f6a922382051cc2874f7231ae5e9742 to your computer and use it in GitHub Desktop.
Simple command line file read with buffer reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.BufferedReader; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
public class Main { | |
public static void main(String[] args) { | |
if(args.length == 0) { | |
showUsage(); | |
return; | |
} | |
String filename = args[0]; | |
if(!Files.exists(Paths.get(filename))) { | |
System.out.println("\n File not found: " + filename); | |
return; | |
} | |
showFileLines(filename); | |
} | |
private static void showFileLines(String filename) { | |
System.out.println(); | |
try(BufferedReader reader = Files.newBufferedReader(Paths.get(filename))) { | |
String line = null; | |
while((line = reader.readLine()) != null) | |
System.out.println(line); | |
} catch(Exception ex) { | |
System.out.println(ex.getClass().getSimpleName() + " - " + ex.getMessage()); | |
} | |
} | |
private static void showUsage() { | |
System.out.println(); | |
System.out.println("Please provide the filename to process on the command line"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment