2013-08-20 48 views
1

所以我試圖導入一個文件在我的res文件夾中,到目前爲止我一直在使用這種方法。編譯後的Java文件輸入無法正常工作

File f = new File("src/resources/levels/" + part + "/" + filename + ".txt"); 

但經過我編譯並運行它作爲一個罐子就不再起作用,我明白這是因爲它尋找磁盤,而不是在罐子上的文件,我有同樣的問題,導入我的圖片和字體,但我設法修復,但研究後,我只是找不到一種不同的方式。當我運行jar時,我沒有收到任何錯誤,只是信息沒有顯示出來,而且這在eclipse中編譯和運行良好。

這是我的資源文件:

package scrolls; 

import java.awt.Font; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

import javax.annotation.Resource; 
import javax.imageio.ImageIO; 

import org.imgscalr.Scalr; 

public class Resources 
{ 
    static BufferedImage[] textures = new BufferedImage[8]; 
    static BufferedImage[] mapTextures = new BufferedImage[9]; 
    static BufferedImage texture; 
    static BufferedImage[] waterAnimated = new BufferedImage[64]; 
    static BufferedImage water; 
    static BufferedImage icon; 
    public static Font f, fs; 
    static int imageCounter = 0; 

    public Resources() 
    { 
     textures(); 
     createArray(texture, textures, 32, 1, 8); 
     createArray(water, waterAnimated, 32, 64, 1); 
     getFont(); 
     buildMapTextures(textures, mapTextures); 
    } 

    public static void counter() 
    { 
     imageCounter++; 
     if (imageCounter >= 500) 
      imageCounter = 0; 
     //System.out.println(imageCounter/8); 
    } 

    private void buildMapTextures(BufferedImage[] textures, BufferedImage[] mapTextures) 
    { 

     for (int i = 0; i <= 7; i++) 
     { 
      mapTextures[i] = resize(textures[i], 3, 3); 
     } 
     mapTextures[8] = resize(waterAnimated[2], 3, 3); 
    } 

    private BufferedImage resize(BufferedImage image, int newW, int newH) 
    { 
     BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, newW, newH, Scalr.OP_ANTIALIAS); 
     return thumbnail; 
    } 

    public static BufferedImage waterAnimation() 
    { 
     return waterAnimated[imageCounter/8]; 
    } 

    private void textures() 
    { 
     try 
     { 
      texture = ImageIO.read(Resource.class.getResource("/resources/textures.png")); 
     } catch (IOException e) 
     { 
     } 

     try 
     { 
      water = ImageIO.read(Resource.class.getResource("/resources/water.png")); 
     } catch (IOException e) 
     { 
     } 

     try 
     { 
      icon = ImageIO.read(Resource.class.getResource("/resources/icon.png")); 
     } catch (IOException e) 
     { 
     } 

    } 

    static BufferedImage player() 
    { 
     BufferedImage player = null; 
     try 
     { 
      player = ImageIO.read(Resource.class.getResource("/resources/player.png")); 
     } catch (IOException e) 
     { 
     } 
     return player; 
    } 

    static void createArray(BufferedImage image, BufferedImage[] images, int size, int rows, int cols) 
    { 
     BufferedImage temp = image; 

     for (int i = 0; i < rows; i++) 
     { 
      for (int j = 0; j < cols; j++) 
      { 
       images[(i * cols) + j] = temp.getSubimage(j * size, i * size, size, size); 
      } 
     } 
    } 

    void readLevel(String filename, int[][] level, int part) 
    { 
     try 
     { 
      File f = new File("src/resources/levels/" + part + "/" + filename + ".txt"); 
      FileReader fr = new FileReader(f); 
      BufferedReader in = new BufferedReader(fr); 
      StringBuilder sb = new StringBuilder(); 
      byte b = 0; 
      while ((b = (byte) in.read()) != -1) 
      { 
       sb.append("" + ((char) b)); 
      } 
      String str = sb.toString(); 
      String[] lines = str.split("(\n|\r)+"); 
      for (int i = 0; i < lines.length; i++) 
      { 
       for (int j = 0; j < lines[i].length(); j++) 
       { 
        level[i][j] = Integer.parseInt("" + lines[i].charAt(j)); 
       } 
      } 
      in.close(); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private void getFont() 
    { 
     try 
     { 
      f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/resources/Jet Set.ttf")); 
      fs = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/resources/Jet Set.ttf")); 
     } catch (Exception e) 
     { 
      System.out.println(e); 
     } 
     f = f.deriveFont(22f); 
     fs = fs.deriveFont(13f); 
    } 
} 

所以,我怎麼會導入我的文件,以便它可以被讀取時,編譯成一個罐子?

+0

'/ src'不會在運行時使用(是你搬出去的IDE)。這聽起來像你在使用Eclipse,我相信Eclipse允許你創建一個'resources'文件夾,同時會自動包含在你的程序中,你應該把資源移動到它(如果你沒有,那麼不用擔心)。路徑應該看起來更像'/ resources/levels /「+ part +」/「+ filename +」.txt「 – MadProgrammer

+0

@MadProgrammer我創建了一個資源包,並將所有的東西放在那裏,/ reresources .... 。與其他東西,如我正在導入的圖像和字體正常工作,但它不適用於txt文件 – user2457344

+0

但爲什麼它存儲在'src'目錄? – MadProgrammer

回答

0

src文件夾不應該在jar裏面,它應該在jar文件旁邊,並且使用絕對路徑。 http://www.tutorialspoint.com/java/io/file_getabsolutepath.htm

編輯:

public void install() throws IOException { 
     String absolutePath = new File("").getAbsolutePath(); 
      JarFile Jar = new JarFile(new File(absolutePath + "/jarName.jar")); 
      extractJar(Jar, absolutePath + "/src/"); 
} 

    public void extractJar(JarFile jar, String dest) throws IOException { 
      java.util.Enumeration enu = jar.entries(); 
      while (enu.hasMoreElements() == true) { 
       java.util.jar.JarEntry file = (java.util.jar.JarEntry) enu.nextElement(); 
       java.io.File f = new java.io.File(dest + java.io.File.separator + file.getName()); 
       System.out.println(file.getName()); 
       if(!f.exists()) 
       { 
        f.getParentFile().mkdirs(); 
       } 

       if (file.isDirectory()) { // if its a directory, create it 
        f.mkdir(); 
        continue; 
       } 
       java.io.InputStream is = jar.getInputStream(file); // get the input stream 
       java.io.FileOutputStream fos = new java.io.FileOutputStream(f); 
       while (is.available() > 0) { // write contents of 'is' to 'fos' 
        fos.write(is.read()); 
       } 
       fos.close(); 
       is.close(); 
      } 
     } 
+0

在eclipse中當我出口作爲可執行Jar它是如何使我的罐子。 – user2457344

+1

'levels .txt'的性質表明它是一個應用程序資源,它通常會作爲**嵌入的資源**交付,而不能由'File'訪問。 –

+0

那麼你將不得不使用JarOutputStream在語法上提取它,然後你可以引用它被提取到的文件夾。 – marko5049

1

交換FileReaderInputStreamReader這樣的事情..

URL urlToText = this.getClass().getResource(
    "/resources/levels/" + part + "/" + filename + ".txt"); 
InputStream is = urlToText.openStream(); 
InputStreamReader isr = new InputStreamReader(is); 
BufferedReader in = new BufferedReader(isr); 
+0

我得到一個錯誤.openURL,「方法openURL()未定義類型的URL」 – user2457344

+0

請參閱編輯的答案。順便說一句 - 對JavaDocs的快速檢查可能爲您解決了它。這應該是第一個檢查的地方.. –

+0

即時得到一個不同的錯誤,「類型不匹配:不能從InputStream轉換到InputStreamReader」 – user2457344