【问题标题】:Adding variables to file path in java在java中将变量添加到文件路径
【发布时间】:2016-02-08 20:50:51
【问题描述】:

是否可以修改文件路径以使其包含字符串变量?

示例代码

String var = "Picture.jpg";
   ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\var");

【问题讨论】:

  • ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\" + var);
  • 天哪,我觉得自己很笨,这里的答案很明显。

标签: java string path


【解决方案1】:

使用String.format

String var = "Picture.jpg";
ImageIcon img = new ImageIcon(String.format("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\%s", var));

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多