user5864689
user5864689

Reputation: 39

How to save a file with custom extension?

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

Answers (2)

Julian Rubin
Julian Rubin

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

Keno
Keno

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.

Additional Resource

Upvotes: 1

Related Questions