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