java如何判断文件路径?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何判断文件路径?相关的知识,希望对你有一定的参考价值。

比如我想写个a.java,在a.java里完成功能:可以得知这个a.java是放在电脑里哪个目录里的。就是判断类自己的文件路径是什么,怎么做呢
一楼是胡扯。我是让这个java类实现寻找自己所在文件夹的功能,我人工打开那还用你说?

package jixutest;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
public class TestERead extends Frame implements ActionListener,ItemListener
private List list;//用于显示文件列表
private TextField details;//用于显示选中的文件(夹)详细情况
private Panel buttons;//用于放置两个button
private Button up,close;
private File currentDir;
private FilenameFilter filter;//文件筛选器
private String[] files;//放置文件(夹)名的数组
private DateFormat dateFormatter=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);//日期格式化,注意getDateTimeInstance有两个参数,分别表示日期和时间
/*
* 说明:
* DateFormat shortDateFormat =DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
* DateFormat mediumDateFormat =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
* DateFormat longDateFormat =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
* DateFormat fullDateFormat =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
* 对应的日期时间格式分别如下:
* 9/29/01 8:44 PM
* Sep 29, 2001 8:44:45 PM
* September 29, 2001 8:44:45 PM EDT
* Saturday, September 29, 2001 8:44:45 PM EDT
* */
public TestERead(String directory,FilenameFilter filter)
super("File Lister");//窗口标题
this.filter=filter;//文件筛选器
addWindowListener(new WindowAdapter()//关闭窗口
public void windowClosing(WindowEvent e)
dispose();

);
list=new List(12,false);//一个12行的list表单
list.setFont(new Font("MonoSpaced",Font.PLAIN,14));//设置字体
list.addActionListener(this);//捕捉双击鼠标的行为
list.addItemListener(this);//捕捉单击鼠标的行为(选中项)

details=new TextField();//显示文件详情
details.setFont(new Font("MonoSpaced",Font.PLAIN,12));
details.setEditable(false);//readonly

buttons=new Panel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,15,15));//靠右排列项,间距15 15
buttons.setFont(new Font("SansSerif",Font.BOLD,14));

up=new Button("上一级目录");
close=new Button("关闭");
up.addActionListener(this);
close.addActionListener(this);

buttons.add(up);
buttons.add(close);

this.add(list,"Center");
this.add(details,"North");
this.add(buttons,"South");
this.setSize(500,300);

listDirectory(directory);//列出指定目录

public void listDirectory(String directory)//此方法作用是列出文件,并不涉及文件的操作
File dir=new File(directory);//创建文件(目录)对象
if(!dir.isDirectory())
throw new IllegalArgumentException("FileLister:no such directory");

files=dir.list(filter);//列出文件并用filter筛选
java.util.Arrays.sort(files);//排序文件
list.removeAll();//清除此前列出的文件列表
list.add("上一级目录");
for(int i=0;i<files.length;i++)
list.add(files[i]);//将文件加入到列表框
this.setTitle(directory);
details.setText(directory);
currentDir=dir;

public void itemStateChanged(ItemEvent e) //选中项(单击)的相应行为,并不涉及双击项目
int i=list.getSelectedIndex()-1;//因为我们在列表中的第一项是“上一级目录”,因此列表的selectedindex要比files的index大一,而我们要获取files的index,就要相应减去1
if(i<0)
return;
String filename=files[i];
File f=new File(currentDir,filename);
if(!f.exists())
throw new IllegalArgumentException("FileLister:no such file or directory");
String info=filename;
if(f.isDirectory())
info+=File.separator;//如果是目录就在后面增加一个分隔符,windows下是“\”,Linux下是“/”
info+=" "+f.length()+ " bytes ";
info +=dateFormatter.format(new java.util.Date(f.lastModified()));
if(f.canRead()) info+=" Read";
if(f.canWrite()) info+=" Write";
details.setText(info);

public void actionPerformed(ActionEvent e)//双击项目、单击button的行为定义,由此我们也发现,对于列表的双击和对按钮的单击都属于ActionEvent
if(e.getSource()==close) this.dispose();//按钮close的行为定义
else if(e.getSource()==up) up();//按钮up的行为定义
else if(e.getSource()==list)
int i=list.getSelectedIndex();
if(i==0) up();//如果selectedindex为0,即第一项,就是“上一级目录”那一项,就调用up()方法
else
String name=files[i-1];
File f=new File(currentDir,name);
String fullname=f.getAbsolutePath();//取得文件(目录)的绝对路径
if(f.isDirectory()) listDirectory(fullname);//如果是目录,则列出
// else new FileViewer(fullname).setVisible(true);//否则创建一个FileViewer类,即要弹出我们昨天所写的FileViewer窗口来读取文件的内容



protected void up()//列出父目录
String parent=currentDir.getParent();
if(parent==null) return;
listDirectory(parent);//一个递归

public static void usage()
System.out.println("Usage: java FileLister [directory_name] "+"[-e file_extension]");
System.exit(0);


public static void main(String args[]) throws IOException
TestERead f;
FilenameFilter filter=null;
String directory=null;

for(int i=0;i<args.length;i++)
if(args[i].equals("-e"))
if(++i>args.length) usage();
final String suffix=args[i];//文件扩展名(-e后接着的输入参数,因为前面已经有++i)
filter=new FilenameFilter()//筛选器filter要实现FilenameFliter接口
public boolean accept(File dir,String name)
/*Tests if a specified file should be included in a file list.
* dir - the directory in which the file was found.
* name - the name of the file.
* 就是说,如果文件的扩展名跟我们输入的一致,则返回true
* 否则判断文件是否为一个目录,如果是一个目录,那么也返回
* 而其他不符合我们输入的文件,则不列出
*/
if(name.endsWith(suffix)) return true;
else return (new File(dir,name)).isDirectory();

;

else
if(directory!=null) usage();//我们初始化的目录是null的,如果非null,表明程序有问题,退出
else directory=args[i];//否则将-e前一个参数赋给directory


if(directory==null) directory=System.getProperty("user.dir");//如果没有输入目录参数,那么就用用户当前目录
f=new TestERead(directory,filter);//创建一个FileLister,这里有两个参数,一个是目录,一个是筛选器,这个筛选器就是我们刚才创建的那个,注意这个类中我们并没有在main方法之外单独创建一个筛选器方法
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);
f.setVisible(true);

参考技术A 你说的是要获取程序文件的当前路径吧.
使用系统函数System.getProperty()可获取当前路径
例子:
System.out.println(System.getProperty("user.dir"));
user.dir指定了当前的路径

参考资料:http://hi.baidu.com/xcl119xcl/blog/item/91d9b3f0559178a9a50f5200.html

本回答被提问者采纳
参考技术B 你在那个文件夹里它就在那个文件夹编译运行它就OK 了 参考技术C String path = System.getProperty("user.dir"); 参考技术D new java.io.File(".").getCanonicalPath()

java判断路径是文件夹还是文件

当给定一个路径没有后缀的时候,很难分辨代码是文件还是文件夹,如下图:

我在桌面建立了一个名为one的文件,路径为:/Users/XXXXXX/Desktop/one

java代码如下:

import java.io.File;
public class Flie_or_Folder {
    
    public static void main(String s[]){
        String path ="/Users/XXXXX/Desktop/one";
        File file = new File(path);
        if(file.isDirectory()){
            System.out.println("是文件夹");
        }
        if(file.isFile()){
            System.out.println("是文件");
        }
    }
}

运行结果:

 

以上是关于java如何判断文件路径?的主要内容,如果未能解决你的问题,请参考以下文章

用JAVA写出一个方法,给定一个路径,判断路径是不是是一个文件。

java中如何判断web工程中图片的绝对路径是不是存在

c#中如何判断一个路径是目录还是文件

java 如果我有一个路径比如 E:/123 在这个路径表示的文件不存在时 如何判断是不是是文件或

c#中如何检测文件路径是不是存在

java 判断路径是不是为文件