0

I am doing this:

File file = new File(String.valueOf(getClass().getClassLoader().getResource("images/logo.png")));

in order to have the entire path file name.
But this instruction below fails:

byte[] fileContent = FileUtils.readFileToByteArray(file);

I am getting this exception:

java.io.FileNotFoundException: File 'file:\C:\Users\thomas\dev\workspace\myapp\target\classes\images\logo.png' does not exist

It's because it needs to have double backslash instead of single backslash (OS: Windows). Is there a way to get rid of this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
thomas
  • 1,201
  • 2
  • 15
  • 35
  • 2
    "It's because it needs to have double backslash instead of single backslash" — No, it has nothing to do with that. You misunderstand the purpose of escape sequences. The real reason is that the path does not exist, just as the error message says. – Konrad Rudolph Jan 10 '23 at 09:35
  • the file does exist. I have tried with C:\\Users\\thomas\\dev\\workspace\\myapp\\target\\classes\\images\\logo.png and it works. – thomas Jan 10 '23 at 09:37
  • 2
    Remove the `file:\ ` in front of the `file` string. But this code won't work once you bundle your code into a JAR file. You cannot treat resources as files (because they generally aren't!). You need to read it as a resource stream! See https://stackoverflow.com/a/20389418/1968 – Konrad Rudolph Jan 10 '23 at 09:38
  • 3
    Are you aware that a string **literal** `"C:\\Users\\thomas"` represents the actual String **value** `C:\Users\Thomas`? In other words, your idea that you need double backslashes is a red herring. The problem is the presence of `file:\C:\...`, which should be just `C:\...`. And that is because resources are not (necessarily) files! If you want to get data from a resource, use `getResourceAsStream`. – Mark Rotteveel Jan 10 '23 at 10:27
  • 1
    You are asking the wrong question. File `logo.png` obviously contains an [Image](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/Image.html). You apparently want to create an actual `Image` from that file and do something with the created `Image` - maybe use it as an [Icon](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) in a _Swing_ application. There are other ways to create an `Image` besides converting the file to a `byte` array. I believe that you really want to know how to convert your "resource" to an `Image`. – Abra Jan 10 '23 at 11:14
  • yes , I want to insert the image in a thymeleaf email template. I couldnt figure out how to do that. – thomas Jan 11 '23 at 09:08

1 Answers1

1

You are very close. You probably want the getResourceAsStream function. As noted in the comments, you do NOT want to use File for this, as it will reference things on the file system (things that will not be there once you build & deploy your app).

A simple demo of reading a Java resource is below:

    public void readResourceBytes()
    {
        final String resourceName = "images/logo.png";
        final ByteArrayOutputStream mem = new ByteArrayOutputStream();
        try (final InputStream is = getClass().getClassLoader().getResourceAsStream(resourceName))
        {
            copy(is, mem);
        }

        byte[] fileContent = mem.toByteArray();
        System.out.println(mem.toByteArray());

        // Or, maybe you just want the ASCII text
        // String fileContent = mem.toString();
        // System.out.println(mem.toString());
    }

    // OK for binary files, not OK for text, should use Reader/Writer instead
    public static void copy(final InputStream from, final OutputStream to) throws IOException
    {
        final int bufferSize = 4096;

        final byte[] buf = new byte[bufferSize];
        int len;
        while (0 < (len = from.read(buf)))
        {
            to.write(buf, 0, len);
        }
        to.flush();
    }
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49