如何创建文件并用Java写入?

如何创建文件并用Java写入?

在Java中创建和写入(文本)文件最简单的方法是什么?



慕码人8056858
浏览 1074回答 4
4回答

江户川乱折腾

创建文本文件:PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");writer.println("The first line");writer.println("The second line");writer.close();创建二进制文件:byte data[] = ...FileOutputStream out = new FileOutputStream("the-file-name");out.write(data);out.close();Java 7+用户可以使用Files该类写入文件:创建文本文件:List<String> lines = Arrays.asList("The first line", "The second line");Path file = Paths.get("the-file-name.txt");Files.write(file, lines, Charset.forName("UTF-8"));//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);创建二进制文件:byte data[] = ...Path file = Paths.get("the-file-name");Files.write(file, data);//Files.write(file, data, StandardOpenOption.APPEND);

一只萌萌小番薯

在Java 7及更高版本中:try&nbsp;(Writer&nbsp;writer&nbsp;=&nbsp;new&nbsp;BufferedWriter(new&nbsp;OutputStreamWriter( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;FileOutputStream("filename.txt"),&nbsp;"utf-8")))&nbsp;{ &nbsp;&nbsp;&nbsp;writer.write("something");}但是有一些有用的实用程序:来自commons-io的FileUtils.writeStringtoFile(..)来自番石榴的Files.write(..)另请注意,您可以使用a&nbsp;FileWriter,但它使用默认编码,这通常是一个坏主意 - 最好明确指定编码。以下是Java 7之前的原始答案Writer&nbsp;writer&nbsp;=&nbsp;null;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;writer&nbsp;=&nbsp;new&nbsp;BufferedWriter(new&nbsp;OutputStreamWriter( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;FileOutputStream("filename.txt"),&nbsp;"utf-8")); &nbsp;&nbsp;&nbsp;&nbsp;writer.write("Something");}&nbsp;catch&nbsp;(IOException&nbsp;ex)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Report}&nbsp;finally&nbsp;{ &nbsp;&nbsp;&nbsp;try&nbsp;{writer.close();}&nbsp;catch&nbsp;(Exception&nbsp;ex)&nbsp;{/*ignore*/}}另请参阅:读取,写入和创建文件(包括NIO2)。

尚方宝剑之说

如果您已经拥有要写入文件的内容(而不是动态生成),则java.nio.file.FilesJava 7中作为本机I / O的一部分添加提供了实现目标的最简单,最有效的方法。基本上创建和写入文件只是一行,而且一个简单的方法调用!以下示例创建并写入6个不同的文件以展示如何使用它:Charset&nbsp;utf8&nbsp;=&nbsp;StandardCharsets.UTF_8;List<String>&nbsp;lines&nbsp;=&nbsp;Arrays.asList("1st&nbsp;line",&nbsp;"2nd&nbsp;line");byte[]&nbsp;data&nbsp;=&nbsp;{1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5};try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file1.bin"),&nbsp;data); &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file2.bin"),&nbsp;data, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StandardOpenOption.CREATE,&nbsp;StandardOpenOption.APPEND); &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file3.txt"),&nbsp;"content".getBytes()); &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file4.txt"),&nbsp;"content".getBytes(utf8)); &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file5.txt"),&nbsp;lines,&nbsp;utf8); &nbsp;&nbsp;&nbsp;&nbsp;Files.write(Paths.get("file6.txt"),&nbsp;lines,&nbsp;utf8, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StandardOpenOption.CREATE,&nbsp;StandardOpenOption.APPEND);}&nbsp;catch&nbsp;(IOException&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();}

九州编程

public&nbsp;class&nbsp;Program&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;text&nbsp;=&nbsp;"Hello&nbsp;world"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedWriter&nbsp;output&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;file&nbsp;=&nbsp;new&nbsp;File("example.txt"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;=&nbsp;new&nbsp;BufferedWriter(new&nbsp;FileWriter(file)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.write(text); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(&nbsp;IOException&nbsp;e&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;finally&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(&nbsp;output&nbsp;!=&nbsp;null&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java