Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package filesscanner; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import static java.lang.System.out; import java.nio.file.FileSystems; import java.nio.file.PathMatcher; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import org.json.simple.JSONObject; /** * * @author zabr1 */ public class MainWindow extends javax.swing.JFrame { /** * Creates new form MainWindow */ public MainWindow() { initComponents(); modelDirectories = new DefaultListModel<>(); modelFiles = new DefaultListModel<>(); jDirectoriesList.setModel(modelDirectories); jFileList.setModel(modelFiles); setTitle("ULTIMATE FILE UPLOAD"); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { scanBtn = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jFileList = new javax.swing.JList(); scanBtn1 = new javax.swing.JButton(); loadFileToServerBtn = new javax.swing.JButton(); saveToJSONBtn = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jDirectoriesList = new javax.swing.JList(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); scanBtn.setText("Add directory"); scanBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { scanBtnActionPerformed(evt); } }); jFileList.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(jFileList); scanBtn1.setText("Remove Directory"); scanBtn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { scanBtn1ActionPerformed(evt); } }); loadFileToServerBtn.setText("Search"); loadFileToServerBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadFileToServerBtnActionPerformed(evt); } }); saveToJSONBtn.setText(" JSON"); saveToJSONBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveToJSONBtnActionPerformed(evt); } }); jDirectoriesList.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane2.setViewportView(jDirectoriesList); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 611, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(scanBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(loadFileToServerBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43) .addComponent(saveToJSONBtn)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(42, 42, 42) .addComponent(scanBtn1))) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE) .addComponent(jScrollPane2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(scanBtn).addComponent(scanBtn1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(loadFileToServerBtn).addComponent(saveToJSONBtn)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void scanBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanBtnActionPerformed File f = new File("C:"); JFileChooser chooser = ShowChooser(f); int res = chooser.showDialog(this, " "); if (res == JFileChooser.APPROVE_OPTION) { File[] files = chooser.getSelectedFiles(); int key = 0; for (File file : files) { modelDirectories.add(key++, chooser.getSelectedFile().toString()); } } }//GEN-LAST:event_scanBtnActionPerformed private JFileChooser ShowChooser(File f) { JFileChooser chooser = new JFileChooser(f); chooser.setMultiSelectionEnabled(true); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); return chooser; } private void scanBtn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanBtn1ActionPerformed if (jFileList.isSelectionEmpty()) { JOptionPane.showMessageDialog(rootPane, "Nothing selected!"); } else { int index = jFileList.getSelectedIndex(); modelDirectories.remove(index); jFileList.setModel(modelFiles); jDirectoriesList.setModel(modelDirectories); } }//GEN-LAST:event_scanBtn1ActionPerformed private void loadFileToServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadFileToServerBtnActionPerformed findMusicFiles(); }//GEN-LAST:event_loadFileToServerBtnActionPerformed private void saveToJSONBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToJSONBtnActionPerformed JSONObject object = new JSONObject(); for (int i = 0; i < modelFiles.getSize(); i++) { object.put("filename" + i, modelFiles.get(i).toString()); } File file = new File("C:"); JFileChooser chooser = ShowChooser(file); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogType(JFileChooser.SAVE_DIALOG); int res = chooser.showDialog(this, " ? ??!!!"); if (res == JFileChooser.APPROVE_OPTION) { try { Writer writer = new FileWriter(chooser.getSelectedFile()); object.writeJSONString(writer); JOptionPane.showMessageDialog(rootPane, "SAVED SUCCESFULLY!!!"); writer.flush(); writer.close(); } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(rootPane, "Something bad!!!"); } } }//GEN-LAST:event_saveToJSONBtnActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainWindow().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JList jDirectoriesList; private javax.swing.JList jFileList; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JButton loadFileToServerBtn; private javax.swing.JButton saveToJSONBtn; private javax.swing.JButton scanBtn; private javax.swing.JButton scanBtn1; // End of variables declaration//GEN-END:variables private DefaultListModel<String> modelFiles; private DefaultListModel<String> modelDirectories; private void findMusicFiles() { fileFinder(); } private void fileFinder() { for (int i = 0; i < modelDirectories.getSize(); i++) { String path = modelDirectories.get(i); File file = new File(path); File[] files = file.listFiles(); if (file.isDirectory()) { checkDirectory(files); } } System.out.println("===================== DONE ======================"); } private void checkDirectory(File[] files) { int i = 0; for (File file1 : files) { if (file1.isDirectory()) { checkDirectory(file1.listFiles()); } if (file1.toString().contains("mp3")) { System.out.println(file1); modelFiles.add(i, file1.toString()); i++; } } } }