最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何使用java中OutStream()向文件中寫入內(nèi)容

這篇文章將為大家詳細(xì)講解有關(guān)如何使用java中OutStream()向文件中寫入內(nèi)容,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

漢中網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),漢中網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為漢中1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的漢中做網(wǎng)站的公司定做!

使用java中OutStream()向文件中寫入內(nèi)容

package Stream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


public class OutStreamDemo01 {
	public static void main(String[] args) {
		//定義文件路徑,沒有該文件會自動創(chuàng)建,如果路徑有文件夾,一定要有,不會自動創(chuàng)建文件夾
		String filename = "e:"+File.separator+"a"+File.separator+"b.txt";
		File file = new File(filename);
		String str = "這些都將寫入文件中";
		byte[] b = str.getBytes();	//將字符串轉(zhuǎn)換成字節(jié)數(shù)
		OutputStream out = null;
		try {
			out = new FileOutputStream(file);	//實例化OutpurStream
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
		
		//寫入
		try {
			out.write(b);		//寫入
			out.close();		//關(guān)閉
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

使用InputStream()讀取文件中的內(nèi)容:

package Stream;
import java.io.*;;
public class InputStreamDemo01 {
	public static void main(String[] args) {
		File file = new File("e:"+File.separator+"a"+File.separator+"b.txt");
		byte[] b = new byte[(int)file.length()];//定義byte字節(jié)的長度
		InputStream in = null;
		int len = 0;
		try {		//處理異常
			in = new FileInputStream(file);		//實例化FileInputstream類
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		//輸出異常
		}
		try {
			len = in.read(b);		//讀取指定文件的內(nèi)容
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(new String(b,0,len));//將字節(jié)數(shù)組轉(zhuǎn)化成字符串輸出指定文件從0開始到len字節(jié)結(jié)束
	}
}

關(guān)于“如何使用java中OutStream()向文件中寫入內(nèi)容”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。


網(wǎng)站名稱:如何使用java中OutStream()向文件中寫入內(nèi)容
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/jchdhd.html