2

I'm working on a Java project. Just one value is stored in a txt file in this location: (src/files/file.txt).

I export the project to jar file and I know that beacuase of the src folder, I need to use the following code to read the file.

InputStream stream = MainUi.class.getResourceAsStream("src/files/tax.txt");

But I have problem with writing to file in such a situation, the file data is needed to be changed. Please let me know how to solve it.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Mustafa Mohammadi
  • 1,433
  • 1
  • 12
  • 25

2 Answers2

1

I export the project to jar file and I know that because of the src folder, I need to use the following code to read the file.

It is not possible for your program (launched from an executable Jar) to write to the text file located inside same Jar file.

Please look at: How can an app use files inside the JAR for read and write?

Community
  • 1
  • 1
Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19
0

This directory is read-only, to change it:

  • Open explorer and go to your src (also files) folder
  • Right click, click on properties
  • Uncheck read-only
  • Launch your jar

One other solution: store your resources in another folder.

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • If I Uncheck read-only, is it possible that use like the simple File? I mean is it needed to use input stream? Thanks. – Mustafa Mohammadi Jul 03 '16 at 10:31
  • For any kind of file reading operation you need to have write permissions. And you should be able to write to this file if you disabled read-only. – Shiro Jul 03 '16 at 10:43