Reputation: 39
I am working on a project which creates a file with .bat
extension.
In C++ it is possible by using
fstream myfile ("example.bat");
fstream<<"echo \"hello\" ";
But how can I do it in Java (JDK 8)?
Upvotes: 1
Views: 378
Reputation: 1235
I think this code does what you want. createFile
will throw FileAlreadyExistsException
if specified file already exists.
Path myBat = Paths.get("example.bat");
Files.createFile(myBat);
try( BufferedWriter reader = Files.newBufferedWriter(myBat) ) {
reader.append("hello");
}
Edit:
I think try-with-resources is needed here.
Upvotes: 1
Reputation: 2098
I would suggest reading up on Java File IO. Whatever you want to call the file you're saving, you can. There aren't any custom extensions as opposed to normal extensions.
Upvotes: 1