[英]Creating a file without the full file path - java
我正在尝试使用文件库来创建文件实例,以便我可以播放它们中的音频,我的问题是如果我切换我正在使用的计算机我必须将文件路径切换到相应的计算机有没有办法制作新文件时不使用完整路径?
public class Main {
public static String[] pathnames;
public static void main(String[] args) {
String FilePaths;
File[] fileList = new File[5];
//Finds the music file
File musicFile = new File("C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/Heartless.wav");
File musicFile1 = new File("C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/Life Is Good.wav");
File musicFile2 = new File("C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/Panini.wav");
File musicFile3 = new File("C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/ROXANNE.wav");
File musicDir = new File("/Users/ranvi/IdeaProjects/Spotify/src/com/company/Music");
//Calls the method PlaySound
PlaySound(musicFile);
}
是的,它是通过命令行参数; 当你通过 java -jar {jarname} 调用你的 jar 时,你可以传递一些这样的参数:
java -jar myapp.jar argument_1、argument_2、argument_3。
在这种情况下,在主要方法的 args 数组中将有 3 个字符串{"argument_1", "argument_2", "argument_3"}
您可以使用这种方式传递文件夹路径,如下所示:
public class Main {
public static String[] pathnames;
public static void main(String[] args) {
String FilePaths;
File[] fileList = new File[5];
//Finds the music file
File musicFile = new File(args[0], "Heartless.wav");
File musicFile1 = new File(args[0], "Life Is Good.wav");
File musicFile2 = new File(args[0], "Panini.wav");
File musicFile3 = new File(args[0], "ROXANNE.wav");
File musicDir = new File(args[1]);
//Calls the method PlaySound
PlaySound(musicFile);
}
并像这样运行您的应用程序:
java -jar myapp.jar "C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/" "C://Users/ranvi/IdeaProjects/Spotify/src/com/company/Music"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.