I have a text file in my resource folder and I need to be able to read/remove/add text. I believe I have to use append so I tried this.
Files.write(Paths.get(Testing.class.getResource("/testresources/SearchList.txt").getPath())
, "the text".getBytes(), StandardOpenOption.APPEND);
This gives me back
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /C:/Users/Ben/workspace/Eve/bin/org/me/Testing/resources/SearchList.txt
If anyone could should me some clarity on this subject that would be great. Thankyou!
This worked for me fine:
You should add the text file to a raw folder. If the raw folder does not exist in your resource directory, create one. And then in order to access it and write or edit it, use this:
OutputStream outputStream=getResources().openRawResource(R.raw.YourTextFile);
then in order to Read from the file, you should do this
InputStream inputStream=getResources().openRawResource(R.raw.YourTextFile);
A leading slash is being automatically added to your path - /C:/Users/Ben/workspace/Eve/bin/org/me/Testing/resources/SearchList.txt
Lots of solutions have been suggested here in answers, Java NIO file path issue
Use any of those mentioned answers as your solution.
Also, for files intended to be in jar, You need to use Paths
method public static Path get(URI uri)
instead of public static Path get(String first,String... more)
See How to get a path to a resource in a Java JAR file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.