2015-04-03 64 views
1

當我對system.out進行編組時。我的列表在控制檯中出現沒有問題。當我嘗試將其發送到文件時,出現錯誤提示當使用JAXB進行編組時,接收FileNotFoundException(訪問被拒絕)

javax.xml.bind.JAXBException 
- with linked exception: 
[java.io.FileNotFoundException: c:\computerparts.xml (Access is denied)] 
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) 
at com.cooksys.assessment.List$2.doSaveCommand(List.java:201) 
at com.cooksys.assessment.List$2.actionPerformed(List.java:179).....ETC 

在我的源中我沒有收到任何錯誤。下面是我保存(馬歇爾)方法

JMenuItem mntmSave = new JMenuItem("Save"); 
     mntmSave.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       try { 
        doSaveCommand(); 
       } catch (JAXBException e1) { 
        e1.printStackTrace(); 
       } 

      } 

      private void doSaveCommand() throws JAXBException { 
       ArrayList<String> save = new ArrayList<>(); 
       for (int i = 0; i < destination.size(); i++) { 
        save.add((String)destination.getElementAt(i)); 
       } 
       ListWrapper Assembledparts = new ListWrapper(); 
       Assembledparts.setAssembledparts (save); 

       File file = new File ("c:\\computerparts.xml"); 

       JAXBContext context = JAXBContext.newInstance(ListWrapper.class); 
       Marshaller marshaller = context.createMarshaller(); 
       marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
       marshaller.marshal(Assembledparts, System.out); 

       marshaller.marshal(Assembledparts, file); 
      } 
     }); 

我希望如果我能得到我的列表在控制檯中我應該能夠調集到一個文件中出現。我錯過了什麼?下面是我的getter setter方法類

import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 

public class ListWrapper { 
@XmlElement(name = "part") 
private List<String> save; 

public List<String> save() { 
    if (save == null) { 
     save = new ArrayList<String>(); 
    } 
    return this.save; 
} 

public void setAssembledparts (ArrayList<String> save) { 
    this.save = save; 

    } 

如果需要的主要代碼如下

import javax.swing.*; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

import java.awt.*; 
import java.awt.event.*; 
import java.io.File; 
import java.util.ArrayList; 

public class List implements ActionListener{ 

    JList destinationList, sourceList; 
    JButton buttonin, buttonout; 

    DefaultListModel source; 
    static DefaultListModel destination; 

    public JPanel createContentPane(){ 

     JPanel totalGUI = new JPanel(); 

     source = new DefaultListModel(); 
     destination = new DefaultListModel(); 

     String shoppingItems[] = {"Case", "Motherboard", "CPU", "RAM", "GPU", 
     "HDD", "PSU"}; 

     for(int i = 0; i < shoppingItems.length; i++) 
     { 
      source.addElement(shoppingItems[i]); 
     } 

     destinationList = new JList(source); 
     destinationList.setVisibleRowCount(10); 
     destinationList.setFixedCellHeight(20); 
     destinationList.setFixedCellWidth(140); 
     destinationList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

     JScrollPane list1 = new JScrollPane(destinationList); 

     sourceList = new JList(destination); 
     sourceList.setVisibleRowCount(10); 
     sourceList.setFixedCellHeight(20); 
     sourceList.setFixedCellWidth(140); 
     sourceList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

     JScrollPane list2 = new JScrollPane(sourceList); 

     JPanel buttonPanel = new JPanel(); 

     buttonin = new JButton(">>"); 
     buttonin.setHorizontalAlignment(SwingConstants.RIGHT); 
     buttonin.addActionListener(this); 
     buttonPanel.add(buttonin); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS)); 

     bottomPanel.add(Box.createRigidArea(new Dimension(10,0))); 
     bottomPanel.add(list1); 
     bottomPanel.add(Box.createRigidArea(new Dimension(5,0))); 
     bottomPanel.add(buttonPanel); 

       buttonout = new JButton("<<"); 
       buttonout.addActionListener(this); 
       buttonPanel.add(buttonout); 
     bottomPanel.add(Box.createRigidArea(new Dimension(5,0))); 
     bottomPanel.add(list2); 
     bottomPanel.add(Box.createRigidArea(new Dimension(10,0))); 

     totalGUI.add(bottomPanel); 
     totalGUI.setOpaque(true); 
     return totalGUI; 
    } 

    private JPanel createSquareJPanel(Color color, int size) { 
     JPanel tempPanel = new JPanel(); 
     tempPanel.setBackground(color); 
     tempPanel.setMinimumSize(new Dimension(size, size)); 
     tempPanel.setMaximumSize(new Dimension(size, size)); 
     tempPanel.setPreferredSize(new Dimension(size, size)); 
     return tempPanel; 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     int i = 0; 


     if(e.getSource() == buttonin) 
     { 
      int[] fromindex = destinationList.getSelectedIndices(); 
      Object[] from = destinationList.getSelectedValues(); 

      for(i = 0; i < from.length; i++) 
      { 
       destination.addElement(from[i]); 
      } 

      for(i = (fromindex.length-1); i >=0; i--) 
      { 
       source.remove(fromindex[i]); 
      } 
     } 

     else if(e.getSource() == buttonout) 
     { 
      Object[] to = sourceList.getSelectedValues(); 
      int[] toindex = sourceList.getSelectedIndices(); 

      for(i = 0; i < to.length; i++) 
      { 
       source.addElement(to[i]); 
      } 

      for(i = (toindex.length-1); i >=0; i--) 
      { 
       destination.remove(toindex[i]); 
      } 
     } 
    } 



    private static void createAndShowGUI() { 

     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("PC Parts Builder"); 
     JMenu file = new JMenu ("File"); 

     List demo = new List(); 
     frame.setContentPane(demo.createContentPane()); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 

     JMenuBar menuBar = new JMenuBar(); 
     frame.setJMenuBar(menuBar); 

     JMenu mnFile = new JMenu("File"); 
     menuBar.add(mnFile); 

     JMenuItem item; 
     file.add(item = new JMenuItem("Load")); 
     item.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
       doOpenCommand(); 
       } catch (JAXBException e1) { 
        e1.printStackTrace(); 
       } 

      } 

      private void doOpenCommand() throws JAXBException{ 

       File file = new File ("c:\\computerparts.xml"); 

       JAXBContext context = JAXBContext.newInstance(ListWrapper.class); 
       Unmarshaller unmarshal = context.createUnmarshaller(); 
       ListWrapper assembledparts = (ListWrapper) unmarshal.unmarshal(file); 
       System.out.println(assembledparts); 
      } 
     }); 

     mnFile.add(item); 


     JMenuItem mntmSave = new JMenuItem("Save"); 
     mntmSave.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       try { 
        doSaveCommand(); 
       } catch (JAXBException e1) { 
        e1.printStackTrace(); 
       } 

      } 

      private void doSaveCommand() throws JAXBException { 
       ArrayList<String> save = new ArrayList<>(); 
       for (int i = 0; i < destination.size(); i++) { 
        save.add((String)destination.getElementAt(i)); 
       } 
       ListWrapper Assembledparts = new ListWrapper(); 
       Assembledparts.setAssembledparts (save); 

       File file = new File ("c:\\computerparts.xml"); 

       JAXBContext context = JAXBContext.newInstance(ListWrapper.class); 
       Marshaller marshaller = context.createMarshaller(); 
       marshaller.setProperty  (Marshaller.JAXB_FORMATTED_OUTPUT, true); 
       marshaller.marshal(Assembledparts, System.out); 

       marshaller.marshal(Assembledparts, file); 
      } 



     }); 
     mnFile.add(mntmSave); 

     JMenuItem mntmNewMenuItem = new JMenuItem("Exit"); 
     mntmNewMenuItem.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     }); 
     mnFile.add(mntmNewMenuItem); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

您是否可以在C:驅動器中創建一個文件,錯誤消息中明確指出「訪問被拒絕」您能否請檢查 – 2015-04-03 20:21:58

+0

我發佈此消息後大概一分鐘內我就發現了它。感謝您的迴應 – 2015-04-03 20:39:48

回答

1

[java.io.FileNotFoundException:C:\ computerparts.xml(訪問被拒絕)]

試以避免c:並將源文件放在另一個文件夾中,即。 C:/temp/FileName.xml

0

這是要注意行: [java.io.FileNotFoundException: c:\computerparts.xml (Access is denied)]

要麼computerparts.xml不存在,或者你沒有寫它的權限。將該文件放在其他位置,並更新行File file = new File ("c:\\computerparts.xml");以指向新的文件位置。