Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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?

like image 565
steven Avatar asked Jul 17 '26 15:07

steven


2 Answers

    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.


Since java 9 use

InputStream.readAllBytes()

Thanks for @naugler

like image 63
Joop Eggen Avatar answered Jul 19 '26 04:07

Joop Eggen


You could use Apache Commons File:

org.apache.commons.io.IOUtils.resourceToByteArray("/tools/test.txt");
like image 22
Oscar Pérez Avatar answered Jul 19 '26 06:07

Oscar Pérez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!