如何使用JFileChooser在表中显示具有任意名称的csv文件,以读取和编辑该文件

crcmnpdw  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(199)

我制作了一个程序来帮助我处理一些电子表格,它工作得很好,但我希望能够使用JFileChooser选择任何csv文件,并能够编辑所选的文件。我这样做的方式,我总是强迫文件有一个特定的名称,我不想这样。我该怎么做?我做了一些研究,但没有成功。谢谢

enter code here
//my list, write, remove and update file    
File fileName = new File("file.csv"); 

@Override
public ArrayList<Data> list() throws Exception {        
    try{            
        ArrayList<Data> listData = new ArrayList<>();
        FileReader fr = new FileReader(fileName);
        try ( 
                BufferedReader br = new BufferedReader(fr)) {
                String line;
                while((line=br.readLine())!=null){
                Data objData = new Data(line);
                listData.add(objData);
            }br.close();
        }
        return listData;
    }catch(IOException erro){
    throw erro;
    }
}

@Override
public void add(Data objData) throws Exception {
    try{
       FileWriter fw = new FileWriter(fileName,true);
        try ( 
            BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(objData.toString()+"\n");
        }
    }catch(IOException erro){
        throw erro;
    }
}

@Override
public void remove(int code) throws Exception{
    ArrayList<Data> list;
    list = list();
    if(list.isEmpty()) return;
    FileWriter fw = new FileWriter(fileName);
    try ( 
            BufferedWriter bw = new BufferedWriter(fw)) {
        for(Data p : list){
            if(p.getCode() != code){
                bw.write(p.toString()+"\n");
            }
        }
    }
}    

@Override
public void update(Data objData) throws Exception {
    try{
        ArrayList<Data> list;
        list = list();
        if(list.isEmpty()) return;
        FileWriter fw = new FileWriter(fileName);
        try ( 
                BufferedWriter bw = new BufferedWriter(fw)) {
            for(Data p : list){
                if(p.getCode() != objData.getCode()){
                    bw.write(p.toString()+"\n");
                }else{
                    bw.write(objData.toString()+"\n");
                }
            }
        }
   }catch(Exception erro){
        throw erro;
    }
}

enter code here
//my UI table list    
private void showData(){
    try{
        ArrayList<Data> list;
        DataDAO Data = new DataDAO();            
        list = Data.list();
        if (list.isEmpty()) return;
        DefaultTableModel model = (DefaultTableModel) jTable_Table1.getModel();
        model.setNumRows(0);
        for(int pos=0; pos < list.size(); pos++){
            String[] line = new String[6];
            Data aux = list.get(pos);
            line[0] = aux.getCode()+"";
            line[1] = aux.getName();
            line[2] = aux.getPrice()+"";
            line[3] = aux.getCargoN()+"";
            line[4] = aux.getTotal_Weight()+"";
            line[5] = aux.getTotal_Vol()+"";
            model.addRow(line);
        }
    }catch (Exception erro){
        JOptionPane.showMessageDialog(rootPane, erro.getMessage());
    }
}

// MY button jfilechooser    
private void open_file() throws Exception{
    try{  
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "Only csv", "csv");
        chooser.setFileFilter(filter);
        chooser.setCurrentDirectory(new File("./"));
        int result = chooser.showOpenDialog(getParent());
        if (result == JFileChooser.APPROVE_OPTION) 
        {                
            File selectedFile = chooser.getSelectedFile();
            String file = selectedFile.getAbsolutePath();
            System.out.println(file);
            showData();            
        }            
    }catch(HeadlessException erro){
        JOptionPane.showMessageDialog(rootPane, erro);
    } 
}
rta7y2nd

rta7y2nd1#

简单地说,像。。。

private JFileChooser fileChooser;

public File getFile(Component parent) {
    if (fileChooser == null) {
        fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Make a choice");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("csv", "CSV");
        fileChooser.addChoosableFileFilter(filter);
    }
    int option = fileChooser.showOpenDialog(parent);
    if (option == JFileChooser.APPROVE_OPTION) {
        return fileChooser.getSelectedFile();
    }
    return null;
}

其大部分由How to Use File Choosers覆盖
您可以将其添加到现有类中,或者根据需要创建实用程序类。
您将需要更改工作流,以具有某种“打开文件”步骤,该步骤将调用该步骤并返回“选定文件”,确保您考虑到null值的可能性。然后将该值分配给File fileName;,这将允许其他工作流继续运行。
如何做到这一点的“方法”取决于实现,您必须与自己的方法一起使用,以找出最佳的使用方法

相关问题