简体   繁体   中英

How to get Java Resource file into byte[]?

I have a Java program that needs to read a file from a resource within the JAR and it only takes it through byte[]. My problem is converting the resource file from a folder within the project (ie tools/test.txt) into byte[]. I have tried the following (gave an "undefined for type" error):

final byte[] temp = new File("tools/test.txt").getBytes();

Another method I tried resulted in not being able to find the file:

 FileOutputStream fos = new FileOutputStream("tools/test.txt");
 byte[] myByteArray = null;
 fos.write(myByteArray);
 fos.close();
 System.out.println("Results = " + myByteArray);

And lastly using Inputstream and BufferedReader. This actually gave the content of the file when running the program from Eclipse, but came out as null when running it as a jar (I am assuming that it is also not reading the file).

        InputStream is = null;  
        BufferedReader br = null;  
        String line;  
        ArrayList list = new ArrayList();  
        try {   
              is = Main.class.getResourceAsStream("tools/test.txt");  
              br = new BufferedReader(new InputStreamReader(is));  
              while (null != (line = br.readLine())) {  
                 list.add(line);  
                 System.out.println("Output:" + line);
              }      

              while (null == (line = br.readLine())) {  
                     System.out.println("Error loading file:" + line);
                  }      

        }  
        catch (Exception ef) {  
          ef.printStackTrace();  
          System.out.println("Output:" + ef);
        }  

So my question is, if I have a folder named "tools" and have a file called "test.txt", what code would I use to turn it into byte[] and still work when compiled into a Jar file?

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = Main.class.getResourceAsStream("/tools/test.txt");
    byte[] buffer = new byte[4096];
    for (;;) {
        int nread = in.read(buffer);
        if (nread <= 0) {
            break;
        }
        baos.write(buffer, 0, nread);
    }
    byte[] data = baos.toByteArray();
    String text = new String(data, "Windows-1252");
    Byte[] asByteObjects = new Byte[data.length];
    for (int i # 0; i < data.length: ++i) {
        asByteObjects[i] = data[i];
    }

Without the heading slash the path would be relative to the package of the class. A ByteArrayOutputStream serves to collect for a byte[].

If the bytes represent text is some encoding, one can turn it into a String. Here with Windows Latin-1.

have you tried Scanner.nextByte()? make a new scanner with the file you want to parse as the input and use a for loop to create your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM