Going through your code it appears all is good except for the ones mentioned above as well. Instead of writing.
Going through your code it appears all is good except for the ones mentioned above as well. Instead of writing String line = br.readLine(); loadProperties(line); while (br.readLine()! = null) { sb.
Append(line); sb. Append("\n"); line = br.readLine(); } prefer to write String line = null; while ((line = br.readLine())! = null) { loadProperties(line); sb.
Append(line); sb. Append("\n"); line = br.readLine(); } Moreover, since it's a java code, so prefer to put forward slash (/) instead of two backslashes(\), while describing a path to a file. For Example : BufferedReader br = new BufferedReader(new FileReader("C:/Apache/tomcat/webapps/GaganIsOnline/WEB-INF/classes/names.
Txt")); And please do check, if the file path is proper, mean to say, does the said file abc. Txt do exists in that given location. Regards.
Fixed one bug as loadProperties is called just once ... – havexz Nov 28 at 18:08.
EDIT: If you're getting a NullPointerException within prop. Load(in), it's possible that Project.class. GetResourceAsStream(line) is returning null.
You should check for that (and also close the input stream in a finally block). Are you sure label. Properties is actually present and available to the class loader?
How are you running this code? If you're using Eclipse or something similar, you may have forgotten to tell it that your label. Properties file is a resource which should be copied to the output directory.
Well here's one problem: while (br.readLine()! = null) { sb. Append(line); sb.
Append("\n"); line = br.readLine(); } That's going to skip every other line. Typically I'd use: while ((line = br.readLine())! = null) { sb.
Append(line); sb. Append("\n"); } Note that only the first line of your file is being used to load a properties file - the rest is just dumped to System.out. Also, the properties file you are loading is then being thrown away - you don't do anything with prop afterwards.(And you should close in in a finally block, assuming it's non-null.).
I did that as but still I am getting error as Exception in thread "main" java.lang. NullPointerException at java.util. Properties$LineReader.
ReadLine(Unknown Source) at java.util.Properties. Load0(Unknown Source) – Raihan Jamal Nov 21 at 6:03 But it should get loaded in loadProperties method right? As I am looping through the keySet?
– Raihan Jamal Nov 21 at 6:08 @RaihanJamal: See my edit - I've put it at the top of the answer now. Basically I believe it can't find your file. – Jon Skeet Nov 21 at 6:09 Got it.
I forgot to put the whole path where each properties file. So in my case each property file is in this directory. InputStream in = Project.class.
GetResourceAsStream("C:\\apps\\apache\\tomcat7\\webapps\\examples\\WEB-INF\\classes\\"+line); So it should be written this way? As if I write this way then still I am getting nullpointer exception – Raihan Jamal Nov 21 at 6:12.
Method br.readLine() is called twice in your code. While (line! = null) { loadProperties(line); sb.
Append(line); sb. Append("\n"); line = br.readLine(); }.
I did that as but still I am getting error as Exception in thread "main" java.lang. NullPointerException at java.util. Properties$LineReader.
ReadLine(Unknown Source) at java.util.Properties. Load0(Unknown Source) – Raihan Jamal Nov 21 at 6:02 @RaihanJamal - What is the location of . Properties files?
– AVD Nov 21 at 6:10 location is this- InputStream in = NiharikaProject.class. GetResourceAsStream("C:\\apps\\apache\\tomcat7\\webapps\\exampl? Es\\WEB-INF\\classes\\"+line); So it should be written this way or some other way?
– Raihan Jamal Nov 21 at 6:14 Move . Properties files into package folder - testing. Project (WEB-INF/classes/testing/project) – AVD Nov 21 at 6:17 Is it possible to load the file from external directory?
– Raihan Jamal Nov 21 at 6:20.
Once you fixed the bug mentioned above. Here is the soln to the problem. Enum PathType {RESOURCE, ABSOLUTE} private static void loadProperties(String line, PathType pathType) { Properties prop = new Properties(); InputStream in = null; if (pathType == PathType.
RESOURCE) in = PropertyTest.class. GetResourceAsStream(line); else { try { in = new FileInputStream(line); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } try { //As soon as it gets into prop.
Load(in), cursor goes to br. Close that is in main method. Prop.
Load(in); for (Object str : prop.keySet()) { Object value = prop. GetProperty((String) str); System.out. Println(str + " - " + value); } in.close(); } catch (IOException e) { e.printStackTrace(); } } Code to call it.
There are various ways. If you want to load properties from base of the class loader. For example, if I am running my application with main function from eclipse, my base will be /classes.So the file label.
Properties is residing there. Also if you are running from Tomcat and label. Properties lies in /classes.
Use below code: loadProperties("/" + line, PathType. RESOURCE); // Load from base path of class loader If you want to load properties from package folder of the class loader. For example, if I am running my application with main function from eclipse, my base will be /classes.
The file label. Properties is residing in /classes/testing/project. Also if you are running from Tomcat and label.
Properties lies in /classes/testing/project use below code: loadProperties(line, PathType. RESOURCE); // Load from base path of class loader + the package path If you want load properties from any absolute path on your hard drive. Use below code: loadProperties("C:\\apps\\apache\\tomcat7\\webapps\\examples\\WEB-INF\\classes\\" + line, PathType.
ABSOLUTE); NOTE: Pls handle the exception as per your need. I just updated your code AS IS.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.