第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何創(chuàng)建文件并用Java寫入?

如何創(chuàng)建文件并用Java寫入?

慕碼人8056858 2019-05-24 15:37:01
如何創(chuàng)建文件并用Java寫入?在Java中創(chuàng)建和寫入(文本)文件最簡(jiǎn)單的方法是什么?
查看完整描述

4 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

創(chuàng)建文本文件:


PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");

writer.println("The first line");

writer.println("The second line");

writer.close();

創(chuàng)建二進(jìn)制文件:


byte data[] = ...

FileOutputStream out = new FileOutputStream("the-file-name");

out.write(data);

out.close();

Java 7+用戶可以使用Files該類寫入文件:


創(chuàng)建文本文件:


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);

創(chuàng)建二進(jìn)制文件:


byte data[] = ...

Path file = Paths.get("the-file-name");

Files.write(file, data);

//Files.write(file, data, StandardOpenOption.APPEND);


查看完整回答
反對(duì) 回復(fù) 2019-05-24
?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

在Java 7及更高版本中:

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("filename.txt"), "utf-8"))) {
   writer.write("something");}

但是有一些有用的實(shí)用程序:

另請(qǐng)注意,您可以使用a FileWriter,但它使用默認(rèn)編碼,這通常是一個(gè)壞主意 - 最好明確指定編碼。

以下是Java 7之前的原始答案


Writer writer = null;try {
    writer = new BufferedWriter(new OutputStreamWriter(
          new FileOutputStream("filename.txt"), "utf-8"));
    writer.write("Something");} catch (IOException ex) {
    // Report} finally {
   try {writer.close();} catch (Exception ex) {/*ignore*/}}

另請(qǐng)參閱:讀取,寫入和創(chuàng)建文件(包括NIO2)。


查看完整回答
反對(duì) 回復(fù) 2019-05-24
?
尚方寶劍之說(shuō)

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果您已經(jīng)擁有要寫入文件的內(nèi)容(而不是動(dòng)態(tài)生成),則java.nio.file.FilesJava 7中作為本機(jī)I / O的一部分添加提供了實(shí)現(xiàn)目標(biāo)的最簡(jiǎn)單,最有效的方法。

基本上創(chuàng)建和寫入文件只是一行,而且一個(gè)簡(jiǎn)單的方法調(diào)用!

以下示例創(chuàng)建并寫入6個(gè)不同的文件以展示如何使用它:

Charset utf8 = StandardCharsets.UTF_8;List<String> lines = Arrays.asList("1st line", "2nd line");byte[] data = {1, 2, 3, 4, 5};try {
    Files.write(Paths.get("file1.bin"), data);
    Files.write(Paths.get("file2.bin"), data,
            StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    Files.write(Paths.get("file3.txt"), "content".getBytes());
    Files.write(Paths.get("file4.txt"), "content".getBytes(utf8));
    Files.write(Paths.get("file5.txt"), lines, utf8);
    Files.write(Paths.get("file6.txt"), lines, utf8,
            StandardOpenOption.CREATE, StandardOpenOption.APPEND);} catch (IOException e) {
    e.printStackTrace();}


查看完整回答
反對(duì) 回復(fù) 2019-05-24
?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

public class Program {
    public static void main(String[] args) {
        String text = "Hello world";
        BufferedWriter output = null;
        try {
            File file = new File("example.txt");
            output = new BufferedWriter(new FileWriter(file));
            output.write(text);
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally {
          if ( output != null ) {
            output.close();
          }
        }
    }}


查看完整回答
反對(duì) 回復(fù) 2019-05-24
  • 4 回答
  • 0 關(guān)注
  • 1094 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)