I understand that a # in a URL denotes an anchor. However, the File.toURI() method escapes the # symbol and renders it as %23 (e.g. file:/Users/Will/Desktop/%23Test/Test.jpg). Because it is escaped, shouldn’t it be interpreted as a literal #, rather than a symbolic #?

After all, The internet standards track protocol for the URI says the following in section 2.4.3:

The character “#” is excluded because it is used to delimit a URI from a fragment identifier in URI references…[Several more examples of excluded characters are given]…Data corresponding to excluded characters must be escaped in order to be properly represented within a URI.

The # symbol is an excluded character. When returned from File.URI(), it is escaped. Therefore the protocol indicates that that it meets the requirements to be properly represented.

Even if that’s not reason enough, Java Swing is able to handle this, as in the following code:
//Setup paths<br /> String portraitPath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "#Test" + File.separator + "Test.jpg";<br /> String uriString = new File(portraitPath).toURI().toString(); //Among other things, # becomes %23<br /> <br /> //Setup view<br /> JFrame frame = new JFrame("Test");<br /> Panel panel = new JPanel();<br /> <br /> //Add label containing the image, rendered from a URL with an escaped # sign<br /> JLabel label = new JLabel(new ImageIcon(new URL(uriString)));<br /> panel.add(label);<br /> <br /> //Finalize everything<br /> frame.setContentPane(panel);<br /> frame.pack();<br /> frame.setVisible(true); //The image is successfully displayed!
Since Java Swing is able to display an image with an escaped # in the URL, it seems that PD4ML, being a Java library, ought to also be able to display an image with an escaped # in the URL.