I need to pass the file path to a function of an external class which uses it in the following way:
byte[] keyBytes = Files.readAllBytes((new File(pathname)).toPath());
The class cannot be edited otherwise I would have used this method to read the file ->
InputStream inputStream = getClass().getResourceAsStream("/file_name.xyz");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
When running the code locally, it's able to retrieve the file but when the JAR is deployed on cloud it's failing to find it.
Currently I have pasted the file at src/main/resources but I can shift it to any other folder also. Just need to pass the path so that the function reads it.
Can someone help me with this? I'm stuck on it from couple of days
Related
I am trying to read a .txt file in java.
I placed the file in the root folder of the project.
When I do this:
URL url = getClass().getResource("/test.txt");
System.out.println(url);
File file = new File(url.getPath());
System.out.println(file.getAbsolutePath());
I get the correct path back.
But when i want to use the File in a FileReader it can't find the file.
Scanner scan = new Scanner(new BufferedReader(new FileReader(file)));
Even when I place a test file on my desktop and use a absolut path the FileReader can't find the file.
I don't know what to do, I have tried a lot of stuff.
Can someone help me.
Resources (Class.getResource) cannot generally be dealt with File, they are files on the class path, possibly packed in a .jar file. You can get a reader as follows:
new InputStreamReader(getClass().getResourceAsStream("/test.txt"), StandardCharsets.UTF_8)
The above uses an InputStream of the resource. As you know the Charset of the file, specify it for a Reader.
That it worked was a working directory issue in combination with your IDE's settings.
There are two things to try here:
Use the full path instead of using a relative path use the full path e.g. something like "/Users/BlueDragon709/Desktop/test.txt" instead of "/test.txt"
If that fails check the file permissions.
When you are currently using File you aren't attempting to access it until you instantiate the Scanner so it not going to fail until you hit that line of code.
I currently have a folder of schema files in SQL that get executed when the application is executed. This is what I use to read and execute these files:
private static String getSchemaFromFile(String filename) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/schema/" + filename + ".sql"));
String queryString;
.....
}
The problem I think is to do with the path of the schema folder. I tried looking at getResourceAsStream but I can't seem to get it working.
It works fine when I run from eclipse but when I compile it into a JAR it says file not found. How do I ensure the path is correct?
Something along these lines. BTW this will probably fail unless you are running your jar (executing it in eclipse probably won't work).
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("src/schema/" + filename + ".sql")));
From eclipse it works because eclipse enable java code to search from src/schema/ directory.But when you are using jar it requires an absolute path.
I will suggest store the absolute path where you place your .sql file in a property file so that you can change whenever you need it.Suppose you from property file you have get the location absolutePath. Then you can do this -
BufferedReader bufferedReader = new BufferedReader(new FileReader(absolutePath + filename + ".sql"));
If the schema is in a jar file, then src/schema is probably not valid, more likely it will be in schema/. First you need to confirm this. The jar file is actually a zip file, so you can unzip it with utility like winzip to examine the contents.
Once confirmed where the schema files are located, they cannot be accessed using FileReader, as they are not in the file system but inside your jar. To access a file from within the jar file, getResourceAsStream is the correct approach. You probably defined the wrong folder when you tried it last time.
I am using BufferedReader and BufferedWriter in a progect in order to export it as Jar and use it,
My code using the streams is the following
br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/data/score.scr")));
bw= new BufferedWriter(new FileWriter("src/data/score.scr"));
I tried to use getClass().getResourceAsStream(); inside BufferedWriter but it doest work, the program works well but it seems ... that the file read is not the one written ... I save a score (BufferedWriter) when I get to the scorepanel (BufferedReader) it is not there
all this in order to export my project as a JAR so I need to modify the second line of my core
In general you cannot write inside a jar file but here is a more detailed answer How can my Java program store files inside of its .jar file?
You could try using your IDE to do this.
eclipse
or through command line
jar cf jar-file input-file(s)
From what it looks like your doing, you don't necessarily need to store data in the jar itself. Just write the data to the same directory or folder as your jar and have your user transfer the entire folder as part of your program.
I am developing an android app using code from a normal java application.
In this java application i am parsing an XML file which i get like this:
File xmlFile = new File("../project/src/resources/words.xml");
Now in android this doesn't seem to work. I get a file not found exception.
I tried to save the file in the res/xml directory but I'm not sure how to get to it.
Can i use the same code to parse the XML as I used for my java application or is there a special way to parse XML files in android?
What is ../project/src/resources.words.xml? Is that a pathname in your project directory on your development machine? Of course an Android program is not going to have access to file paths defined on your machine.
If words.xml is a static file that you'd like access to, you should include it in the /res/raw subdirectory of your project. Then you can access it using the methods described in the documentation here:
http://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)
Or, you can put it in /assets and use this method:
http://developer.android.com/reference/android/content/res/AssetManager.html
You can do:
InputStream is = this.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
... then read the content of the file as a stream ...
Your file must be placed in res/raw/yourfile and resourceId is an integer in R.raw... corresponding to your filename (R.raw.yourfile)
I have a small java program that reads a file in, in eclipse i have the file in the main project dir and the class file is within the src dir. This works fine.
I want to you this small piece of code within a web project im working on, currently i have the class file in src/tools/, but im lost on where to put the file?
I have tried it in a few places yet it throws file not found.
Where is the best place to store this file? and how can i ensure i have the right path when using the following code?
FileInputStream fstream = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
You either need to load the file as a resource (e.g., getResourceAsStream(), use a path relative to the app (getRealPath()), or put it in an absolute location and use a full path.
If you put the resource file in the root of your classpath (so if your source path for java files is src/main/java then put the resource file in there) then you can do this.
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("myresource.txt")));
Now if you want you can put it in a package, so the file would be in src/main/java/com/sksamuel
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("com/sksamuel/myresource.txt")));
Finally, if you're using maven or something similar, then you would put the file into src/main/resources and not src/main/java, and at compile time the two paths are combined, so you would use the same code as above.
My answer assumes the file will be packaged up with your java code and is something that would change by a developer. If it's generated, or needs to be changed outside the jar/war then this isn't the best way.