如何将选定的文件选择器目录放入当前工作目录。我正在制作一个组织文件夹的项目,并在Youtube上找到了一个类似的项目,但它只运行在organizer.jar文件所在的位置 我想要做的是一个用户界面,它使用文件选择器直接选择文件夹目录,这样我就不会在任何想要组织文件夹的地方移动或复制organizer.jar文件。
以下是代码:
package my.GUI;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
/**
*
* @author atom
*/
public class GUI extends javax.swing.JFrame {
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
}
/**
* 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">
private void initComponents() {
jLayeredPane1 = new javax.swing.JLayeredPane();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
direct = new javax.swing.JTextField();
DirView = new javax.swing.JInternalFrame();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
exten = new javax.swing.JTextField();
folder = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(0, 0, 204), null), "Directory"));
jLabel1.setText("Select Directory:");
direct.setHorizontalAlignment(javax.swing.JTextField.CENTER);
direct.setText("<<<== Click Me ==>>>");
direct.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
directMouseClicked(evt);
}
});
DirView.setVisible(true);
javax.swing.GroupLayout DirViewLayout = new javax.swing.GroupLayout(DirView.getContentPane());
DirView.getContentPane().setLayout(DirViewLayout);
DirViewLayout.setHorizontalGroup(
DirViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 585, Short.MAX_VALUE)
);
DirViewLayout.setVerticalGroup(
DirViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 390, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(DirView)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(direct, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(direct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DirView)
.addContainerGap())
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(0, 0, 204), null), "Instruction"));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 149, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(0, 0, 153), null), "Organizer"));
jLabel2.setText("Please Enter Costum Extension:");
jLabel3.setText("Please Enter the Name of the Folder:");
exten.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
extenActionPerformed(evt);
}
});
jButton1.setText("Let's get Organized");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
final JFileChooser chooser = new JFileChooser();
private void directMouseClicked(java.awt.event.MouseEvent evt) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
direct.setText(chooser.getSelectedFile().getAbsolutePath());
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
File f;
f = new File(chooser.getSelectedFile().getAbsolutePath());
FileList fl = new FileList();
Component c2 = fl.getGui(f.listFiles(new TextFileFilter()),false);
JInternalFrame frame = DirView;
JPanel gui = new JPanel(new BorderLayout());
gui.add(c2,BorderLayout.CENTER);
c2.setPreferredSize(new Dimension(500,280));
gui.setBorder(new EmptyBorder(3,3,3,3));
frame.setContentPane(gui);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private void extenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
**String path = chooser.getSelectedFile().getAbsolutePath();**
**File file = new File(path);**
**String[] content = file.list();**
String tmp = "";
int index = 0;
String ex_user = exten.getText();
String name_user = folder.getText();
String[] extention = {
"jpg",
"jpeg",
"png",
"txt",
"pdf",
"doc",
"docx",
"ppt",
"pptx",
"accdb",
"xls",
"mdb",
"flv",
"mov",
"mp4",
"mpg",
"3gp",
"webm",
"exe",
"msi",
"rar",
"zip",
"7zip",
"tar",
"gz",
"mp3",
"wmv",
"txt",
"html" };
String[] Folder_name = {
"Images",
"Images",
"Images",
"Text Files",
"Books",
"MS Documents",
"MS Documents",
"MS Documents",
"MS Documents",
"MS Documents",
"MS Documents",
"MS Documents",
"Video Files",
"Video Files",
"Video Files",
"Video Files",
"Video Files",
"Video Files",
"Setup",
"Setup",
"Archives",
"Archives",
"Archives",
"Archives",
"Archives",
"Audio",
"Audio",
"Text Files",
"Web Pages" };
try
{
if (!ex_user.equals(""))
{
extention = new String[1];
extention[0] = ex_user;
Folder_name = new String[1];
Folder_name[0] = name_user;
}
}
catch (NullPointerException ex)
{
JOptionPane.showMessageDialog(null, "You Cancel the Operation");
System.exit(1);
}
String[] ex = unique(getEx(content));
for (int i = 0; i < ex.length; i++) {
if (Arrays.asList(extention).contains(ex[i]))
{
index = Arrays.asList(extention).indexOf(ex[i]);
tmp = createFolder(Folder_name[index]);
process(ex[i], tmp, content);
}
}
JOptionPane.showMessageDialog(null, "your files is now orgnized");
}
public static void process(String ex, String d, String[] content)
{
File tmp = new File("");
for (int i = 0; i < content.length; i++) {
if ((!tmp.isDirectory()) && (content[i].endsWith(ex)))
{
tmp = new File(content[i]);
move(tmp.getAbsolutePath(), genrate(tmp.getAbsolutePath()) + d + "\\" + content[i]);
}
}
}
public static void move(String from, String to)
{
Path From = Paths.get(from, new String[0]);
Path To = Paths.get(to, new String[0]);
try
{
Files.move(From,
To, new CopyOption[] { StandardCopyOption.ATOMIC_MOVE });
}
catch (Exception ex)
{
System.err.println(ex.getMessage());
}
}
public static String genrate(String path)
{
String[] x = path.split("\\\\");
String result = "";
for (int i = 0; i < x.length - 1; i++) {
result = result + x[i] + "\\";
}
return result;
}
public static String[] getEx(String[] a)
{
String tmp = "";
String tmp2 = "";
String[] ex = new String[a.length];
for (int i = 0; i < a.length; i++)
{
for (int j = a[i].length() - 1; j >= 0; j--) {
tmp = tmp + a[i].charAt(j);
}
try
{
tmp = tmp.substring(0, tmp.indexOf('.'));
}
catch (Exception ee)
{
tmp = tmp.substring(0, 3);
}
for (int j = tmp.length() - 1; j >= 0; j--) {
tmp2 = tmp2 + tmp.charAt(j);
}
ex[i] = tmp2;
tmp2 = "";
tmp = "";
}
return ex;
}
public static String[] unique(String[] x)
{
String a = "";
for (int i = 0; i < x.length; i++) {
if (!a.contains(x[i])) {
a = a + x[i] + " ";
}
}
return a.split(" ");
}
public static String createFolder(String name)
{
File d = new File(name);
d.mkdir();
return name;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
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)
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JInternalFrame DirView;
private javax.swing.JTextField direct;
private javax.swing.JTextField exten;
private javax.swing.JTextField folder;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
这是我在netbeans中运行时的输出:
C:\Users\atom\Desktop\HomeMFS\28_days_later.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\28_days_later.zip
C:\Users\atom\Desktop\HomeMFS\47777.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\47777.zip
C:\Users\atom\Desktop\HomeMFS\apache-tomcat-8.0.36-windows-x64.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\apache-tomcat-8.0.36-windows-x64.zip
C:\Users\atom\Desktop\HomeMFS\apache-tomcat-9.0.0.M8-windows-x64.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\apache-tomcat-9.0.0.M8-windows-x64.zip
C:\Users\atom\Desktop\HomeMFS\Common_1.0.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Common_1.0.zip
C:\Users\atom\Desktop\HomeMFS\DocumentExplorer_src.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\DocumentExplorer_src.zip
C:\Users\atom\Desktop\HomeMFS\ds_digital.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\ds_digital.zip
C:\Users\atom\Desktop\HomeMFS\DuplicateFinder_src.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\DuplicateFinder_src.zip
C:\Users\atom\Desktop\HomeMFS\fast-md5-2.7.1.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\fast-md5-2.7.1.zip
C:\Users\atom\Desktop\HomeMFS\FIUI_Tuyen_HNQ.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\FIUI_Tuyen_HNQ.zip
C:\Users\atom\Desktop\HomeMFS\Game of Thrones_6x05_HDTV.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game of Thrones_6x05_HDTV.en.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E01.720p.HDTV.SVA.en_1.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E01.720p.HDTV.SVA.en_1.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E02.720p.Web-DL.NTb.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E02.720p.Web-DL.NTb.en.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E03.720p.HDTV.AVS.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E03.720p.HDTV.AVS.en.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E04.720p.HDTV.AVS.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E04.720p.HDTV.AVS.en.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E05.1080p.HDTV.BATV.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E05.1080p.HDTV.BATV.en.zip
C:\Users\atom\Desktop\HomeMFS\Game.of.Thrones.S06E05.WEBRip.en.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Game.of.Thrones.S06E05.WEBRip.en.zip
C:\Users\atom\Desktop\HomeMFS\GroupDocs.Viewer-for-Java-master.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\GroupDocs.Viewer-for-Java-master.zip
C:\Users\atom\Desktop\HomeMFS\javaplanner-v1.5-gpl (1).zip -> C:\Users\atom\Desktop\HomeMFS\Archives\javaplanner-v1.5-gpl (1).zip
C:\Users\atom\Desktop\HomeMFS\KK PATCH BY @AKSHAYGALAXYSTARMOD.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\KK PATCH BY @AKSHAYGALAXYSTARMOD.zip
C:\Users\atom\Desktop\HomeMFS\oc4j_extended_101350.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\oc4j_extended_101350.zip
C:\Users\atom\Desktop\HomeMFS\pa_gapps-stock-4.4.4-20150410-signed.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\pa_gapps-stock-4.4.4-20150410-signed.zip
C:\Users\atom\Desktop\HomeMFS\Reminder.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\Reminder.zip
C:\Users\atom\Desktop\HomeMFS\tut.zip -> C:\Users\atom\Desktop\HomeMFS\Archives\tut.zip
C:\Users\atom\Desktop\HomeMFS\ce2.23.tar -> C:\Users\atom\Desktop\HomeMFS\Archives\ce2.23.tar
C:\Users\atom\Desktop\HomeMFS\ce2.23.tar.gz -> C:\Users\atom\Desktop\HomeMFS\Archives\ce2.23.tar.gz
C:\Users\atom\Desktop\HomeMFS\File-Orgnizer-Version-2.5.rar -> C:\Users\atom\Desktop\HomeMFS\Archives\File-Orgnizer-Version-2.5.rar
C:\Users\atom\Desktop\HomeMFS\HomeMFS.rar -> C:\Users\atom\Desktop\HomeMFS\Archives\HomeMFS.rar
C:\Users\atom\Desktop\HomeMFS\rufus-2.10p.exe -> C:\Users\atom\Desktop\HomeMFS\Setup\rufus-2.10p.exe
C:\Users\atom\Desktop\HomeMFS\SOFTWARE PROTOTYPING.ppt -> C:\Users\atom\Desktop\HomeMFS\MS Documents\SOFTWARE PROTOTYPING.ppt
BUILD SUCCESSFUL (total time: 19 seconds)
我想组织下载文件夹,但它在项目文件夹所在的桌面上运行。
答案 0 :(得分:0)
我明白了。我只是将.getAbsoluteFile()添加到我想要的文件中,它可以工作......