简体   繁体   English

如何将正确的相对路径写入文件

[英]How to write the correct relative path to the file

I'm doing an exercise where it picks and displays three random cards from a file of card images stored in lib/cards/cards of the project.我正在做一个练习,它从存储在项目的 lib/cards/cards 中的卡片图像文件中挑选并显示三张随机卡片。 Card images are named from 1.png to 52.png.卡片图像的命名从 1.png 到 52.png。 My java file is in src which has the same level with lib Here's the code I used.我的 java 文件在 src 中,它与 lib 具有相同级别这是我使用的代码。

Random rd = new Random();
int[] card = new int[3];
//randomize cards
card[0] = rd.nextInt(52) + 1;
card[1] = rd.nextInt(52) + 1;
card[2] = rd.nextInt(52) + 1;
ImageView image_0 = new ImageView("lib/cards/cards/"+ Integer.toString(card[0]) + ".png");
ImageView image_1 = new ImageView("D:/Visual Studio/New/Test/lib/cards/cards/"+ Integer.toString(card[1]) + ".png");
ImageView image_2 = new ImageView("D:/Visual Studio/New/Test/lib/cards/cards/"+ Integer.toString(card[2]) + ".png");

Absolute path works just fine for image_1 and image_2, and relative path doesn't work for image_0.绝对路径适用于 image_1 和 image_2,而相对路径不适用于 image_0。 I wonder if I can use relative path for this, and if I can how to write the correct relative path for this to work.我想知道我是否可以为此使用相对路径,以及是否可以为此编写正确的相对路径。

try adding./ in the beginning of the path if the image in the same folder as the file u are in ex if your current file is in lib and the image is in lib type./nameOfImage if u want to go one level app (AKA if the image in the parent folder ) add../ to the beginning of the path ex if your current file is in lib and your image is in the Test folder to go to it your file will be../nameOfImage如果您当前的文件在 lib 中并且图像在 lib 类型中,则尝试在路径的开头添加 ./ 如果与文件在同一文件夹中的图像在 ex 中。 AKA 如果父文件夹中的图像)添加../ 到路径的开头 ex 如果您的当前文件在 lib 中,并且您的图像在 go 的 Test 文件夹中,那么您的文件将是../nameOfImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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