package com.qa.opencart.factory;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
public class ReadFile {
/*
The resource URL is not working in the JAR
If we try to access a file that is inside a JAR,
It throws NoSuchFileException (linux), InvalidPathException (Windows)
Resource URL Sample: file:java-io.jar!/json/file1.json
*/
private String getFileFromResource(String fileName) throws URISyntaxException{
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
// failed if files have whitespaces or special characters
//return new File(resource.getFile());
return new File(resource.toURI()).getAbsolutePath();
}
}
public static void main(String[] args) throws URISyntaxException {
// TODO Auto-generated method stub
String str =new ReadFile().getFileFromResource("log4j2.xml");
System.out.println(str);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.