简体   繁体   中英

Creating a file without the full file path - java

I'm trying to use the File library to create instances of files so I can play the audio from them, my problem is if I switch the computer I'm using I have to switch the Filepath to the corresponding computer is there a way to not use the full path when making a new File?

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);


}

yes, it is, via command line parameter; when you invoke your jar via java -jar {jarname} you can pass some parameters like this:

java -jar myapp.jar argument_1, argument_2, argument_3.

in this case in args array in main methos there will be 3 string{"argument_1", "argument_2", "argument_3"}

you can use this way to pass folders path like this:

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);
}

and run your application like this:

java -jar myapp.jar "C://Users/ranvi/IdeaProjects/SpotifyRemake/src/Particle/Spotify/Music/" "C://Users/ranvi/IdeaProjects/Spotify/src/com/company/Music"

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