Check if file exists at given path using Java and create if NOT
Problem statement : Want to check if a file exists or not at given location and create if not to avoid FileNotFoundexception
Solution : using Java
public void createFileIfDoesNotExists() throws IOException {
File usersHTMLFile = new File(System.getPr...
qa-amitsahu.hashnode.dev1 min read
Nice! I like to use the
Filesutility class, look:import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String... args) throws IOException { final Path path = Paths.get(System.getProperty("user.dir"), "Report.html"); if (Files.notExists(path)) Files.createFile(path); } }