public static String read(final String location) { final URI uri = createURI(location); return Exceptions.tryIt( String.class, new Exceptions.TrialWithReturn<String>() { @Override public String tryIt() throws Exception { String path = location; path = getWindowsPathIfNeeded(path); if (uri.getScheme() == null) { Path thePath = FileSystems.getDefault().getPath(path); return read(Files.newBufferedReader(thePath, DEFAULT_CHARSET)); } else if (uri.getScheme().equals(FILE_SCHEMA)) { return readFromFileSchema(uri); } else if (uri.getScheme().equals(CLASSPATH_SCHEMA) || uri.getScheme().equals(JAR_SCHEMA)) { return readFromClasspath(uri.toString()); } else { return read(location, uri); } } }); }
public static List<String> readLines(final String location) { final String path = getWindowsPathIfNeeded(location); final URI uri = createURI(path); return (List<String>) Exceptions.tryIt( Typ.list, new Exceptions.TrialWithReturn<List>() { @Override public List<String> tryIt() throws Exception { if (uri.getScheme() == null) { Path thePath = FileSystems.getDefault().getPath(path); return Files.readAllLines(thePath, DEFAULT_CHARSET); } else if (uri.getScheme().equals(FILE_SCHEMA)) { Path thePath = FileSystems.getDefault().getPath(uri.getPath()); return Files.readAllLines(thePath, DEFAULT_CHARSET); } else { return readLines(location, uri); } } }); }
public static void eachLine(final String location, final EachLine eachLine) { final URI uri = createURI(location); Exceptions.tryIt( new Exceptions.Trial() { @Override public void tryIt() throws Exception { if (uri.getScheme() == null) { Path thePath = FileSystems.getDefault().getPath(location); BufferedReader buf = Files.newBufferedReader(thePath, DEFAULT_CHARSET); eachLine(buf, eachLine); } else if (uri.getScheme().equals(FILE_SCHEMA)) { Path thePath = null; if (Sys.isWindows()) { String path = uri.toString(); path = path.replace('/', Sys.windowsPathSeparator()); if (slc(path, 0, 6).equals("file:\\")) { path = slc(path, 6); } thePath = FileSystems.getDefault().getPath(path); } else { thePath = FileSystems.getDefault().getPath(uri.getPath()); } BufferedReader buf = Files.newBufferedReader(thePath, DEFAULT_CHARSET); eachLine(buf, eachLine); } else { eachLine(location, uri, eachLine); } } }); }