亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過在輸出應用程序中指定目錄來刪除文件

如何通過在輸出應用程序中指定目錄來刪除文件

慕仙森 2023-08-16 17:52:03
如果我給出確切的文件夾位置,則會刪除文件。但我想要得到的是它必須搜索包含子文件夾的目錄,它還必須沿著子文件夾搜索并刪除下拉列表中指定的指定文件(例如文件名將是 borerry_furn)請幫助我提前致謝。
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

它必須搜索包含子文件夾的目錄,也必須沿著子文件夾搜索

搜索目錄的方法需要是遞歸的。

以下是列出所有子目錄中的文件的遞歸方法的示例:

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;


public class TableFile extends JFrame

    implements ActionListener, Runnable

{

    JTable table;

    DefaultTableModel model;

    JTextField path;

    JLabel currentFile;

    JButton getFiles;

    int totalFiles;

    int totalDirectories;


    public TableFile()

    {

        path = new JTextField("C:\\java");

        add(path, BorderLayout.PAGE_START );


        getFiles = new JButton( "Get Files" );

        getFiles.addActionListener( this );

        add(getFiles, BorderLayout.LINE_START );


        String[] columnNames = {"IsFile", "Name"};


        model = new DefaultTableModel(columnNames, 0);

        table = new JTable( model );


        JScrollPane scrollPane = new JScrollPane( table );

        add(scrollPane, BorderLayout.PAGE_END);


        currentFile = new JLabel(" ");

//      add(currentFile, BorderLayout.PAGE_END); // displays filename in label

    }


    public void actionPerformed(ActionEvent e)

    {

        model.setNumRows(0);


        new Thread( this ).start();


        table.requestFocusInWindow();

    }


    public void run()

    {

        totalFiles = 0;

        totalDirectories = 0;

        listFiles( new File( path.getText() ) );

        System.out.println("Directories: " + totalDirectories);

        System.out.println("Files      : " + totalFiles);

    }


    private void listFiles(File dir)

    {

        updateTable( dir );

        totalDirectories++;

        System.out.println("Processing directory: " + dir);


        //  add a delay to demonstrate processing one directory at a time


        try { Thread.sleep(500); }

        catch(Exception e) {}


        File[ ] entries = dir.listFiles( );

        int size = entries == null ? 0 : entries.length;


        for(int j = 0; j < size; j++)

        {

            if (entries[j].isDirectory( ))

            {

                listFiles( entries[j] );

            }

            else

            {

                updateTable( entries[j] );

                currentFile.setText( entries[j].toString() );

                totalFiles++;

            }

        }

    }


    private void updateTable(final File file)

    {

        SwingUtilities.invokeLater(new Runnable()

        {

            public void run()

            {

                Vector<Object> row = new Vector<Object>(2);

                row.addElement( new Boolean( file.isFile() ) );

                row.addElement( file.toString() );

                model.addRow( row );

                int rowCount = table.getRowCount() - 1;

                table.changeSelection(rowCount, rowCount, false, false);

            }

        });

    }


    public static void main(String[] args)

    {

        TableFile frame = new TableFile();

        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

        frame.pack();

        frame.setLocationRelativeTo( null );

        frame.setVisible(true);

    }

}


查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號