Solved

urgent help require in applet problem

Posted on 2004-04-23
30
164 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
Comment Utility
0
 
LVL 9

Assisted Solution

by:Nick_72
Nick_72 earned 83 total points
Comment Utility
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
Comment Utility
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 83 total points
Comment Utility
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
Comment Utility
And, of course, Nick_72's observation is correct regarding the file path.  :-)
0
 

Author Comment

by:imranb35
Comment Utility
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
Comment Utility
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
Comment Utility
>>
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
Comment Utility
>>// 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
Comment Utility
you need to also specify the package details, or instead use a relative resource name.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 83 total points
Comment Utility
Have you signed the Applet?

You acn write to a file this way: http://www.javaalmanac.com/egs/java.io/WriteToFile.html
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
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
Comment Utility
>>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
Comment Utility
i dont want to write to client machine but to server's data file
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
you can use a servlet or similiar technology to implement uploading files to your server.
0
 

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
CEHJ, how could the sandbox possibly allow the above call?
0
 
LVL 86

Expert Comment

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

Expert Comment

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

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
I believe the question has been answered and points should be distributed accordingly
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
I also believe points should be distributed accordingly....

R.K
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Sounds ok.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now