简体   繁体   中英

Read an image file within the Eclipse Project in java

I am working on a simple 2D Sidescroller and I use this :

bgData = new File("C:\\\\Users\\\\User\\\\Desktop\\\\Folder\\\\" + "Workspace\\\\Sidescroller\\\\src\\\\data\\\\bg.png");

Since it reads a file within the code base, why cant I access it?

Exception got:

javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(Unknown Source)

Your actual problem is that you're using \\ char as part of the path.

Instead try changing:

bgData = new File("C:\\Users\\User\\Desktop\\Folder\\" 
+ "Workspace\\Sidescroller\\src\\data\\bg.png");

for

bgData = new File("C:/Users/User/Desktop/Folder/"
+ "Workspace/Sidescroller/src/data/bg.png");

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