Solved

urgent help require in applet problem

Posted on 2004-04-23
30
175 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
[X]
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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

617 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