|
|
Java Applets - Access denied to file when applet is started | ||
Discussion by abhatia923 with 2 Replies.
Last Update: July 18, 2010, 11:07 am | |||
I am getting access denied to file when execute(compiled successfully) the following applet
Please help:
import java.applet.*;
import java.awt.*;
public class counterApplet extends Applet{
public static int i=0;
public static String no="";
public static String pre="You Are Visitor Number: ";
public static String p="";
public void init(){
FileInputStream file=null;
try {file = new FileInputStream("counter");} catch(FileNotFoundException e) {System.exit(1);}
//load value and increment it
try{for(i=0;i!=10;) {i=(file.read());if(i==-1) {break;} else{no=no+(char)i;}}
i = Integer.parseInt(no)+1;}catch(Exception e){System.exit(1);}
//write new value
no=""+i;
BufferedOutputStream bu = null;
try {
bu= new BufferedOutputStream(new FileOutputStream("counter"));
bu.write(no.getBytes());}catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}
finally {
try {
if (bu != null) {
bu.flush();
bu.close();}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void start(){}
public void paint(Graphics g){
p=pre+no;
g.drawString(p,20,30);}
}
Please help:
CODE
import java.io.*;import java.applet.*;
import java.awt.*;
public class counterApplet extends Applet{
public static int i=0;
public static String no="";
public static String pre="You Are Visitor Number: ";
public static String p="";
public void init(){
FileInputStream file=null;
try {file = new FileInputStream("counter");} catch(FileNotFoundException e) {System.exit(1);}
//load value and increment it
try{for(i=0;i!=10;) {i=(file.read());if(i==-1) {break;} else{no=no+(char)i;}}
i = Integer.parseInt(no)+1;}catch(Exception e){System.exit(1);}
//write new value
no=""+i;
BufferedOutputStream bu = null;
try {
bu= new BufferedOutputStream(new FileOutputStream("counter"));
bu.write(no.getBytes());}catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}
finally {
try {
if (bu != null) {
bu.flush();
bu.close();}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void start(){}
public void paint(Graphics g){
p=pre+no;
g.drawString(p,20,30);}
}
Fri Jul 16, 2010 Reply New Discussion
If you think about where applets execute, you will find that a counter makes no sense.
Sun Jul 18, 2010 Reply New Discussion
Processing (0)
|
Index




