「路径选择java」路径选择工具

博主:adminadmin 2023-01-01 15:06:07 993

今天给各位分享路径选择java的知识,其中也会对路径选择工具进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java 文件输出路径选着

可以通过swing技术实现,继承自JFileChooser 即可。也可以设置固定类型的文件选择。

举例:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package hkrt.b2b.util;

import java.io.File;

//import java.io.FileFilter;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

/**

*/

public class FileChooser extends JFileChooser {

JFileChooser jfc = new JFileChooser();

public String openWin() {

jfc.setAcceptAllFileFilterUsed(false);//设置文件过滤条件,在文件选择中没有“所有文件”的选项

jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置文件选择类型,在这里只是选择具体文件

jfc.setFileFilter(

new FileFilter() {

@Override

public boolean accept(File f) {

if(f.getName().toLowerCase().endsWith(".xls")){

return f.getName().toLowerCase().endsWith(".xls");//添加过滤文件类型。以后缀做判断

} else if (f.getName().toLowerCase().endsWith(".xlsx")){

return f.getName().toLowerCase().endsWith(".xlsx");

}

return false;

}

@Override

public String getDescription() {

return "Excel File";//在文件类型中的显示

}

});

jfc.showOpenDialog(null);

File xls = jfc.getSelectedFile();

if(xls == null){

return "";

}

String resultOpen = jfc.getSelectedFile().getPath();//获取文件路径

return resultOpen;

}

}

备注:以上方法就获取到了文件的绝对路径,返回值即是路径值。

JAVA选择文件夹路径,该怎么解决

用JFileChooser,并且setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

Java codepublic class DemoJFileChooser extends JPanel

implements ActionListener {

JButton go;

JFileChooser chooser;

String choosertitle;

public DemoJFileChooser() {

go = new JButton("Do it");

go.addActionListener(this);

add(go);

}

public void actionPerformed(ActionEvent e) {

int result;

chooser = new JFileChooser();

chooser.setCurrentDirectory(new java.io.File("."));

chooser.setDialogTitle(choosertitle);

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

//

// disable the "All files" option.

//

chooser.setAcceptAllFileFilterUsed(false);

//

if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

System.out.println("getCurrentDirectory(): "

+ chooser.getCurrentDirectory());

System.out.println("getSelectedFile() : "

+ chooser.getSelectedFile());

}

else {

System.out.println("No Selection ");

}

}

public Dimension getPreferredSize(){

return new Dimension(200, 200);

}

public static void main(String s[]) {

JFrame frame = new JFrame("");

DemoJFileChooser panel = new DemoJFileChooser();

frame.addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

);

frame.getContentPane().add(panel,"Center");

frame.setSize(panel.getPreferredSize());

frame.setVisible(true);

}

}

java自动路径选择

用 File 类 封装, 然后用 getAbsolutePath 方法

File A=new File("文件名及其路径");

String Path=A.getAbsolutePath();

java导出文件时让用户选择路径怎么弄?

使用保存文件对话框:\x0d\x0a\x0d\x0a /**\x0d\x0a * 保存\x0d\x0a */\x0d\x0aprivate void saveFile(){\x0d\x0aJFileChooser dialog = new JFileChooser();\x0d\x0adialog.setDialogTitle("另存为");\x0d\x0adialog.setFileSelectionMode(JFileChooser.FILES_ONLY);\x0d\x0adialog.setDialogType(JFileChooser.SAVE_DIALOG);\x0d\x0adialog.setFileFilter(new TextFileFilter("*.txt", "文本文档(*.txt)"));\x0d\x0aint result = dialog.showSaveDialog(this);\x0d\x0aif(result == JFileChooser.APPROVE_OPTION){\x0d\x0aFile file = dialog.getSelectedFile();\x0d\x0afileName = file.getAbsolutePath();//得到文件全名\x0d\x0a...\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a附文本类型过滤器:\x0d\x0aimport java.io.File;\x0d\x0aimport java.util.ArrayList;\x0d\x0aimport javax.swing.filechooser.FileFilter;\x0d\x0a\x0d\x0a/**\x0d\x0a * 设置文件打开对话框的文件过滤器\x0d\x0a * @author developer\x0d\x0a */\x0d\x0apublic class TextFileFilter extends FileFilter {\x0d\x0aprivate ArrayList extensions = new ArrayList();\x0d\x0aprivate ArrayList descriptions = new ArrayList();\x0d\x0a\x0d\x0apublic TextFileFilter(){\x0d\x0asuper();\x0d\x0a}\x0d\x0a\x0d\x0apublic TextFileFilter(String extension, String description) {\x0d\x0asuper();\x0d\x0athis.extensions.add(extension);\x0d\x0athis.descriptions.add(description);\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic boolean accept(File pathname) {\x0d\x0aif (pathname != null) {\x0d\x0aif (pathname.isDirectory()) {\x0d\x0areturn true;\x0d\x0a}\x0d\x0aString extension = getExtension(pathname);\x0d\x0afor(int i=0; i

怎样根据自己电脑上Java安装的正确路径进行选择保存设置

你先试1下找到你安装的Java程序的bin目录,这个bin目录路径就是“怎样根据自己电脑上Java安装的正确路径进行选择,保存设置”需要的路径。注:首先得安装java程序,有JDK和JRE两个,前者是给开发人员使用的,后者是给普通用户是用的,你需要安装JRE。不推荐安装到C盘,你可以安装到D盘(安装路径不可以有空格和中文字符)。剩下的就是找bin目录了如果这个方法不行,可以追问,我再给你其他方法

查看原帖

路径选择java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于路径选择工具、路径选择java的信息别忘了在本站进行查找喔。