[java] Pb bizarre à la création d'un fichier

Résolu/Fermé
Shino_ Messages postés 3 Date d'inscription mercredi 20 mai 2009 Statut Membre Dernière intervention 20 mai 2009 - 20 mai 2009 à 19:55
Shino_ Messages postés 3 Date d'inscription mercredi 20 mai 2009 Statut Membre Dernière intervention 20 mai 2009 - 20 mai 2009 à 20:22
Bonjour,
J'ai un petit probléme quand je veux créer un nouveau fichier :
Je lui donne son nouveau nom donc ici en l'occurence"youhou", seul petit probléme, il me rajoute à la fin de chaque fichier que je crée une suite aléatoire de 6 caractéres... (pourquoi 6 ? je ne sais pas ...)
Si quelqu'un a une idée
public class test {

public static void newTempFile (String filename, File temp) throws IOException {
File fileTest=new File("/home/shino/Bureau/Lol/"+filename);
if (fileTest.exists()==true) { System.out.println("Existe déjà :-O)");}
if (temp.exists()==true){

temp = new File("/home/shino/Bureau/Lol/");
File.createTempFile(filename, ".tmp", temp); //throw IOException
}
else System.out.println("yata");
}



/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

File temp=new File("/home/shino/Bureau/Lol");
newTempFile("youhou",temp);

}
A voir également:

1 réponse

Shino_ Messages postés 3 Date d'inscription mercredi 20 mai 2009 Statut Membre Dernière intervention 20 mai 2009
20 mai 2009 à 20:22
Probléme résolu... en fait en créant un nouveau temp file, java génére une série de caractére aléatoire à la fin pour être sur de ne rien écraser... afin d'éviter ce probléme il faut forcer le nouveau nom en quelque sorte...
la ligne que j'ai changer est en gras si ca en interesse quelques uns..



public class test {

public static void newTempFile (String filename, File temp) throws IOException {
String s = new String();
s = filename;
File ff = new File("/home/shino/Bureau/Lol/"+filename);
File fileTest=new File("/home/shino/Bureau/Lol/"+filename);
if (fileTest.exists()==true) { System.out.println("Existe déjà :-O)");}
if (temp.exists()==true){

temp = new File("/home/shino/Bureau/Lol/");
File.createTempFile(s, ".tmp", temp).renameTo(ff); //throw IOException

}
else System.out.println("yata");
}



/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String s = new String();
s="youhou";
File temp=new File("/home/shino/Bureau/Lol");
/*moveTempFile("Jaaea",temp);*/
newTempFile(s,temp);

}
0