Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

How to read a text file from a Restful web service

  • 12-06-2012 10:21am
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    Hi all,

    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.


Comments

  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    First block of code assumes that file.txt is in the WEB-INF/classes folder of your war or it will throw a NPE

    Second block of code will look for the file in the current working directory of the java process. You would need to give it an absolute path for it to work if you wanted to take that approach in a war.


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    lynchie wrote: »
    First block of code assumes that file.txt is in the WEB-INF/classes folder of your war or it will throw a NPE.

    Thanks. Yes you are right. I used my build.xml to copy my text file into the classes folder and it worked.


Advertisement