拒绝访问?JAVA
我正在为我在学校的java课程制作一个游戏,需要写入一个文件。我从我以前编写的一个工作程序中复制了一些代码,但每当我运行它时,就会出现一个拒绝访问错误。我的java老师说我对该文件有完全的读写权限,但它仍然指出了错误 文件Hiscore.txt确实存在,拼写与此完全相同 错误在方法MakeOutputFile中,特别是printwriter的初始化位置。它也出现在CheckOutputFile中,但该方法当前已被注释掉 我是这个网站的新手,所以我不太确定如何输入代码,所以结果会很差拒绝访问?JAVA,java,file-io,applet,file-permissions,accesscontrolexception,Java,File Io,Applet,File Permissions,Accesscontrolexception,我正在为我在学校的java课程制作一个游戏,需要写入一个文件。我从我以前编写的一个工作程序中复制了一些代码,但每当我运行它时,就会出现一个拒绝访问错误。我的java老师说我对该文件有完全的读写权限,但它仍然指出了错误 文件Hiscore.txt确实存在,拼写与此完全相同 错误在方法MakeOutputFile中,特别是printwriter的初始化位置。它也出现在CheckOutputFile中,但该方法当前已被注释掉 我是这个网站的新手,所以我不太确定如何输入代码,所以结果会很差 Except
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException:
access denied ("java.io.FilePermission" "Hiscore.txt" "write")
代码
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
导入java.io.File;
导入java.io.PrintWriter;
导入java.io.IOException;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类TowerDefense扩展JApplet实现MouseListener{
int XPO,YPO,金钱,攻击产卵,攻击计数,生命,攻击阿尔特,他的核心,
激光;//当前方块的x,y位置
private DrawingArea canvas;//要在其上绘制网格的面板画布
专用int[][]编号;
私人int[][]损害;
JPanel startscreen;
文件ifile、ofile、qfile;
扫描仪输入、输出、问题阅读器;
布尔开始、播放、检查核心、文件存在、结束、inquestionstate;
长期损害;
定时器;
布尔[][]移动;
按钮;
公共文件;
PrintWriter makesOutput;
//按钮开始;
//建造师
公共塔式围栏(){
inquestionstate=false;
JButton begin=新JButton(“begin”);
//开始=新的按钮(“开始”);
checkhiscore=false;
xpos=ypos=attackcount=laser=4;
按钮=新的JButton(“双倍伤害持续15秒”);
货币=200;
attackspawn=0;
总损伤=0;
寿命=10;
附着高度=24;
fileexists=true;
数字=新整数[30][30];
损坏=新整型[20][20];
移动=新布尔值[20][20];
定时器;
结束=假;
对于(int i=0;i<20;i++){
对于(int j=0;j<20;j++){
移动[i][j]=假;
损伤[i][j]=0;
}
}
对于(int i=0;i),不允许不受信任的小程序读取/写入客户端计算机上的文件对象
替代方案是:
将信息存储在服务器上
对小程序进行数字签名,并在要求用户信任它时让用户确认它
部署一个插件2JRE,并使用JNLPAPI服务来协调文件写入
我不认为很多人会想通过你的整个程序来寻找问题的方法。考虑把你的问题孤立到问题部分——这样的巨大代码块会驱使很多有帮助的人,请删除每第二个(空白)。行,并对代码块使用一致的逻辑缩进。作为新手,提示:捕获异常并打印堆栈跟踪。在其中找到最后一个代码(类和行号)这是在异常之前执行的,请告诉我们它是哪一个。请将您的问题简化为a。您希望我们通读您的所有代码吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TowerDefence extends JApplet implements MouseListener {
int xpos, ypos, money, attackspawn, attackcount, lives, attackerhealth, hiscore,
laser; // x,y location of current square
private DrawingArea canvas; // JPanel canvas on which to draw grid
private int[][] numbers;
private int[][] damage;
JPanel startscreen;
File ifile, ofile, qfile;
Scanner input, output, questionreader;
boolean start, play, checkhiscore, fileexists, end, inquestionstate;
long totaldamage;
Timer timer;
boolean [][] moved;
JButton button;
public File fargle;
PrintWriter makesOutput;
//JButton begin;
// Constructor
public TowerDefence( ) {
inquestionstate = false;
JButton begin = new JButton ("BEGIN");
//begin = new JButton("BEGIN");
checkhiscore = false;
xpos = ypos = attackcount = laser = 4;
button = new JButton("Double Damage for 15 seconds");
money = 200;
attackspawn = 0;
totaldamage = 0;
lives = 10;
attackerhealth = 24;
fileexists = true;
numbers = new int[30][30];
damage = new int [20][20];
moved = new boolean [20][20];
Timer timer;
end = false;
for (int i = 0; i< 20; i++){
for (int j = 0; j< 20; j++){
moved[i][j] = false;
damage [i][j] = 0;
}
}
for (int i = 0; i<30; i++){
for (int j = 0; j<30; j++){
numbers[i][j] = 0;
}
}
start = false;
play = false;
ifile = new File("map.txt");
ofile = new File ("hiscore.txt");
qfile = new File("questions.txt");
RepaintAction action = new RepaintAction();
timer = new Timer(1000, action);
timer.start();
}
private class RepaintAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
repaint(); // Call the repaint() method in the panel class.
}
}// MANDATORY: required initialization of JApplet
// MANDATORY: required initialization of JApplet
public void init ( ) {
canvas = new DrawingArea(); // create a new Drawing Area
setContentPane(canvas); // connect the canvas to JApplet
canvas.setBackground(Color.lightGray); // make the background lightGray
canvas.addMouseListener(this);
canvas.requestFocus(); // focus keyboard on JApplet'
}
// MANDATORY: Define DrawingArea as a JPanel
class DrawingArea extends JPanel {
JButton begin = new JButton ("BEGIN");
JButton upgrade = new JButton ("upgrade");
JComboBox selectmap = new JComboBox();
String op1, op2 = "yodel";
String askquestion;
String option = "ocean";
String findplace = "";
JRadioButton option1, option2;
JLabel question;
ButtonGroup group = new ButtonGroup();
JFrame questionframe = new JFrame();
public void paintComponent(Graphics g) {
super.paintComponent(g); // MANDATORY: Must be first method called.
if (!play&&!end){
ReadIt1();
StartScreen(g);
StartButton();
}
if (start){
ReadIt();
}
if (play){
ChangeAttackers();
drawGrid(g);
Attack(g);
DamageCheck();
if (attackspawn % 15 == 0){
SpawnAttackers();
ReadQuestions();
}
LifeCheck(g);
}
// CheckOutputFile();
//if (fileexists){
MakeOutputFile();
//}
if (end){
LifeCheck(g);
}
}
public void ReadQuestions(){
if(!inquestionstate){
try{
questionreader = new Scanner(qfile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
while (!findplace.equalsIgnoreCase(op2)){
findplace = questionreader.nextLine();
}
askquestion = questionreader.nextLine();
op1 = questionreader.nextLine();
op2 = questionreader.nextLine();
System.out.println(askquestion);
System.out.println(op1);
System.out.println(op2);
question = new JLabel(askquestion);
option1 = new JRadioButton(op1);
option2 = new JRadioButton(op2);
questionframe.setLayout(new GridLayout(0,1));
questionframe.removeAll();
questionframe.getContentPane().add(question);
questionframe.getContentPane().add(option1);
questionframe.getContentPane().add(option2);
questionframe.setVisible(true);
inquestionstate = true;
}
}
public void ReadIt1(){
if (option.equalsIgnoreCase("ocean")){
ifile = new File("oceanmap.txt");
}
else if (option.equalsIgnoreCase("canyon")){
ifile = new File("map.txt");
}
try{
input = new Scanner(ifile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
for (int i = 0; i<20; i++){
for (int j = 0; j < 20; j++){
numbers [j+5][i+5] = input.nextInt();
}
}
input.close();
}//end method ReadIt
public void StartScreen(Graphics g){
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("canyon")){
g.setColor(Color.green);
}
else if (option.equalsIgnoreCase("canyon")){
g.setColor(Color.black);
}
else if (option.equalsIgnoreCase("ocean")&&numbers[row+5][col+5] == 0){
g.setColor(Color.yellow);
}
else if (option.equalsIgnoreCase("ocean")){
g.setColor(Color.cyan);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setFont( new Font ("Serif", Font.BOLD, 48));
g.setColor(Color.red);
g.drawString("TOWER DEFENCE", 10,100);
}
}
}
public void StartButton(){
selectmap.addItem("Ocean");
selectmap.addItem("Canyon");
begin.setVisible(true);
begin.addActionListener(new ButtonListener());
this.setLayout(null);
this.add(begin);
begin.setBounds(0,468,500,32);
selectmap.setVisible(true);
this.add(selectmap);
selectmap.setBounds(300,300, 100,25);
}
// Draw the numbers on the grid
// Draw the Sudoku grid of rectangles
public void LifeCheck(Graphics g){
if(!inquestionstate){
JButton upgrade = new JButton ("upgrade");
upgrade.addActionListener(new ButtonListener());
for (int i = 0; i<20; i++){
for (int j = 19; j<20; j++){
if (numbers [i+5][j+5] == 2){
lives--;
numbers[i+5][j+5] = 1;
}
}
}
if (lives <=0){
play = false;
end = true;
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.green);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.black);
}
else if (numbers [row+5][col+5] == 2){
g.setColor(Color.red);
}
else if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.yellow);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.cyan);
}
else if (numbers [row+5][col+5] == 3){
g.setColor(Color.gray);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setColor(Color.black);
g.drawString("Money: "+money, 420,480);
g.drawString("Lives: "+lives, 360,480);
g.drawString("Laser: "+laser, 240,480);
System.out.print(damage[row][col]);
}
}
this.setLayout(null);
this.add(upgrade);
upgrade.setBounds(0, 475, 100, 25);
Font myFont = new Font ("Arial", Font.BOLD, 25);
g.setFont(myFont);
if (option.equalsIgnoreCase("Ocean")){
g.setColor(Color.red);
}
else if (option.equalsIgnoreCase("Canyon")){
g.setColor(Color.cyan);
}
g.drawString("You have lost. You lasted for " + attackspawn + "seconds.",10,100);
}
}
}
public void SpawnAttackers(){
if(!inquestionstate){
int currentspawn = 0;
attackcount++;
attackerhealth+=3;
if (attackerhealth % 10 == 0){
attackerhealth += (int)(attackerhealth*1.1);
}
if (attackerhealth % 17 == 0){
attackerhealth += (int)(attackerhealth*1.2);
}
if (attackerhealth % 12 == 0){
attackerhealth += (int)(attackerhealth*1.3);
}
if (attackerhealth % 2 == 0){
attackerhealth += 1+(int)(attackerhealth * 1.05);
}
for (int i = 0; i < 20; i++){
for (int j = 0; j< 20; j++){
if (numbers [j+5][i+5] == 1){
numbers [j+5][i+5] = 2;
currentspawn++;
if (currentspawn == attackcount){
i = j = 100;
}
}
}
}
}
}
public void drawGrid(Graphics g) {
if(!inquestionstate){
money++;
attackspawn++;
}
upgrade.addActionListener(new ButtonListener());
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.green);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Canyon")){
g.setColor(Color.black);
}
else if (numbers[row+5][col+5] == 0&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.yellow);
}
else if (numbers [row+5][col+5] == 1&&option.equalsIgnoreCase("Ocean")){
g.setColor(Color.cyan);
}
else if (numbers [row+5][col+5] == 2){
g.setColor(Color.red);
}
else if (numbers [row+5][col+5] == 3){
g.setColor(Color.gray);
}
g.fillRect(30 + col *22, 30 + row * 22, 21, 21);
g.setColor(Color.black);
g.drawString("Money: "+money, 420,480);
g.drawString("Lives: "+lives, 360,480);
g.drawString("Laser: "+laser, 240,480);
g.drawString("Wave: " + (int)(attackspawn/15), 170,480);
g.drawString("Total Damage: " + totaldamage, 170,20);
System.out.print(damage[row][col]);
}
System.out.println();
}
this.setLayout(null);
this.add(upgrade);
upgrade.setBounds(0, 475, 100, 25);
System.out.println();
}
public void ReadIt(){
if(!inquestionstate){
if (option.equalsIgnoreCase("ocean")){
ifile = new File("oceanmap.txt");
}
else if (option.equalsIgnoreCase("canyon")){
ifile = new File("map.txt");
}
try{
input = new Scanner(ifile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file map.txt");
System.exit(1);
}
for (int i = 0; i<20; i++){
for (int j = 0; j < 20; j++){
numbers [j+5][i+5] = input.nextInt();
}
}
start = false;
play = true;
input.close();
}
}//end method ReadIt
public void DamageCheck(){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (damage[i][j] >=attackerhealth){
numbers[i+5][j+5] = 1;
damage[i][j] = 0;
money+=25;
}
}
}
}
}
public void Attack(Graphics g){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (numbers [i+5][j+5] == 3){
for (int o = -3; o<4; o++){
for (int e = -3; e<4; e++){
if (numbers [i+o+5][j+e+5] == 2){
damage [i+o][j+e] +=laser;
totaldamage+=laser;
g.setColor(Color.white);
g.drawLine(40+j*22, 40+i*22, 40+(j+e)*22, 40+(i+o)*22);
}
}
}
}
}
}
}
}
public void ChangeAttackers(){
if(!inquestionstate){
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
moved[i][j] = false;
}
}
for (int i = 0; i<20; i++){
for (int j = 0; j<20; j++){
if (numbers[i+5][j+5] == 2){
if (numbers[i+5][j+6] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+5][j+6] = 2;
damage[i][j+1] = damage[i][j];
damage[i][j] = 0;
moved [i][j+1] = true;
System.out.println("Move Right");
}
else if ((i>13)||(i<9&&i>=7)){
if (numbers[i+4][j+5] == 1&&moved [i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+4][j+5] = 2;
moved [i-1][j] = true;
damage[i-1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Up");
}
else if (numbers [i+6][j+5] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+6][j+5] = 2;
moved [i+1][j] = true;
damage[i+1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Down");
}
}
else if ((i<=13&&i>=10)||(i<7)){
if (numbers [i+6][j+5] == 1&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+6][j+5] = 2;
moved [i+1][j] = true;
damage[i+1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Down");
}
else if (numbers[i+4][j+5] == 1&&moved [i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+4][j+5] = 2;
moved [i-1][j] = true;
damage[i-1][j] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Up");
}
}
else if (j!=0){
if (numbers[i+5][j+4] == 1&&j!=0&&moved[i][j] == false){
numbers [i+5][j+5] = 1;
numbers [i+5][j+4] = 2;
moved[i][j-1] = true;
damage[i][j-1] = damage[i][j];
damage[i][j] = 0;
System.out.println("Move Left");
}
}
}
}
}
}
}
public void CheckOutputFile(){
int number;
try{
output = new Scanner(ofile);
}catch(FileNotFoundException e){
System.out.println("ERROR: cannot open file hiscore.txt");
fileexists = true;
// System.exit(1);
}
if (fileexists){
hiscore = output.nextInt();
checkhiscore = true;
output.close();
}
}
public void MakeOutputFile ( ) {
fargle = new File ("Hiscore.txt");
try {
makesOutput = new PrintWriter ( fargle );
}catch ( IOException e ){
System.out.println("Cannot create file to be written to");
System.exit(1);
}// end catch
makesOutput.println("\nHiscore :\t" + attackspawn);
makesOutput.println ( );
System.exit(1);
}// end method MakeOutput File
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
System.out.println("RAWR");
if (evt.getSource() == begin){
System.out.println("Button Pressed");
begin.setVisible(false);
selectmap.setVisible(false);
play = true;
start = true;
option = (String)selectmap.getSelectedItem();
System.out.println(option);
}
if (evt.getSource() == upgrade){
if (money>=50){
money+=-50;
laser+=1+(int)(laser*.1);
}
}
}
}
}
public void mousePressed(MouseEvent m){
int x = m.getX();
int y = m.getY();
for (int row = 0; row < 20; row ++){
for (int col = 0; col < 20; col ++){
if (x>30+col*22&&x<51+col*22&&y>30+row*22&&y<51+row*22){
if (numbers [row+5][col+5] == 1&&money>200){
numbers [row+5][col+5] = 0;
money+=-200;
repaint();
}
else if (numbers [row+5][col+5] == 0&&money>50){
System.out.println(row+" " +col);
money+=-50;
numbers [row+5][col+5] = 3;
repaint();
}
}
}
}
}
public void mouseClicked(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m){}
}