奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

java語言

java寫入文件的方法有哪些

時間:2024-10-05 20:29:46 java語言 我要投稿
  • 相關推薦

java寫入文件的方法有哪些

  java寫入文件的方法有哪些?下面百分網小編帶大家一起來看看詳細內容,有需要的朋友們一起看看吧!想了解更多相關信息請持續關注我們應屆畢業生考試網!

  一,FileWritter寫入文件

  FileWritter, 字符流寫入字符到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作為FileWritter構造函數的第二個參數,它會保留現有的內容,并追加新內容在文件的末尾。

  1. 替換所有現有的內容與新的內容。

  new FileWriter(file);2. 保留現有的內容和附加在該文件的末尾的新內容。

  復制代碼 代碼如下:

  new FileWriter(file,true);

  追加文件示例

  一個文本文件,命名為“javaio-appendfile.txt”,并包含以下內容。

  ABC Hello追加新內容 new FileWriter(file,true)

  復制代碼 代碼如下:

  package com.yiibai.file;

  import java.io.File;

  import java.io.FileWriter;

  import java.io.BufferedWriter;

  import java.io.IOException;

  public class AppendToFileExample

  {

  public static void main( String[] args )

  {

  try{

  String data = " This content will append to the end of the file";

  File file =new File("javaio-appendfile.txt");

  //if file doesnt exists, then create it

  if(!file.exists()){

  file.createNewFile();

  }

  //true = append file

  FileWriter fileWritter = new FileWriter(file.getName(),true);

  BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

  bufferWritter.write(data);

  bufferWritter.close();

  System.out.println("Done");

  }catch(IOException e){

  e.printStackTrace();

  }

  }

  }

  結果

  現在,文本文件“javaio-appendfile.txt”內容更新如下:

  ABC Hello This content will append to the end of the file

  二,BufferedWriter寫入文件

  緩沖字符(BufferedWriter )是一個字符流類來處理字符數據。不同于字節流(數據轉換成字節),你可以直接寫字符串,數組或字符數據保存到文件。

  復制代碼 代碼如下:

  package com.yiibai.iofile;

  import java.io.BufferedWriter;

  import java.io.File;

  import java.io.FileWriter;

  import java.io.IOException;

  public class WriteToFileExample {

  public static void main(String[] args) {

  try {

  String content = "This is the content to write into file";

  File file = new File("/users/mkyong/filename.txt");

  // if file doesnt exists, then create it

  if (!file.exists()) {

  file.createNewFile();

  }

  FileWriter fw = new FileWriter(file.getAbsoluteFile());

  BufferedWriter bw = new BufferedWriter(fw);

  bw.write(content);

  bw.close();

  System.out.println("Done");

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  三,FileOutputStream寫入文件

  文件輸出流是一種用于處理原始二進制數據的字節流類。為了將數據寫入到文件中,必須將數據轉換為字節,并保存到文件。請參閱下面的完整的例子。

  復制代碼 代碼如下:

  package com.yiibai.io;

  import java.io.File;

  import java.io.FileOutputStream;

  import java.io.IOException;

  public class WriteFileExample {

  public static void main(String[] args) {

  FileOutputStream fop = null;

  File file;

  String content = "This is the text content";

  try {

  file = new File("c:/newfile.txt");

  fop = new FileOutputStream(file);

  // if file doesnt exists, then create it

  if (!file.exists()) {

  file.createNewFile();

  }

  // get the content in bytes

  byte[] contentInBytes = content.getBytes();

  fop.write(contentInBytes);

  fop.flush();

  fop.close();

  System.out.println("Done");

  } catch (IOException e) {

  e.printStackTrace();

  } finally {

  try {

  if (fop != null) {

  fop.close();

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  }

  //更新的JDK7例如,使用新的“嘗試資源關閉”的方法來輕松處理文件。

  package com.yiibai.io;

  import java.io.File;

  import java.io.FileOutputStream;

  import java.io.IOException;

  public class WriteFileExample {

  public static void main(String[] args) {

  File file = new File("c:/newfile.txt");

  String content = "This is the text content";

  try (FileOutputStream fop = new FileOutputStream(file)) {

  // if file doesn't exists, then create it

  if (!file.exists()) {

  file.createNewFile();

  }

  // get the content in bytes

  byte[] contentInBytes = content.getBytes();

  fop.write(contentInBytes);

  fop.flush();

  fop.close();

  System.out.println("Done");

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

【java寫入文件的方法有哪些】相關文章:

Java寫入文件的方法05-24

學習java的方法有哪些07-01

java讀文件寫文件的方法06-24

Java讀取、寫入文件如何解決亂碼問題08-24

Java讀取郵件的方法有哪些05-31

java調用bat文件的方法08-09

java序列化的方法有哪些10-06

JAVA中終止線程的方法有哪些07-26

講述java讀取properties文件的方法08-13

關于java調用bat文件的方法08-12

主站蜘蛛池模板: 武胜县| 苍溪县| 政和县| 资源县| 兴城市| 慈利县| 宜宾市| 建湖县| 武夷山市| 大余县| 安国市| 大埔县| 盐亭县| 平罗县| 莲花县| 丽水市| 濮阳市| 大埔县| 甘孜县| 将乐县| 塘沽区| 东海县| 桓台县| 忻城县| 林州市| 通州市| 务川| 习水县| 聂荣县| 略阳县| 西吉县| 涿鹿县| 昭通市| 东莞市| 延庆县| 巴彦淖尔市| 柳江县| 宁晋县| 剑阁县| 北川| 乐至县|