2012-12-19 109 views
-1

信息:

所以我做了一個簡單的類,節省了特定的俄羅斯方塊遊戲的高分。java文件IO運行時錯誤

我需要該文件是可訪問的,當我的整個遊戲被激活爲.JAR時,所以我需要使文件位置外部。

所以現在我使用「c://」loctaion cuase我確定所有電腦都有。但我不確定所有電腦都有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");  
} 

} 
+0

1.您不應該在C:/的根目錄中創建文件。請在用戶的主目錄中創建它(例如/ Users/)。 2.(in'loadHighScores()')爲什麼即使創建掃描器失敗,您仍然繼續掃描數據? –

回答

2

我不明白荷蘭人,但我假設這個手段「拒絕訪問」 ?

產生java.io.IOException:Toegang geweigerd

如果是這樣,這可以由Windows限制文件的寫爲C引起的:\出於安全目的。

您必須寫入其他位置,或以管理員身份運行您的應用程序

+0

+1學習荷蘭語。當我讀到時,我失去了它。您的正確,只需將其轉換爲http://translation.babylon.com/dutch/to-english/ –

+0

@Kevin Bowersox,gerrytan是正確的。我最近做了一個應用程序,它刪除了空目錄,但由於用戶權限,我無法將路徑設置爲C:\並清理整個驅動器。您需要以管理員身份運行該程序。 瑣事:對於C:\驅動器中沒有管理員權限的用戶,僅禁用RW選項。你仍然可以遍歷目錄。 –