I'm trying to create an instance of a file to parse html records from a property value. the problem is in the url of the file that I must put in the file properties, here is my example :
the correspondance code for reading file :
public void extraxtElementWithoutId() {
Map<String,List<List<Element>>> uniciteIds = new HashMap<String,List<List<Element>>>();
FileReader fileReader = null;
Document doc = null;
try {
fileReader = new FileReader(new ClassPathResource(FILEPROPERTYNAME).getFile());
Properties prop = new Properties();
prop.load(fileReader);
Enumeration<?> enumeration = prop.propertyNames();
List<List<Element>> fiinalReturn = null;
while (enumeration.hasMoreElements()) {
String path = (String) enumeration.nextElement();
System.out.println("Fichier en question : " + prop.getProperty(path));
URL url = getClass().getResource(prop.getProperty(path));
System.out.println(url.getPath());
File inputFile = new File(url.getPath());
doc = Jsoup.parse(inputFile, "UTF-8");
//fiinalReturn = getListofElements(doc);
//System.out.println(fiinalReturn);
fiinalReturn = uniciteIds.put("Duplicat Id", getUnicityIds(doc));
System.out.println(fiinalReturn);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
fileReader.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Thank you in advance,
Best Regards.
You are making a very common mistake for line -
URL url = getClass().getResource(prop.getProperty(path));
Try with property value as ( by removing src ) - /testHtmlFile/test.html and so on. Don't change code.
UrlEnterer1=/testHtmlFile/test.html instead of preceding it with src.
prop.getProperty(path) should be as per your build path location for the file. Check your build directory as how these files are stored. These are not stored under src but directly under build directory.
This answer explains a little bit about path value for file reading from class path.
Also, as a side note ( not related to question ) , try not doing prop.getProperty(path) but directly injecting property value in your class using org.springframework.beans.factory.annotation.Value annotation.
Related
I would like to read files from sub directories of resource folder.
I am facing issues with jar execution.
This is my directory structure.
src/main/resources
|_ Conf
|_ conf1
|_ config.txt
|_ conf2
|_ config.txt
Here, I am trying to read config.txt files from all sub directories of Conf folder. I do not know what sub directories Conf will have. I know the classpath till Conf. So, I will give classpath till Conf and trying to get sub directories and files.
I tried to achieve this using ClassPathResource. This works fine if it is file. I am facing issues when it comes to directory. I am using getFile api to get the directory path to walk through that directory for sub directories which is causing issue in jar execution.
Here is my code:
Below code is to read sub directories in Conf folder.
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
ClassPathResource classPathResource = new ClassPathResource("Conf");
File dir = classPathResource.getFile();
Files.walk(Paths.get(dir.toString()))
.filter(Files::isDirectory)
// This is to exempt current dir.
.filter((Path p)->!p.toString().equals(dir.toString()))
.forEach(f-> {list.add(readDirectory(f.toString()));});
Reading each sub directory.
public Map<String, String> readDirectory(String dir) {
Map<String, String> map = new HashMap<String, String>();
String confDir = dir.substring(dir.lastIndexOf(File.separator)+1);
try {
Files.list(Paths.get(dir))
.filter(f->f.toString().matches(".*conf\\.txt"))
.forEach(file ->approvedTermsMap.put
(confDir,readFile(file.toFile())));
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
Reading file:
public String readFile(File confFile) {
StringBuffer terms = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new
FileReader(confFile)))
{
reader.lines().forEach(term->
terms.append(term + "|"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return terms.toString();
}
Here, I should not use classPathResource.getFile() to get the absolute path because it tries to find file in file system which will not avilable in case of jar. So, I need alternate way to get absolute path of resource directory. I have to pass it to File.walk api to find sub directories and files.
As mentioned in the question, first I want to get confX directories then read conf.txt files.
Finally, I could solve my issue as below.
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
try {
Resource resources[] = resolver.getResources("classpath:Conf/*/");
} catch (IOException e) {
e.printStackTrace();
}
This will give all sub directories of Conf directory. Here / at the end in classpath:Conf/*/ is very important. If we do not give / it will work normally but will not work in jar.
From the above code block resources[] array will contains directory location like this class path resource [Conf/conf1/] and so on. I need sub directory name to read corresponding file. Here is the code for it.
Arrays.asList(resources).stream()
.forEach(resource ->{
Pattern dirPattern = Pattern.compile(".*?\\[(.*/(.*?))/\\]$");
if (resource.toString().matches(".*?\\[.*?\\]$")) {
Matcher matcher = dirPattern.matcher(resource.toString());
if (matcher.find()) {
String dir = matcher.group(1);
readFile(dir);
}
}
});
public void readFile(String dir)
{
ClassPathResource classPathResource = new ClassPathResource(dir+ "/conf.txt");
try (BufferedReader fileReader = new BufferedReader(
new InputStreamReader(classPathResource2.getInputStream()))) {
fileReader.lines().forEach(data -> System.out.println(data));
}catch (IOException e) {
e.printStackTrace();
}
}
I need to map each txt file with its corresponding directory. That is why I approached this way. If you just need to get files and read you can do it like below. This will list everything under Conf directory.
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
try {
Resource resources[] = resolver.getResources("classpath:Conf/**");
} catch (IOException e) {
e.printStackTrace();
}
Try the following code. It can scan up the required files up to n levels which can be specified using maxDepth varaible in following code
// Finding a file upto x level in File Directory using NIO Files.find
Path start = Paths.get("/Users/***/Documents/server_pull");
int maxDepth = 5;
try(Stream<Path> stream = Files.find(start,
maxDepth,
(path, attr) -> String.valueOf(path).endsWith(".txt"))){
String fileName = stream
.sorted()
.map(String::valueOf)
.filter((path) -> {
//System.out.println("In Filter : "+path);
return String.valueOf(path).endsWith("config.txt");
})
.collect(Collectors.joining());
System.out.println("fileName : "+fileName);
}catch(Exception e){
e.printStackTrace();
}
Another way by using Files.walk methods as follows:
// Finding a file upto x level in File Directory using NIO Files.walk
Path startWalk = Paths.get("/Users/***/Documents/server_pull");
int depth = 5;
try( Stream<Path> stream1 = Files.walk(startWalk,
depth)){
String walkedFile = stream1
.map(String::valueOf)
.filter(path -> {
return String.valueOf(path).endsWith("config.txt");
})
.sorted()
.collect(Collectors.joining());
System.out.println("walkedFile = "+walkedFile);
}catch(Exception e){
e.printStackTrace();
}
When I getting something in my code I use a short path like
this.getStylesheets().add("/css/editorTool.css");
but when I writing a new file I give exact paht - like:
File f = new File("D:\\IdeaProjects\\SmartCRM for TB\\EditorTool\\resourses\\html\\test.html");
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(html);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
As i will have to distribute this app among some users I will not be able to garanty that the absolute path to my app will be the same. How i can write it to so that it is like "path_to_the_folder_that_is_contained_in_app + fileName?
you can use system property "user.dir" to get current working path, the code is alike this:
String currentWorkingPath = System.getProperty("user.dir");
String fullPath = currentWorkingPath + File.separator+ fileName;
then the file will be under currentWorkingPath when the program run on any PC.
You can use getResource() method of java.lang.Class<T>
The java.lang.Class.getResource() finds a resource with a given name
Documentation Here
usage:
File file = new File(getClass().getResource("html/test.html").getPath());
Quick one. I'm trying to deploy a program, which borks at the following code. I want to read a properties file named, adequately, properties.
Properties props = new Properties();
InputStream is;
// First try - loading from the current directory
try {
File f = new File("properties");
is = new FileInputStream(f);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace(System.err);
is = null;
}
try {
if (is == null) {
// Try loading from classpath
is = getClass().getResourceAsStream("properties");
}
//Load properties from the file (if found), else crash and burn.
props.load(is);
} catch (IOException e) {
e.printStackTrace(System.err);
}
Everything goes well when I run the program through Netbeans.
When I run the JAR by itself, though, I get two exceptions.
java.io.FileNotFoundException: properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
.
.
.
Exception in Application start method
Exception in Application stop method
java.lang.reflect.InvocationTargetException
.
.
.
(exception during props.load(is) because is == null)
I'm running the file from the "dist" folder. I've tried placing the properties file inside the folder with the jar, without result. Normally, the properties file is located in the root project folder.
Any ideas?
You read your file as a resource (getResourceAsStream("properties");). So it must be in the classpath. Perhaps in the jar directly or in a directory which you add to the classpath.
A jar is a zip file so you can open it with 7zip for example add your properties file to the jars root level and try it again.
Thanks to the comments, I built an absolute path generator based on the current run directory of the jar. Props to you, guys.
private String relativizer(String file) {
URL url = RobotikosAnomologitos.class.getProtectionDomain().getCodeSource().getLocation();
String urlString = url.toString();
int firstSlash = urlString.indexOf("/");
int targetSlash = urlString.lastIndexOf("/", urlString.length() - 2) + 1;
return urlString.substring(firstSlash, targetSlash) + file;
}
So my new file-reading structure is:
Properties props = new Properties();
InputStream is;
// First try - loading from the current directory
try {
File f = new File("properties");
is = new FileInputStream(f);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace(System.err);
is = null;
}
try {
if (is == null) {
// Try loading from classpath
String pathToProps = relativizer("properties");
is = new FileInputStream(new File(pathToProps));
//is = getClass().getResourceAsStream(pathToProps);
}
//Load properties from the file (if found), else crash and burn.
props.load(is);
} catch (IOException e) {
e.printStackTrace(System.err);
}
// Finally parse the properties.
//code here, bla bla
I'm trying to change some values in my config.properties file.There is change in values when I set them but it doesn't get saved. here is my code
public class Config {
String filename = "config.properties";
public void displayConfig() throws IOException {
Properties prop = new Properties();
InputStream input = null;
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("unable to find " + filename);
return;
}
prop.load(input);
System.out.println();
System.out.println(prop);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println(key + " : " + value);
}
return;
}
public void setConfig() throws Exception {
File filename = new File("config.properties");
Properties prop = new Properties();
FileInputStream in = new FileInputStream(filename);
prop.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
prop.setProperty("db", "csv");
prop.setProperty("user", "tej");
prop.setProperty("password", "54321");
prop.store(out, null);
out.close();
System.out.println();
System.out.println(prop);
}
}
and the output when I call displayConfig,setConfig,displayConfig is like
{user=localhost, db=dtcc, password=12345}
db : dtcc
user : localhost
password : 12345
{user=tej, db=csv, password=54321, key=value}
{user=localhost, db=dtcc, password=12345}
db : dtcc
user : localhost
password : 12345
Well, that's quite expected, since displayConfig() doesn't load its properties from the same location as setConfig().
displayConfig() loads them from the resource config.properties at the root of the classpath, and setConfig loads and saves them in a file in the current directory.
BTW; even if the current directory happens to be in the classpath, I think getResourceAsStream() caches the stream content the first time it's called.
Choose your poison: either you want readable and writable files, and you should use file IO, or you want read-only resources loaded from the classpath, and you should use getResource[AsStream]. Don't mix both.
I want to write into a *.properties file. Thats my code how I do this:
properties = loadProperties("user.properties");//loads the properties file
properties.setProperty(username, password);
try {
properties.store(new FileOutputStream("user.properties"), null);
System.out.println("Wrote to propteries file!" + username + " " + password);
I do not get an exception, but I also do not get the output written into the file.
Here is also my file-structure:
I appreciate your answer!!!
UPDATE
I load my properties file with:
InputStream in = ClassLoader.getSystemResourceAsStream(filename);
My question is, how to load it from a specific path?
Here is my "new" File Structure:
Here is my testing code:
#Test
public void fileTest() throws FileNotFoundException, IOException {
File file = null;
Properties props = new Properties();
props.setProperty("Hello", "World");
URL url = Thread.currentThread().getContextClassLoader()
.getResource("exceptions/user.properties");
try {
file = new File(url.toURI().getPath());
assertTrue(file.exists());
} catch (URISyntaxException e) {
e.printStackTrace();
}
props.store(new FileOutputStream(file), "OMG, It werks!");
}
It does creates and rewrites a file in my target/classes/exceptions directory (in a maven/eclipse proyect) so I guess it really works, but of course that is not tested in a JAR file.
Here is the file:
#OMG, It werks!
#Sat Nov 10 08:32:44 CST 2012
Hello=World
Also, check this question: How can i save a file to the class path
So maybe what you want to do never will work.