关于java对目录、文件的操作问题? 我试过了,getpath()不能得到目录 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个我自己写的程序做例子学习编程: // 我们有的时候知道一个大概的类名,但是不知道该类的全名,这个时候可以用 // 这个类来解决。 // 方法如下: // 例如查找一个名字中包含String的类:C:\exam\myjavatool>java FindType StringC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xml/utils/XMLString.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/functions/FuncString.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/objects/XString.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/operations/String.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/CharsetString.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/print/PrintDialog$MediaString.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/lang/String.classC:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/text/AttributedString.classC:\JavaWork\jakarta-ant-1.4.1\bootstrap\lib\ant.jar!org/apache/tools/ant/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 < as.length; j++) { File next = new File(""+dir+"\\"+as[j]); if(next.isFile()) { if(next.getAbsolutePath().endsWith(".class")) pw.println(next.getAbsolutePath()); } else { findFileInDir(next,pw); } } } public void findFileInJar(File jarFile,PrintWriter pw) { try{ JarInputStream jis = new JarInputStream(new FileInputStream(jarFile)); ZipEntry entry = jis.getNextEntry(); while(entry != null) { if(!entry.isDirectory()) { if(entry.getName().endsWith(".class")) pw.println(jarFile.getAbsolutePath()+"!"+entry.getName()); } jis.closeEntry(); entry = jis.getNextEntry(); } } catch (Exception e) { System.err.println("Error in File "+jarFile.getName()); } } public static void main(String args[]) { FindType ft = new FindType(); String name = null; for(int i=0;i<args.length;i++) { if(args[i].equalsIgnoreCase("-e")) ft.expr=true; else if(args[i].equalsIgnoreCase("-cp")) { i++; System.getProperties().setProperty("java.class.path",args[i]); } else name = args[i]; } if(name != null) ft.find(name); else { System.err.println("Usage: java FindType [-e] <fileName> [-cp classPath]"); System.exit(1); } } private File tmpFile; private boolean expr=false; } 还是不行例如我要得到“my.txt”这个文件所在的目录File f = new File("my.txt");String d = f.getPath();String dd = f.getAbsoluteFile();打印d 和dd 结果都是“my.txt” 程序在jdk1.4下边编译运行通过! (1)用“.”做目录建立一个文件,然后对这个文件用getAbsolutePath()得到绝对路经。(2)没必要 那就用这个吧刚才我试了一下可以用的getAbsolutePath() 英雄会之在线笔试面试,等你来挑战(更新至2013/5/22) 求解决客户端写出的字符串为什么不能在服务器端控制台显示。 jsp页面内实现 不同 模块的 拖曳 并能够保存 小鸟请教老鸟 java Unicode问题 装了tomcat后http://localhost:8080/该页无法显示! 监听器到底是什么意思? 《Jsp与Servelet权威指南》和《Jsp与Servelet核心技术》那本翻译比较好? java高手请进,一个问题 如何将.class 文件做成 .exe文件 被迫重贴。两段小程序竟让无数高手折腰!!! 如何才能更改label上的图片?
// 我们有的时候知道一个大概的类名,但是不知道该类的全名,这个时候可以用
// 这个类来解决。 // 方法如下: // 例如查找一个名字中包含String的类:C:\exam\myjavatool>java FindType String
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xml/utils/XMLString.cla
ss
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/functions/FuncStr
ing.class
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/objects/XString.c
lass
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/operations/String
.class
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/CharsetString.class
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/print/PrintDialog$MediaStr
ing.class
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/lang/String.class
C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/text/AttributedString.class
C:\JavaWork\jakarta-ant-1.4.1\bootstrap\lib\ant.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 < as.length; j++)
{ File next = new File(""+dir+"\\"+as[j]);
if(next.isFile())
{ if(next.getAbsolutePath().endsWith(".class"))
pw.println(next.getAbsolutePath());
}
else
{
findFileInDir(next,pw);
}
}
}
public void findFileInJar(File jarFile,PrintWriter pw)
{
try{
JarInputStream jis = new JarInputStream(new FileInputStream(jarFile));
ZipEntry entry = jis.getNextEntry();
while(entry != null)
{ if(!entry.isDirectory())
{ if(entry.getName().endsWith(".class"))
pw.println(jarFile.getAbsolutePath()+"!"+entry.getName());
}
jis.closeEntry();
entry = jis.getNextEntry();
}
} catch (Exception e)
{
System.err.println("Error in File "+jarFile.getName());
}
}
public static void main(String args[])
{
FindType ft = new FindType();
String name = null;
for(int i=0;i<args.length;i++)
{
if(args[i].equalsIgnoreCase("-e")) ft.expr=true;
else if(args[i].equalsIgnoreCase("-cp"))
{ i++;
System.getProperties().setProperty("java.class.path",args[i]);
}
else
name = args[i];
}
if(name != null)
ft.find(name);
else
{
System.err.println("Usage: java FindType [-e] <fileName> [-cp classPath]");
System.exit(1);
}
}
private File tmpFile;
private boolean expr=false;
}
例如我要得到“my.txt”这个文件所在的目录
File f = new File("my.txt");
String d = f.getPath();
String dd = f.getAbsoluteFile();打印d 和dd 结果都是“my.txt”
绝对路经。
(2)没必要
刚才我试了一下
可以用的getAbsolutePath()