1

所以我尝试导入我的 res 文件夹中的文件,到目前为止我一直在使用这种方法。

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

但是在我编译它并将它作为一个 jar 运行之后它不再工作,我理解这是因为它在磁盘上而不是 jar 上寻找文件,我在导入我的图像和字体时遇到了同样的问题,但我设法修复那但是经过研究,我找不到不同的方法。运行 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);
    }
}

那么我将如何导入我的文件以便在编译成 jar 时可以读取它?

4

2 回答 2

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);
于 2013-08-20T23:58:12.250 回答
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();
            }
        }
于 2013-08-20T23:54:13.817 回答