import java.io.File;
import java.io.IOException;
public class FileSeparator {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String repertoireCourant= System.getProperty("user.dir");
String absoluteFilePath = "";
absoluteFilePath = repertoireCourant+ File.separator + filename;
System.out.println("Chemin absolu : " + absoluteFilePath);
File file = new File(absoluteFilePath);
if (file.createNewFile()) {
System.out.println("Fichier crée!");
} else {
System.out.println("Fichier existe dèjà.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Chemin absolu : C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
Fichier crée!
import java.io.File;
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String repertoireCourant = System.getProperty("user.dir");
File file = new File(repertoireCourant, filename);
System.out.println("Chemin: " + file.getAbsolutePath());
if (file.createNewFile()) {
System.out.println("Fichier crée!");
} else {
System.out.println("Fichier existe dèjà.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Chemin: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
Fichier existe dèjà.
import java.io.File;Sortie:
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String repertoireCourant = System.getProperty("user.dir");
String cheminAbsolu = "";
String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("win") >= 0) {
//si windows
cheminAbsolu = repertoireCourant + "\\" + filename;
} else if (os.indexOf("nix") >= 0 ||
os.indexOf("nux") >= 0 ||
os.indexOf("mac") >= 0) {
//si unix or mac
cheminAbsolu = repertoireCourant + "/" + filename;
}else{
//os introuvable
cheminAbsolu = repertoireCourant + "/" + filename;
}
System.out.println("Chemin : " + cheminAbsolu);
File file = new File(cheminAbsolu);
if (file.createNewFile()) {
System.out.println("Fichier créé");
} else {
System.out.println("Fichier existant déjà");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Chemin: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txtRessources:
Fichier existe dèjà.
Please disable your ad blocker and refresh the window to use this website.