package File;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
//构造函数实现具体文件选择,获取文件所在路径
public class FileChooser {
String Str = new String();
String Str1 = new String();
String Str2 = new String();
public String actionPerformed() {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showDialog(new JLabel(), "选择");
File file = jfc.getSelectedFile();
if (file==null){
return null;
}
if (file.isDirectory()) {
Str1 = file.getAbsolutePath();
} else if (file.isFile()) {
Str1 = file.getAbsolutePath();
}
Str = jfc.getSelectedFile().getName(); //System.out.println("文件名:"+Str);
String[] S = Str1.split("\\\\");//因为字符串里的\\ 到了split里,需要分别转义,那么自然就需要用\\ \\来表示了,也就是\\\\
int j = S.length;
for (int i = 0; i < S.length - 1; i++) {
Str2 += S[i] + "/";
}
Str2 += S[j - 1];
System.out.println("处理结果:" + Str2);
return Str2;
}
private boolean isDirectory() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private String getAbsolutePath() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private boolean isFile() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
//构造函数实现具体文件选择,获取文件所在路径
public class FileChooser {
String Str = new String();
String Str1 = new String();
String Str2 = new String();
public String actionPerformed() {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showDialog(new JLabel(), "选择");
File file = jfc.getSelectedFile();
if (file==null){
return null;
}
if (file.isDirectory()) {
Str1 = file.getAbsolutePath();
} else if (file.isFile()) {
Str1 = file.getAbsolutePath();
}
Str = jfc.getSelectedFile().getName(); //System.out.println("文件名:"+Str);
String[] S = Str1.split("\\\\");//因为字符串里的\\ 到了split里,需要分别转义,那么自然就需要用\\ \\来表示了,也就是\\\\
int j = S.length;
for (int i = 0; i < S.length - 1; i++) {
Str2 += S[i] + "/";
}
Str2 += S[j - 1];
System.out.println("处理结果:" + Str2);
return Str2;
}
private boolean isDirectory() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private String getAbsolutePath() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private boolean isFile() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}