如何通过在输出应用程序中指定目录来删除文件

https://img4.mukewang.com/64dc9ca800010a3e04330523.jpg

如果我给出确切的文件夹位置,则会删除文件。但我想要得到的是它必须搜索包含子文件夹的目录,它还必须沿着子文件夹搜索并删除下拉列表中指定的指定文件(例如文件名将是 borerry_furn)

请帮助我提前致谢。


慕仙森
浏览 83回答 1
1回答

温温酱

它必须搜索包含子文件夹的目录,也必须沿着子文件夹搜索搜索目录的方法需要是递归的。以下是列出所有子目录中的文件的递归方法的示例: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&nbsp; &nbsp; implements ActionListener, Runnable{&nbsp; &nbsp; JTable table;&nbsp; &nbsp; DefaultTableModel model;&nbsp; &nbsp; JTextField path;&nbsp; &nbsp; JLabel currentFile;&nbsp; &nbsp; JButton getFiles;&nbsp; &nbsp; int totalFiles;&nbsp; &nbsp; int totalDirectories;&nbsp; &nbsp; public TableFile()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; path = new JTextField("C:\\java");&nbsp; &nbsp; &nbsp; &nbsp; add(path, BorderLayout.PAGE_START );&nbsp; &nbsp; &nbsp; &nbsp; getFiles = new JButton( "Get Files" );&nbsp; &nbsp; &nbsp; &nbsp; getFiles.addActionListener( this );&nbsp; &nbsp; &nbsp; &nbsp; add(getFiles, BorderLayout.LINE_START );&nbsp; &nbsp; &nbsp; &nbsp; String[] columnNames = {"IsFile", "Name"};&nbsp; &nbsp; &nbsp; &nbsp; model = new DefaultTableModel(columnNames, 0);&nbsp; &nbsp; &nbsp; &nbsp; table = new JTable( model );&nbsp; &nbsp; &nbsp; &nbsp; JScrollPane scrollPane = new JScrollPane( table );&nbsp; &nbsp; &nbsp; &nbsp; add(scrollPane, BorderLayout.PAGE_END);&nbsp; &nbsp; &nbsp; &nbsp; currentFile = new JLabel(" ");//&nbsp; &nbsp; &nbsp; add(currentFile, BorderLayout.PAGE_END); // displays filename in label&nbsp; &nbsp; }&nbsp; &nbsp; public void actionPerformed(ActionEvent e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; model.setNumRows(0);&nbsp; &nbsp; &nbsp; &nbsp; new Thread( this ).start();&nbsp; &nbsp; &nbsp; &nbsp; table.requestFocusInWindow();&nbsp; &nbsp; }&nbsp; &nbsp; public void run()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; totalFiles = 0;&nbsp; &nbsp; &nbsp; &nbsp; totalDirectories = 0;&nbsp; &nbsp; &nbsp; &nbsp; listFiles( new File( path.getText() ) );&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Directories: " + totalDirectories);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Files&nbsp; &nbsp; &nbsp; : " + totalFiles);&nbsp; &nbsp; }&nbsp; &nbsp; private void listFiles(File dir)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; updateTable( dir );&nbsp; &nbsp; &nbsp; &nbsp; totalDirectories++;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Processing directory: " + dir);&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; add a delay to demonstrate processing one directory at a time&nbsp; &nbsp; &nbsp; &nbsp; try { Thread.sleep(500); }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e) {}&nbsp; &nbsp; &nbsp; &nbsp; File[ ] entries = dir.listFiles( );&nbsp; &nbsp; &nbsp; &nbsp; int size = entries == null ? 0 : entries.length;&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < size; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (entries[j].isDirectory( ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listFiles( entries[j] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTable( entries[j] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentFile.setText( entries[j].toString() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalFiles++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void updateTable(final File file)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(new Runnable()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vector<Object> row = new Vector<Object>(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.addElement( new Boolean( file.isFile() ) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.addElement( file.toString() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addRow( row );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rowCount = table.getRowCount() - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.changeSelection(rowCount, rowCount, false, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; TableFile frame = new TableFile();&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation( EXIT_ON_CLOSE );&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo( null );&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java