• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing data in Properties file and reading via HashMap  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I have a properties file that stores the state code as key and value as the state.
Eg.
MH = Maharashtra
KA=Karnataka
GA= Goa
And using HashMap i ask from the user input of code and then the program gives output as the state.
Here i have written the code for it. I know that the code has mistakes. I want the code to be corrected .
And while with this code i run the program,  I am getting error
java.io.FileNotFoundException: resources\code.properties (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.techlabs.codemapproperties.CodeMapProperties.<init>(CodeMapProperties.java:24)
at com.techlabs.codemapproperties.CodeMapProperties.main(CodeMapProperties.java:80)


 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are doing too much in one place. Start with a method which reads the key and value from the properties file and displays them on screen; once you have done that you can easily put them into the Map. Adding cities or similar belongs in a different method. Remember that methods called from the constructor shou‍ld always be tagged private or final (it is not necessary to use both).
I would also suggest you regard the Properties class as legacy code and don't use it. Use a plain simple Map implementation; what is wrong with HashMap?
The particular exception you are suffering seems to be to do with problems about the location of the file you are reading. Please confirm you are using the correct path to that file, and that the file allows reading.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ campbell. I have been given a task to do it with properties file.
I want to know the correct code. I am unable to figure out the code
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And i guess i am using properties file because there are too many codes
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.FileNotFoundException: resources\code.properties (The system cannot find the path specified)

This error simply means that the system cannot find the code.properties file on your computer.  Is the code.properties file in the correct folder?  Can your system find the resources folder?  How are you executing this code?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have codes and corresponding states to it.
1. I want to read from the properties file the data. It is not reading if i populate the data into the file. Hence i have to supply it externally using file writer. If i populate the data and just read it give error as -

      File Not Found Exception
       
2. I want the all the data when a letter in the key is asked.When i write M , i want all the data with Key starting from M.
Eg
        MH = Maharashtra
        MP = Madhya Pradesh
        ML= Meghalaya
        MN = Manipur
        MZ=Mizoram

Code i have so far is :
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working on something similar and This is a pretty helpful example
https://www.mkyong.com/java/java-properties-file-examples/

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question would appear to be a continuation of the previous question; since you shouldn't create two threads, I thought it better to merge the two threads.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:@ campbell. I have been given a task to do it with properties file.
I want to know the correct code. . . .
You already have a properties file. Have you been told to use a Properties object? If you have to use one, which I am not convinced is the best design, use it as a Map<Object, Object> rather than using its superclasses. But you won't get correct code by trying lots of things all together in one place. Start by being able to open the file; the exception is probably because you have given the wrong location for the file. What is the format for the file? The format you gave in the first post is inconsistent. Do you have spaces in it or not?
Anyway, the first thing I would do is find a way to open the file. Once you have opened the file, work out how to read it. Is the file a binary file or text? That shou‍ld how you read it: text Readers/Writers, binary file=InputStream/OutputStream. Reading the file and populating your map shou‍ld be the first stage of your application; then you can fill in the other parts.
As I said earlier, those parts belong in different methods.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prior to this line
add

Then look to see if your file is actually at that location.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, stop using the File class. Look in the Java™ Tutorials, particularly the first section after File I/O entitled “what is a Path?” Use the Path class and use the methods of Paths to obtain a Path. You can get a buffered reader for that Path from a Files class' method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!