0

I created these 2 applications i.e JavaApplet and WebApplication and integrated them using the following link :

Integrating an Applet in Web Application

The Applet was running reading the text file correctly which was located at "src/new1.txt" before integration.
But when this Java Applet project library was integrated(as shown in the NETBEANS forum link) in WebApplication project(jsp)...then it was unable to read the file but the applet was visible with no error(but was not showing results of file read).

So, where the file should be kept. I would like to mention that I only want this WebApplication project as it's part of the currently running website.
The whole thing is working correctly but after integration it stopped reading the file at "src/new1.txt".
I can't keep the file at another location, it should be under NETBEANS PROJECT location and should read the file even after integration.

John Snow
  • 977
  • 7
  • 12
Aman Chawla
  • 304
  • 3
  • 8
  • 23

1 Answers1

1

I personally prefer to store text files in a separate folder under web pages folder(like web pages/TextFiles/new1.txt).
But for reading the file stored in project ,it is not the matter where your store it but how you reading.Reading as normal file or as a resource.?
How are you reading your src/new1.txt file. If you are going to store it in your project, remember that you then need to access it as a resource i.e.,

     getClass().getResource() .

With the file stored in your project, you can't just create a 'File' object the normal way.
For more details visit :How to really read text file from classpath in Java
update
You need to replace this code

 File file = new File(yourclassname.class.getClassLoader().getResource("src\\data.txt").toURI());
 BufferedReader bf2 = new BufferedReader(new FileReader(file));  

with statemnet

BufferedReader bf2 = new BufferedReader(new FileReader("src\\data.txt")); 

Note: replace yourclassname with orginal class of yours.

Community
  • 1
  • 1
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • Thanks for your suggestions, now I should read text file using getClass().getResource(). I was previously using this: String x = "src\\data.txt"; Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\Notepad.exe " + x); and for reading file it was BufferedReader bf2 = new BufferedReader(new FileReader("src\\data.txt")); Now, Please suggest me how should I change it now. – Aman Chawla Aug 05 '13 at 05:09
  • @Aman Chawla I updated answer please look into it.I hope it help you.Thank you – Prabhaker A Aug 05 '13 at 06:56
  • thanks for this, it is showing error at class name, I've entered the correct name....is it an error as I'm using Swing – Aman Chawla Aug 05 '13 at 08:53
  • @Aman Chawla You done correct thing.You need to enter you defined class name in place 'yourclassname'.for example my class name is `TestDemo` then it should be `TestDemo.class.getClassLoader().getResource("src\\data.txt").toURI()` – Prabhaker A Aug 05 '13 at 09:09
  • no no, I've done what you've said. I've entered the class name correctly, mine was AppletClass1 – Aman Chawla Aug 05 '13 at 10:58