I am writing a Jersey Restful service to be deployed on Tomcat via a war file.
The service needs to read data in 3 text files. The text files need to exist on the file system or read from the classpath. I have tried to read the data from the file system and classpath but neither are working. I would be happy with either way - it doesn't matter.
If it was to use the following code,can someone tell me where to place the text file specified so that the code picks up the file?
BufferedReader br = new BufferedReader(new InputStreamReader
(this.getClass().getClassLoader().getResourceAsStream("myfile.txt")));
I am getting a null pointer exception.
If I was to read the file from the file system, use the following code, where do I place the files in my Jar?
FileInputStream fs = new FileInputStream("myFile.txt");
DataInputStream is = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
I am getting a FileNotFound exception.
Any suggestions are welcome.
Use the ReadAllText method of the My. Computer. FileSystem object to read the contents of a text file into a string, supplying the path and file encoding type. The following example reads the contents of the UTF32 file test.
In html we defined input tag with file type for upload any text files and div tag defined with id 'res' for appends retrieved contents on webpage. In <script> we adding addEventListener event on input tag by id 'infl' using that we defining 'change' function, within that we created file reader object 'file_reader'.
I copied my text file into the WEB-INF/classes
folder and used this.getClass().getClassLoader().getResourceAsStream("/myfile.txt")
and it worked.
Thanks all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With