-1

信息:

所以我做了一个简单的类来保存某个俄罗斯方块游戏的高分。

当我的整个游戏都以.JAR 形式保存时,我需要该文件可以访问,因此我需要将文件位置设置为外部。

所以现在我使用“c://”位置,因为我确定所有计算机都有。但我不确定所有计算机都有 LineHighscores.txt 和 TimeHighscores.txt 文件,因此需要创建它们。

问题:

每当我运行我的代码时,我都会收到一个错误:

  c:/TimeHighscores.txt
  c:/TimeHighscores.txt
  dec 19, 2012 2:26:35 AM tetris.FileIO loadHighscores
  SEVERE: null
  java.io.IOException: Toegang geweigerd
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:947)
at tetris.FileIO.loadHighscores(FileIO.java:75)
at tetris.FileIO.getTimeScores(FileIO.java:37)
at tetris.HighScores.<init>(HighScores.java:41)
at tetris.Menu$2.actionPerformed(Menu.java:75)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
            (BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
            (ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
            (ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
            (ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters
            (EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
            (EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

    java.io.FileNotFoundException: c:\TimeHighscores.txt 
   (Het systeem kan het    opgegeven bestand niet vinden)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tetris.FileIO.loadHighscores(FileIO.java:84)
at tetris.FileIO.getTimeScores(FileIO.java:37)
at tetris.HighScores.<init>(HighScores.java:41)
at tetris.Menu$2.actionPerformed(Menu.java:75)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
           (BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
              (ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
              (ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege
              (ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters
              (EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter
              (EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
               (EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

代码:

   public class FileIO   {
private File file;
private Scanner filescScanner, lineScanner;
private PrintWriter fileWriter;
private String[][] data;
static final String lineHighscoreTXT = "c://LineHighscores.txt", timeHighscoreTXT = "c://TimeHighscores.txt";


public FileIO () {
    data = new String[100][2];
}
public String[][] getLineScores(){
    System.out.println(lineHighscoreTXT);
    return this.loadHighscores(lineHighscoreTXT);
}
public String[][] getTimeScores(){
    System.out.println(timeHighscoreTXT);
    return this.loadHighscores(timeHighscoreTXT);
}

public void setLineScores( String name,String lines ){
    boolean found= false;
    data = this.getLineScores();
    for(int i = 0; i<data.length && !found ; i++){
        if(data[i][0] == null || "Niemand".equals(data[i][0])){
            data[i][0]=name;
            data[i][1]=lines;
            found=true;
        }

    }
    this.saveHighscores(lineHighscoreTXT,data);
}
public void setTimeScores(String time, String name){
    boolean found= false;
    data = this.getLineScores();
    for(int i = 0; i<data.length && !found ; i++){
        if(data[i][0] == null || "Niemand".equals(data[i][0])){
            data[i][0]=name;
            data[i][1]=time;
            found=true;
        }

    }
    this.saveHighscores(timeHighscoreTXT,data);
}

private String[][] loadHighscores( String fileLocation){

int x=0;
String test = "";
System.out.println(fileLocation.toString());
File file = new File(fileLocation);
if (!file.exists()){
    try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
        }
}
try{
    filescScanner = new Scanner(file);}
catch(Exception ioe){System.err.println(ioe);}

   if (filescScanner.hasNext()){


        while(filescScanner.hasNextLine()&& x<100) {

            lineScanner = new Scanner(filescScanner.nextLine());
            lineScanner.useDelimiter("-/-");


            data[x][0]=lineScanner.next();//name    
            data[x][1]=lineScanner.next();//data   
            x++;

        }
        lineScanner.close();
        filescScanner.close();
   }
   else{
       data[0][0] = "Niemand";
       data[0][1] = "0";
   }
    return data;

}
private void saveHighscores( String fileLocation, String[][] data){
    int x=0;
        file = new File(fileLocation);  
        if (!file.exists()){try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
        }
  }
    try {
        fileWriter = new PrintWriter(file);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
    }

    if(data.length>x){
        while(data.length>x && data[x][0] != null ){
            fileWriter.println(data[x][0]+"-/-"+data[x][1]);
            x++;
        }
        fileWriter.close();
    }
}



 public static void main(String[] args){
    FileIO file = new FileIO();
    file.setLineScores("55555555", "KoenKevin");      
}

 }
4

1 回答 1

2

我不懂荷兰语,但我假设这意味着“访问被拒绝”?

java.io.IOException:Toegang geweigerd

如果是这样,这可能是由于 Windows 出于安全目的限制文件写入 C:\ 造成的。

您必须写入其他位置,或以管理员身份运行您的应用程序

于 2012-12-19T01:52:43.040 回答