0

I have a question: How can I change/edit text file placed in class folder which is added to build path in my java project.

I'm working with Apache Nutch and in my gui app user must be able to edit regex-urlfilter.txt file (add or delete filter). It is palaced in nutchConf directory which was added to Build Path.

I'll be grateful for any answer.

EDIT: And I want to get it to work after exporting to jar and run from terminal.

jpelczar
  • 128
  • 1
  • 9
  • What seems to be a problem? – PM 77-1 May 31 '14 at 20:05
  • 1
    when i export project to jar file - nutchConf/regex-urlfilter.txt doesn't work. It's my problem. No it isn't duplicate because I know how to reda this file but i don't know what to edit and save it. – jpelczar May 31 '14 at 20:10
  • What do you mean by "*does not work*"? – PM 77-1 May 31 '14 at 20:12
  • App throw a exception that file doesn't exist. – jpelczar May 31 '14 at 20:17
  • OK. See your point now. You may want to look at answers to [Modifying a file inside a jar](http://stackoverflow.com/questions/1224817/modifying-a-file-inside-a-jar) question. Do not get totally discouraged by the accepted answer. – PM 77-1 May 31 '14 at 20:17
  • Post the relevant part of your code, the entire exception and stack trace. – PM 77-1 May 31 '14 at 20:18
  • I think that i write other application to edit txt file outside jar file and next update jar (jar uf jar-file file). Thanks @PM 77-1 for usefull link. – jpelczar May 31 '14 at 20:56

1 Answers1

0

This was actually a big problem for me, and I always wondered why there weren't more tutorials to do this.

You need to use this method: class.getResourceAsStream("/path/here"); to get an InputStream to your file.

So you can do this:

InputStream is = getClass().getResourceAsStream("/path/here");
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

Edit:

Oh. So you want to EDIT the file in the Jar? Well, it is possible(I believe) by using a zip library to get into the Jar. But honestly this is a lot more difficult than it should be. In my opinion, you should just save the config file to a predetermined location (ex. "C:\Program Files (x86)\appnamehere\config.txt" or something) and just access that path.

Kyranstar
  • 1,650
  • 2
  • 14
  • 35