mercredi 4 novembre 2015

Generate file new to jar

I have a jar with my application that should create a file next to it. So in folder I will have this :

 |_ MyApplication.jar
 |_ generatedFile.txt

Easy thing I thought... nope.. I am lost... I have a code like this:

URL location = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
        String path = location.getFile().substring(0, location.getFile().lastIndexOf("/MyContext"));
        File file = new File(fileName + ".txt");
        File file1 = new File(path + "/MyFileName.txt");
        File file2 = new File(path.substring(1) + "/MyFileName.txt");

I tried different combinations, googled alot and I am lost... if I get for example


and so on, the paths are correct... but the file isn't generated... Only working case is the first one, but that is located inside the jar and I don't want that.

I also tried to moving the existing file outside using


but that hasn't helped me at all..

Can someone help me with this ? And explain to me why isn't the examples above working ? Thanks..

Aucun commentaire:

Enregistrer un commentaire