FileSystemUtil { public static boolean checkFileOrResourceExists(String filename) { if (new File(filename).exists()) { return true; } try (InputStream is = FileSystemUtil.class.getResourceAsStream(removeClasspathPrefix(filename))) { return is != null; } catch (IOException e) { LOGGER.error("Error reading resource {}", filename, e); return false; } } private  FileSystemUtil(); static boolean checkFileOrResourceExists(String filename); static void assertFileOrResourceExists(String filename); static String readFileAsText(String filename); static String readFileAsText(String fileName, String alternateDir); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void writeStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void appendStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static byte[] readBytesFromFile(String filename); static void deleteDirectoryQuietly(String rootDirName); static final String CLASSPATH_PREFIX; }
@Test public void testCheckFileExists() { assertThat(FileSystemUtil.checkFileOrResourceExists("build.gradle"), is(true)); assertThat(FileSystemUtil.checkFileOrResourceExists("/stg/Test.stg"), is(true)); assertThat(FileSystemUtil.checkFileOrResourceExists("classpath:/stg/Test.stg"), is(true)); assertThat(FileSystemUtil.checkFileOrResourceExists("/not/existing/file"), is(false)); assertThat(FileSystemUtil.checkFileOrResourceExists("classpath:/not/existing/resource"), is(false)); }