Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

urgent help require in applet problem

Posted on 2004-04-23
30
172 Views
Last Modified: 2013-12-29
i have an applet and that has to read text files that contain data, these text files are placed in the same directory where applet class file is placed
but when i write this code in applet class

new file("c:\application\data.txt " );

security exception is raised

and when i used

new file(getcodebase().tostring()+"data.txt " );

file notfound exception is raised

so what to do help

0
Comment
Question by:imranb35
  • 7
  • 6
  • 4
  • +4
30 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 85 total points
ID: 10901367
0
 
LVL 9

Assisted Solution

by:Nick_72
Nick_72 earned 83 total points
ID: 10901376
Here you would need double backslashes:

new File("c:\\application\\data.txt " );

try:

new File(getcodebase().tostring() + "\\data.txt " );
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10901386
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 83 total points
ID: 10901394
try here..ur answer is there..

http://forums.devshed.com/archive/t-40645

best of luck..

R.K.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10901410
And, of course, Nick_72's observation is correct regarding the file path.  :-)
0
 

Author Comment

by:imranb35
ID: 10901832
i  tried  rama_krishna580 link
that was

String host = getCodeBase().toHost();
StringBuffer buf = new StringBuffer();
try {
URL url = new URL("http://" + host + "/st.java");
InputStream in = url.openStream();
and got
conncetionrefused error
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10902014
rama_krishna580's link describes how the get resources out of a location inherent to and under the control of the web browser.

You have two options:
1. Make changes to your security policy file (will only work with JDK 1.2 and later)
2. Sign your applet.

Item 2 is really your only option, and it doesn't require jumping through hoops. There are plenty of links already posted. Your answer is in those links.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 83 total points
ID: 10903414
>>
read text files that contain data, these text files are placed in the same directory where applet class file is placed
>>

Then the following should work fine:

InputStream in = getClass().getResourceAsStream("/data.txt");
// now open a BufferedReader on it and read it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10903422
>>// now open a BufferedReader on it and read it

i.e.

BufferedReader br = new BufferedReader(new InputStreamReader(in));
0
 
LVL 92

Assisted Solution

by:objects
objects earned 83 total points
ID: 10905221
you need to also specify the package details, or instead use a relative resource name.
0
 
LVL 92

Expert Comment

by:objects
ID: 10905229
eg.

InputStream in = getClass().getResourceAsStream("/com/imranb35/data.txt");

or if being called from your Applet class:

InputStream in = getClass().getResourceAsStream("data.txt");
0
 

Author Comment

by:imranb35
ID: 10908146
thnkx cehj
 your analysis was superb i have fixed the problem its now reading data
could you plz tell me how could i write data from applet to "data.txt"
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 83 total points
ID: 10911260
Have you signed the Applet?

You acn write to a file this way: http://www.javaalmanac.com/egs/java.io/WriteToFile.html
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10911282
try this...

     try
     {
        BufferedWriter bufferOut = new BufferedWriter(new FileWriter("/outfilename.txt"));
        bufferOut .write("Your String goes here");
        bufferOut .close();
    } catch (IOException e) { }

best of luck..


R.K
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10912206
>>could you plz tell me how could i write data from applet to "data.txt"

Unfortunately you'll need to sign it in order to do that. If the applet is for your own use and not intended for distribution then you can simply alter the policy file of the plugin if you're using the Sun one:

http://www.mindprod.com/jgloss/signedapplets.html

0
 

Author Comment

by:imranb35
ID: 10912435
i dont want to write to client machine but to server's data file
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10912718
You can't do that generally without some mechanism, such as ftp, on the server. If it's on your lan, then that's different and you can probably use the unc name to write to it without signing
0
 
LVL 92

Expert Comment

by:objects
ID: 10914001
you can use a servlet or similiar technology to implement uploading files to your server.
0
 

Author Comment

by:imranb35
ID: 10915271
plz tell me the detail of using
unc name method
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10918211
Is said 'probably' as i'm not sure if it's going to work (i haven't tried it). Start as follows. If it's looking promising, i'll give you the rest:

URL url = new URL("file:/machinename/share_name/dir_name/filename.txt");
URLConnection conn = url.openConnection();
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10919309
CEHJ, how could the sandbox possibly allow the above call?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919901
Because dir_name is the applet's codebase (perhaps i should have stressed that)
0
 
LVL 92

Expert Comment

by:objects
ID: 10922963
If it is the applets codebase then you should *use* getCodebase() and not hardcode the details.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10922994
>>then you should *use* getCodebase()

Yes, that would be better. Have you got that imranb35? - what's being said is that if you want to write to your file, then the applet needs to have been loaded from a network share on your lan. The file that needs to be written should be in your applet's codebase.

I haven't tested this and can't at the moment, as i'm not on a Windows network
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 11163090
I believe the question has been answered and points should be distributed accordingly
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 11163424
I also believe points should be distributed accordingly....

R.K
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11678032
Sounds ok.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question