5

Tôi đang gặp sự cố khi thao tác này hoạt động. Nó có trong một chuỗi bao gồm nhiều mẩu thông tin được đặt lại với nhau. Tuy nhiên, khi tôi cố gắng viết String vào một tập tin để theo dõi những thay đổi trong chương trình theo thời gian, tôi nhận được một báo Access is denied lỗi:Quyền truy cập bị từ chối khi sử dụng FileOutputStream

void writeToFile(String input) throws Exception{ 
      File file = new File("C:\\WeatherExports\\export.txt"); 
      if(!file.exists()){ 
        file.createNewFile(); 
      } 
      BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true)); 
      try{ 
        inFile.append(input); 
        inFile.newLine(); 
      } catch(Exception e){ 
        e.printStackTrace(); 
      } 
      inFile.close(); 
    } 

stacktrace yeilds:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied) 

Full stacktrace:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied) 
at java.io.FileOutputStream.openAppend(Native Method) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileWriter.<init>(Unknown Source) 
at org.weatheralert.InfoManipMethods.writeToFile(InfoManipMethods.java:58) 
at org.weatheralert.Form.actionPerformed(Form.java:108) 
at javax.swing.JTextField.fireActionPerformed(Unknown Source) 
at javax.swing.JTextField.postActionEvent(Unknown Source) 
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source) 
at javax.swing.SwingUtilities.notifyAction(Unknown Source) 
at javax.swing.JComponent.processKeyBinding(Unknown Source) 
at javax.swing.JComponent.processKeyBindings(Unknown Source) 
at javax.swing.JComponent.processKeyEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$000(Unknown Source) 
at java.awt.EventQueue$1.run(Unknown Source) 
at java.awt.EventQueue$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

Dòng 58:

BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true)); 
+1

Người dùng bạn đang chạy chương trình có quyền tạo và ghi tệp vào thư mục đó không? –

+0

Đó là tài khoản của riêng tôi là quản trị viên trên máy tính của tôi. Tôi đã tắt UAC cho chính tôi và một người dùng khác trên máy tính. –

+0

Tôi có thể tạo thành công tệp nếu tôi không thêm thư mục và đặt tệp trực tiếp vào đĩa C: –

Trả lời

10

Bạn phải tạo các thư mục trước. Nhưng bạn không thể gọi file.mkdirs() - bạn cần gọi file.getParentFile() .mkdirs() - nếu không, bạn sẽ tạo một thư mục với tên của tệp (sau đó sẽ ngăn bạn tạo tệp cùng tên).

Tôi cũng sẽ đề cập rằng bạn nên kiểm tra mã kết quả của mkdirs(), chỉ trong trường hợp không thành công.

Và mặc dù bạn không yêu cầu nó, tôi vẫn đề cập đến việc bạn không cần phải gọi createNewFile() (FileWriter của bạn sẽ tạo ra nó).

và chỉ dành riêng cho triệt để, hãy chắc chắn để đưa file.close của bạn() trong một khối finally, và ném ngoại lệ của bạn (không chỉ cần in) - ở đây bạn đi:

void writeToFile(String input) throws IOException{ 
      File file = new File("C:\\WeatherExports\\export.txt"); 
      if (!file.getParentFile().mkdirs()) 
        throw new IOException("Unable to create " + file.getParentFile()); 
      BufferedWriter out = new BufferedWriter(new FileWriter(file,true)); 
      try{ 
        out.append(input); 
        out.newLine(); 
      } finally { 
        out.close(); 
      } 
    } 
6

Có một khả năng khác (chỉ dành cho bất kỳ ai có thể đọc được điều này sau sự thật). Tôi đã có cùng một vấn đề, nhưng tất cả các thư mục cha đã tồn tại. Vấn đề hóa ra là có một thư mục có cùng tên với tệp tôi đang cố tạo.

+0

Nice! Câu trả lời chính xác! Đã dành cho tôi rất nhiều thời gian. – ryvantage

Các vấn đề liên quan