import java.io.File;
import java.io.IOException;
public class FileSeparator {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory= System.getProperty("user.dir");
String absoluteFilePath = "";
absoluteFilePath = currentdirectory + File.separator + filename;
System.out.println("Absolute Path: " + absoluteFilePath);
File file = new File(absoluteFilePath);
if (file.createNewFile()) {
System.out.println("File created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Absolute path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
File created!
import java.io.File;
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory = System.getProperty("user.dir");
File file = new File(currentdirectory, filename);
System.out.println("Path: " + file.getAbsolutePath());
if (file.createNewFile()) {
System.out.println("File created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
File already exists.
import java.io.File;Output:
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory = System.getProperty("user.dir");
String AbsolutePath = "";
String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("win") >= 0) {
//if windows
Absolutepath = currentdirectory + "\\" + filename;
} else if (os.indexOf("nix") >= 0 ||
os.indexOf("nux") >= 0 ||
os.indexOf("mac") >= 0) {
//if unix or mac
AbsolutePath = currentDirectory + "/" + filename;
}else{
//os not found
AbsolutePath = currentdirectory + "/" + filename;
}
System.out.println("Path: " + AbsolutePath);
File file = new File(AbsolutePath);
if (file.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("File already existing");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txtResources:
File already exists.
Please disable your ad blocker and refresh the window to use this website.