国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Java操作文件輸出為字符串以及字符串輸出為文件的方法

2024-07-14 08:41:42
字體:
供稿:網(wǎng)友

文件輸出為字符串示例代碼:

/**  * 讀取文件為字符串  *   * @return  */public static String readString() {  String str = "";  File file = new File("C:/Users/wan7/Desktop/表單/粗集料試驗/粗集料沖擊值試驗(T0322-2000).html");  try {   FileInputStream in = new FileInputStream(file);   // size 為字串的長度 ,這里一次性讀完   int size = in.available();   byte[] buffer = new byte[size];   in.read(buffer);   in.close();   str = new String(buffer, "utf-8");  } catch (IOException e) {   return null;  }  return str; }

字符串輸出為文件示例代碼:

 /**  * 輸出到文件  */ public static void outFile(String s) {  File file = new File("C:/Users/wan7/Desktop/11111111111.html");  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 = s.getBytes();   fop.write(contentInBytes);   fop.flush();   fop.close();   System.out.println("Done");  } catch (IOException e) {   e.printStackTrace();  } }

以上這篇Java操作文件輸出為字符串以及字符串輸出為文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 张家港市| 建水县| 西乌珠穆沁旗| 左贡县| 巴南区| 哈巴河县| 葵青区| 舒兰市| 工布江达县| 乐昌市| 宜章县| 怀柔区| 肇东市| 开化县| 大理市| 子长县| 金山区| 蒲江县| 襄樊市| 卢湾区| 景东| 阿勒泰市| 揭东县| 武邑县| 浏阳市| 巨鹿县| 团风县| 松溪县| 凤山县| 财经| 商水县| 常熟市| 富蕴县| 大城县| 东乌珠穆沁旗| 安塞县| 天镇县| 陇西县| 老河口市| 大英县| 原阳县|