Creating a File
NOTE: Please know that this does overwrite the file if it does not exist
Click Here to see a list of file tutorials with Java
public void createFile(String fileContent, String filename, String location){ try { String pathtofile = location+"/"+filename; // This is location you want the file to be. // Example would be... '/Users/dan/Desktop/myfile.txt' File f = new File(location); // Create a file with that location if(!(f.exists)){ // If file does not already exist, create one. f.createNewFile(); } FileWriter writer = new FileWriter(f.getAbsoluteFile()); BufferedWriter bufWriter = new BufferedWriter(writer); // Writer to write on file bufWriter.write(fileContent); // Writing... bufWriter.close(); // Close the writer } catch (IOException e) { e.printStackTrace(); } }
Now when you want to create a file, all you must do is call the method with the correct parameters!
Also remember NOT to add a slash at the end of the file location!
Make it something like…
createFile("Random File Content!!!", "coolfile.txt", "Users/dan/Desktop");
For more file tutorials with Java click Here!