在Java中創建文件的方式有以下幾種:
File file = new File("example.txt");
file.createNewFile();
FileOutputStream fos = new FileOutputStream("example.txt");
// 寫入數據到文件
fos.write("Hello, world!".getBytes());
fos.close();
BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"));
writer.write("Hello, world!");
writer.close();
String content = "Hello, world!";
Files.write(Paths.get("example.txt"), content.getBytes());
RandomAccessFile raf = new RandomAccessFile("example.txt", "rw");
raf.writeBytes("Hello, world!");
raf.close();