上层逻辑控制和事务处理使用 Java 开发,而底层核心功能使用 C/C++ 实现,这已经成为一种较为通用的开发模式。但由于 Windows 操作系统的默认设置,上述语言在对长路径名(>260 字符)文件的处理时会遇到一些问题。本文列出了不同的 JDK 版本在 Windows 操作系统上对于长路径名文件处理的区别,给出了两种支持长路径名文件的 C/C++ 编程方法,同时还指出了从 JDK 5.0 开始才完全支持长路径名。使用本文的方法,可以解决在 Windows 平台上标准 API 函数对长路径名文件支持的局限性问题,给开发测试工作带来方便。
Windows 对长路径名文件的限制
众所周知,微软的文件系统经历了 fat->fat32->NTFS 的技术变革。且不论安全和文件组织方式上的革新,单就文件名而言,已经从古老的 DOS 8.3 文件格式(仅支持最长 8 个字符的文件名和 3 个字符的后缀名)转变为可以支持长达 255 个字符的文件名。而对于路径长度,NTFS 也已经支持长达 32768 个字符的路径名。
然而,Windows 操作系统并没有完全放开路径名长度的限制,在 windef.h 中,可以找到如下的宏:
#define MAX_PATH 260
事实上,所有的 Windows API 都遵循这个限制。因此,每当我们试图更改某一文件的文件名时,当输入的文件名长度 ( 全路径 ) 到达一定限度时,虽然文件名本身还未达到 255 个字符的限制,但是任何输入将不再被接受,这其实正是由于操作系统不允许 260 个字符(byte)的文件全路径。
实际应用中,这种 260 个字符的全路径的限制给应用开发带来了很大的不便。试想如下应用:我们希望给应用服务器增加一个本地 cache 的功能,该功能可以把远程服务器上的文件留下一个本地的副本。一个合理的实现可以把 url 映射为文件名,当 url 很长时,cache 文件的长度也会很长。当文件名长度超过 255,我们可以把映射文件名的前 255 个字符作为目录名称。但是,我们仍然无法解决 260 个字符的全路径限制。另外,如果一个应用软件的目录结构过深,很容易出现某些文件名长度(含路径)超过 260 个字符,并因此造成安装或删除的失败。总而言之,该限制给我们的开发测试工作带来了诸多不便。
对于一些网络服务器,往往需要将 Java 代码用于上层逻辑控制 / 事务处理的开发,同时将 C/C++ 用于底层核心功能的实现。为此,我们研究了这两种程序语言对长路径名文件的支持情况。其中,对于 Java,比较了两个常用版本 1.4 和 5.0 对长路径支持的差异性;对于 C/C++ 语言的局限性,提出了我们的解决方法。
实验环境 :
操作系统: Windows xp
文件系统: NTFS 文件系统
Java 编译环境: IBM JDK 1.4.2 以及 IBM JDK 5.0
C++ 编译环境: VC.net
在 Java 中使用长路径名文件
Java 语言并不需要对长路径名文件进行特殊的处理,就可以支持长路径名文件的创建、读写和删除操作等基本操作。但是,JDK 1.4.2 和 JDK 5.0 在长路径的支持上是不同的,JDK 1.4.2 并不是完全支持所有的长路径名文件操作,比如访问文件属性的操作是不支持的。我们设计了如下代码来验证 JDK 1.4.2 和 JDK 5.0 对长路径名文件支持的区别。
清单 1. 对长路径名文件操作的 Java 实验代码:
try {
String fileName = "E:\\VerylongpathVerylongpathVerylongpath
VerylongpathVerylongpathVerylongpathVerylongpath
VerylongpathVerylongpathVerylongpathVerylongpath\\
VerylongpathVerylongpathVerylongpathVery
longpathVerylongpathVerylongpathVerylongpath
VerylongpathVerylongpathVerylongpathVerylongpa
th.txt";
System.out.println("Filename: " + fileName);
System.out.println("File path length: " + fileName.length());
String renameFileName = "E:\\VerylongpathVerylongpathVerylongpath
VerylongpathVerylongpathVerylongpathVerylongpath
VerylongpathVerylongpathVerylongpathVerylongpath\\Short.txt";
//Create the file.
File file = new File(fileName);
if (!file.exists())
file.createNewFile();
if (file.exists())
System.out.println("The file exists!");
if (file.canRead())
System.out.println("The file can be read!");
if (file.canWrite())
System.out.println("The file can be written!");
if (file.isFile())
System.out.println("It's a file!");
//Write to the created file.
FileOutputStream out = new FileOutputStream(file);
PrintStream p = new PrintStream(out);
p.println("This is only a test!");
p.close();
//Read the information from that file.
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
while (true) {
String sl = br.readLine();
if (sl == null) {
break;
} else {
sb.append(sl + "\n");
}
}
br.close();
System.out.println("The content in the file:");
System.out.print("\t" + sb.toString());
//File rename
File newfile = new File(renameFileName);
if (newfile.exists())
System.out.println(renameFileName + "exsited");
else {
if (file.renameTo(newfile)){
System.out.println("Rename sucessful!");
} else {
System.out.println("Rename failed!");
}
}
//delete file
if (file.delete())
System.out.println("The old file deleted!");
if (newfile.delete())
System.out.println("The renamed file deleted!");
} catch (IOException e) {
//Error happened
e.printStackTrace();
System.out.println("Error occurs in writing to the file.");
}
}