Solved

urgent help require in applet problem

Posted on 2004-04-23
30
169 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 67
ForLoop Example 3 48
table example 4 29
login form jsp example 2 25
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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

777 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