文件輸出為字符串示例代碼:
/** * 讀取文件為字符串 * * @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操作文件輸出為字符串以及字符串輸出為文件的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選