为什么相同的 JAVA 程序在 Windows 和 Linux 等不同平台上的工作方式不同?
Why same JAVA program is working differently in different Platforms like Windows and Linux?
相同的和平 JAVA 程序在不同平台上的工作方式不同。
例如我写了一个 JAVA 用于将存储在一个文件夹中的不同 Tiff 文件合并为多页 Tiff。
请在下面找到程序。
public String merge(String dirPath) {
String inputDir = dirPath;
File faxSource = new File(inputDir);
File file[] = faxSource.listFiles();
int numImages = file.length;
String name = "";
List<BufferedImage> images = new ArrayList<BufferedImage>();
Arrays.sort(file, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
try {
for (int i = 0; i < numImages; i++) {
name = name + file[i].getName();
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",
ss, null);
int numPages = decoder.getNumPages();
for (int j = 0; j < numPages; j++) {
PlanarImage op = new NullOpImage(
decoder.decodeAsRenderedImage(j), null, null,
OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
}
// name=UUID.randomUUID().toString()+".tiff";
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
OutputStream out = new FileOutputStream(inputDir + "\" + name);
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out,
params);
// encoder.
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 1; i < images.size(); i++) {
imageList.add(images.get(i));
}
params.setExtraImages(imageList.iterator());
encoder.encode(images.get(0));
out.close();
} catch (Exception e) {
}
return inputDir + "\" + name;
}
假设文件夹中包含4张tiff图片(A.tiff,B.tiff,C,.tiff,D.tiff)。这些Tiff文件是从S#中依次下载的。
如果我运行上面的程序在windows服务器中它正在按照A.tiff+B.tiff+[=的顺序进行整理37=]+D.tiff.
如果我 运行 Amazon EC2 中的同一个程序 Linux 正在获取输出 A.tiff+B.tiff+D.tiff +C.tiff。
知道为什么相同的 JAVA 代码在 Windows 和 Linux 中 运行 不同吗?
检查 Linux 盒子上的文件系统; EXT3
的修改日期精度,我猜它正在使用,是 1 秒。如果您在一秒钟内下载两个文件,它们可能具有相同的时间。
Windows,另一方面,通常使用 NTFS,其文件时间精度为 100 纳秒。
相同的和平 JAVA 程序在不同平台上的工作方式不同。
例如我写了一个 JAVA 用于将存储在一个文件夹中的不同 Tiff 文件合并为多页 Tiff。
请在下面找到程序。
public String merge(String dirPath) {
String inputDir = dirPath;
File faxSource = new File(inputDir);
File file[] = faxSource.listFiles();
int numImages = file.length;
String name = "";
List<BufferedImage> images = new ArrayList<BufferedImage>();
Arrays.sort(file, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
try {
for (int i = 0; i < numImages; i++) {
name = name + file[i].getName();
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",
ss, null);
int numPages = decoder.getNumPages();
for (int j = 0; j < numPages; j++) {
PlanarImage op = new NullOpImage(
decoder.decodeAsRenderedImage(j), null, null,
OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
}
// name=UUID.randomUUID().toString()+".tiff";
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
OutputStream out = new FileOutputStream(inputDir + "\" + name);
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out,
params);
// encoder.
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 1; i < images.size(); i++) {
imageList.add(images.get(i));
}
params.setExtraImages(imageList.iterator());
encoder.encode(images.get(0));
out.close();
} catch (Exception e) {
}
return inputDir + "\" + name;
}
假设文件夹中包含4张tiff图片(A.tiff,B.tiff,C,.tiff,D.tiff)。这些Tiff文件是从S#中依次下载的。
如果我运行上面的程序在windows服务器中它正在按照A.tiff+B.tiff+[=的顺序进行整理37=]+D.tiff.
如果我 运行 Amazon EC2 中的同一个程序 Linux 正在获取输出 A.tiff+B.tiff+D.tiff +C.tiff。
知道为什么相同的 JAVA 代码在 Windows 和 Linux 中 运行 不同吗?
检查 Linux 盒子上的文件系统; EXT3
的修改日期精度,我猜它正在使用,是 1 秒。如果您在一秒钟内下载两个文件,它们可能具有相同的时间。
Windows,另一方面,通常使用 NTFS,其文件时间精度为 100 纳秒。