当前位置: 技术问答>java相关
关于java对目录、文件的操作问题?
来源: 互联网 发布时间:2015-06-20
本文导语: 请问:怎么获得当前的目录名?怎么得到指定目录下所有的文件? 请各位给这方面的一些资料、用法、程序片断。 | FILE类的GETPATH()LIST() | listFile([args]) | ...
请问:怎么获得当前的目录名?怎么得到指定目录下所有的文件?
请各位给这方面的一些资料、用法、程序片断。
请各位给这方面的一些资料、用法、程序片断。
|
FILE类的GETPATH()LIST()
|
listFile([args])
|
给你个我自己写的程序做例子学习编程:
// 我们有的时候知道一个大概的类名,但是不知道该类的全名,这个时候可以用
// 这个类来解决。
// 方法如下:
// 例如查找一个名字中包含String的类:
C:exammyjavatool>java FindType String
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xml/utils/XMLString.cla
ss
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/functions/FuncStr
ing.class
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/objects/XString.c
lass
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/operations/String
.class
C:JavaWorkjdk1.4b3jrelibrt.jar!sun/awt/CharsetString.class
C:JavaWorkjdk1.4b3jrelibrt.jar!sun/awt/print/PrintDialog$MediaStr
ing.class
C:JavaWorkjdk1.4b3jrelibrt.jar!java/lang/String.class
C:JavaWorkjdk1.4b3jrelibrt.jar!java/text/AttributedString.class
C:JavaWorkjakarta-ant-1.4.1bootstraplibant.jar!org/apache/tools/a
nt/taskdefs/Replace$NestedString.class
完全由fightboy编写,大家都可以使用和修改,传播
代码如下:
import java.io.*;
import java.util.regex.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
public class FindType
{
public void find(String fileName)
{
PrintWriter pw = null;
try{
tmpFile = File.createTempFile("Find.",".tmp",new File("."));
pw = new PrintWriter(new FileOutputStream(tmpFile));
tmpFile.deleteOnExit();
}catch(Exception e)
{
System.err.println("Unable to create Temp file.");
return;
}
String path = System.getProperties().getProperty("java.class.path");
StringTokenizer st = new StringTokenizer(path,";");
while(st.hasMoreTokens())
{
String part = st.nextToken();
File partFile = new File(part);
if(partFile.exists())
{
if(partFile.isDirectory())
findFileInDir(partFile,pw);
else
findFileInJar(partFile,pw);
}
}
pw.close();
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream(tmpFile));
BufferedReader br = new BufferedReader(isr);
findString(fileName,br);
br.close();
}catch(IOException e)
{
System.err.println("Unable to read Temp file.");
return;
}
}
public void findString(String fileName,BufferedReader br) throws IOException
{ String curStr;
String cmpStr;
Pattern p;
if(expr) p = Pattern.compile(fileName);
else p = Pattern.compile(".*"+fileName+"*");
while((curStr=br.readLine())!=null )
{
cmpStr=curStr.replace('/','.');
cmpStr=cmpStr.replace('\','.');
cmpStr=cmpStr.replaceAll("\.class","");
cmpStr=cmpStr.replaceAll(".*!","");
// System.out.println(cmpStr);
Matcher m = p.matcher(cmpStr);
if(m.matches())
System.out.println(curStr);
}
}
public void findFileInDir(File dir,PrintWriter pw)
{
String as[] = dir.list();
for(int j = 0; j
// 我们有的时候知道一个大概的类名,但是不知道该类的全名,这个时候可以用
// 这个类来解决。
// 方法如下:
// 例如查找一个名字中包含String的类:
C:exammyjavatool>java FindType String
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xml/utils/XMLString.cla
ss
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/functions/FuncStr
ing.class
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/objects/XString.c
lass
C:JavaWorkjdk1.4b3jrelibrt.jar!org/apache/xpath/operations/String
.class
C:JavaWorkjdk1.4b3jrelibrt.jar!sun/awt/CharsetString.class
C:JavaWorkjdk1.4b3jrelibrt.jar!sun/awt/print/PrintDialog$MediaStr
ing.class
C:JavaWorkjdk1.4b3jrelibrt.jar!java/lang/String.class
C:JavaWorkjdk1.4b3jrelibrt.jar!java/text/AttributedString.class
C:JavaWorkjakarta-ant-1.4.1bootstraplibant.jar!org/apache/tools/a
nt/taskdefs/Replace$NestedString.class
完全由fightboy编写,大家都可以使用和修改,传播
代码如下:
import java.io.*;
import java.util.regex.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
public class FindType
{
public void find(String fileName)
{
PrintWriter pw = null;
try{
tmpFile = File.createTempFile("Find.",".tmp",new File("."));
pw = new PrintWriter(new FileOutputStream(tmpFile));
tmpFile.deleteOnExit();
}catch(Exception e)
{
System.err.println("Unable to create Temp file.");
return;
}
String path = System.getProperties().getProperty("java.class.path");
StringTokenizer st = new StringTokenizer(path,";");
while(st.hasMoreTokens())
{
String part = st.nextToken();
File partFile = new File(part);
if(partFile.exists())
{
if(partFile.isDirectory())
findFileInDir(partFile,pw);
else
findFileInJar(partFile,pw);
}
}
pw.close();
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream(tmpFile));
BufferedReader br = new BufferedReader(isr);
findString(fileName,br);
br.close();
}catch(IOException e)
{
System.err.println("Unable to read Temp file.");
return;
}
}
public void findString(String fileName,BufferedReader br) throws IOException
{ String curStr;
String cmpStr;
Pattern p;
if(expr) p = Pattern.compile(fileName);
else p = Pattern.compile(".*"+fileName+"*");
while((curStr=br.readLine())!=null )
{
cmpStr=curStr.replace('/','.');
cmpStr=cmpStr.replace('\','.');
cmpStr=cmpStr.replaceAll("\.class","");
cmpStr=cmpStr.replaceAll(".*!","");
// System.out.println(cmpStr);
Matcher m = p.matcher(cmpStr);
if(m.matches())
System.out.println(curStr);
}
}
public void findFileInDir(File dir,PrintWriter pw)
{
String as[] = dir.list();
for(int j = 0; j