java 写一个HelloJavaWorld你好世界输出到操作系统文件Hello.txt文件中
生活随笔
收集整理的這篇文章主要介紹了
java 写一个HelloJavaWorld你好世界输出到操作系统文件Hello.txt文件中
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.beiwo.homework;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;public class demo5 {/*** 在程序中寫一個(gè)"HelloJavaWorld你好世界"輸出到操作系統(tǒng)文件Hello.txt文件中** 程序分析:文件寫入,要用到輸出流FileOutputStream* 步驟:* 1.找到目標(biāo)文件* 2.建立通道* 3.寫入數(shù)據(jù)* 4.關(guān)閉資源* */public static void main(String[] args) {// 向文件C:/Hello.txt,寫入內(nèi)容File file = new File("C:\\Users\\cdlx2016\\Desktop\\Hello.txt");FileOutputStream fos = null;try {// 創(chuàng)建輸出流fos = new FileOutputStream(file);// 把String類型的字符串轉(zhuǎn)化為byte數(shù)組的數(shù)據(jù)保存在輸出流中fos.write("HelloJavaWorld你好世界".getBytes());} catch (Exception e) {System.out.println("寫入異常");throw new RuntimeException(e);} finally {try {// 關(guān)閉輸出流
fos.close();} catch (IOException e) {// TODO 自動(dòng)生成的 catch 塊System.out.println("關(guān)閉失敗");throw new RuntimeException(e);}}}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/Liang-Haishan216/p/6136489.html
總結(jié)
以上是生活随笔為你收集整理的java 写一个HelloJavaWorld你好世界输出到操作系统文件Hello.txt文件中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。