新手用戶在這裏,第一次我在這裏發帖,所以我會盡力讓它正確,對於任何錯誤提前抱歉。在eclipse中讀取文件內部不工作的文件
我一直在嘗試從jar文件夾內的文件中讀取文件,並將它們的路徑添加到字符串數組中。我的JAR文件夾結構是這樣的: 的.class文件位於FileIO.jar \ COM \ yumeprojects \ flowdemonstration \ 圖像位於FileIO.jar \ COM \ yumeprojects \ flowdemonstration \影像\
當我運行程序從eclipse外部的編譯jar文件得到的結果是準確的,找到5個圖像並將它們的路徑添加到數組中。 這是輸出:
E:\Yumeprojects 2011-2012\File IO tests>java -jar "FileIO0_2.jar"
(file:/E:/Yumeprojects%202011-2012/File%20IO%20tests/FileIO0_2.jar <no signer ce
rtificates>)
com/yumeprojects/flowdemonstration/test/bakgrund01.png
com/yumeprojects/flowdemonstration/test/bakgrund02.png
com/yumeprojects/flowdemonstration/test/bakgrund03.png
com/yumeprojects/flowdemonstration/test/bakgrund04.png
com/yumeprojects/flowdemonstration/test/bakgrund05.png
Number of images: 5
當我從Eclipse中運行我得到這個:
(file:/E:/Yumeprojects%202011-2012/Project%20Mathematics/flow/bin/ <no signer certificates>)
Number of images: 0
的代碼看起來是這樣的,是不是我的,我抄了一遍,從這個網站。
package com.yumeprojects.flowdemonstration;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
String[] fileNames;
CodeSource src = Main.class.getProtectionDomain().getCodeSource();
List<String> list = new ArrayList<String>();
public static void main(String[] args){
Main m = new Main();
m.init();
}
public void init(){
try {
read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read() throws IOException{
if(src != null){
URL jar = src.getLocation();
System.out.println(jar);
ZipInputStream zip = new ZipInputStream(jar.openStream());
System.out.println(zip);
System.out.println(zip.getNextEntry());
ZipEntry ze = null;
ZipEntry ze2 = null;
while((ze = zip.getNextEntry()) != null){
String entryName = ze.getName();
if(entryName.endsWith(".png") || entryName.endsWith(".PNG")) {
list.add(entryName);
System.out.println(entryName);
}
}
}
fileNames = list.toArray(new String[list.size() ]);
System.out.println("Number of images: " + fileNames.length);
}
}
爲什麼不在eclipse中工作?它需要一個jar文件來執行嗎?如果是這樣,我如何讓eclipse執行它,以便我可以在eclipse中使用此代碼,並獲得與從Eclipse以外的jar中啓動它時獲得的結果相同的結果?
請幫
//庫爾滕
編輯 由於這些評論並非用於分析代碼,好,我會在這裏解析它,而不是 Syso看起來像這樣從Eclipse內部:
file:/E:/Yumeprojects%202011-2012/Project%20Mathematics/flow/bin/
[email protected]
null
Number of images: 0
外:
E:\Yumeprojects 2011-2012\File IO tests>java -jar "FileIO0_3.jar"
file:/E:/Yumeprojects%202011-2012/File%20IO%20tests/FileIO0_3.jar
[email protected]
META-INF/MANIFEST.MF
com/yumeprojects/flowdemonstration/test/bakgrund01.png
com/yumeprojects/flowdemonstration/test/bakgrund02.png
com/yumeprojects/flowdemonstration/test/bakgrund03.png
com/yumeprojects/flowdemonstration/test/bakgrund04.png
com/yumeprojects/flowdemonstration/test/bakgrund05.png
Number of images: 5
E:\Yumeprojects 2011-2012\File IO tests>
第二次編輯:這是工作代碼!它並不漂亮,代碼結構完全是地獄,但它做它應該做的。感謝Guillaume Polet幫助我解決這個問題!
package com.yumeprojects.flowdemonstration;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
String[] fileNames;
CodeSource src = Main.class.getProtectionDomain().getCodeSource();
List<String> list = new ArrayList<String>();
URL jar;
URI jar2;
File source;
String[] images;
static boolean inIDE;
public static void main(String[] args){
if(args.length > 0){
for(int i = 0; i < args.length; i++){
if(args[i].equalsIgnoreCase("inIDE")){
inIDE = true;
}
}
}
Main m = new Main();
m.init();
}
public void init(){
try {
read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read() throws IOException{
if(src != null){
jar = src.getLocation();
if(inIDE){
try {
jar2 = new URI(jar.toURI().toString() + "/com/yumeprojects/flowdemonstration/test");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jar2);
source = new File(jar2);
images = source.list();
for(int i = 0; i < images.length; i++){
System.out.println(images[i]);
}
}else{
System.out.println(jar);
ZipInputStream zip = new ZipInputStream(jar.openStream());
System.out.println(zip);
System.out.println(zip.getNextEntry());
ZipEntry ze = null;
while((ze = zip.getNextEntry()) != null){
String entryName = ze.getName();
if(entryName.endsWith(".png") || entryName.endsWith(".PNG")) {
list.add(entryName);
System.out.println(entryName);
}
}
fileNames = list.toArray(new String[list.size() ]);
System.out.println("Number of images: " + fileNames.length);
}
}
}
}
我猜你正在Eclipse項目中運行相同的代碼?所以這意味着這些類不會被打包成jar。這會導致您的代碼在jar中的假設無效。你有沒有嘗試過你的jar網址系統? – 2012-03-04 22:06:21
syso看起來像這次日食 文件中:/ E:/ Yumeprojects%202011-2012 /項目%20Mathematics /流程/斌/ [email protected] 空 圖像數:0 外食 file:/ E:/ Yumeprojects%202011-2012/Project%20Mathematics/flow/bin/ [email protected] null 圖片數量:0 – Kurten 2012-03-04 22:59:50