0

I am creating a project in java in which i am converting the project into jar file containing a text file along with it. But i googled and found out getResourceAsStream() has to be used to obtain the file which is embedded along with jar file for processing.So i done that and it was working fine for reading the contents of the file using scanner passing inputstream as parameter. But now my problem is how to write to a file got from an inputstream using printwriter and filewriter.

InputStream stream = Sample_Test.class.getResourceAsStream("/resources/Project_Names.txt")

So please guide me how to write a string the following file.

user1791442
  • 11
  • 1
  • 4
  • Do you want to write to the file packaged in the jar or to an external file? – Pouriya Zarbafian Sep 17 '15 at 09:02
  • 1
    Well, its not advisable. But, you can find an answer here -> http://stackoverflow.com/questions/2797367/write-to-a-file-stream-returned-from-getresourceasstream – Techidiot Sep 17 '15 at 09:03
  • http://stackoverflow.com/questions/13000937/read-and-write-to-java-file-via-resource Also check and answer here – Techidiot Sep 17 '15 at 09:06

2 Answers2

0

Jar is an archive, which is meant to be unchanged. If you need your jar (application) to write something, write to an external source. That's the reason api offers you only inputstream .

I suggest you make a separate folder and make your application point to that folder and do all your external activity there

Akash Yadav
  • 2,411
  • 20
  • 32
0

Because you text file is within jar file, you cannot change its content.

yelliver
  • 5,648
  • 5
  • 34
  • 65