最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java_IO向文件中寫入和讀取內(nèi)容代碼實(shí)例

 更新時(shí)間:2019年03月25日 10:29:54   作者:非凡_m  
這篇文章主要介紹了java_IO向文件中寫入和讀取內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用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) {
		//定義文件路徑,沒有該文件會(huì)自動(dòng)創(chuàng)建,如果路徑有文件夾,一定要有,不會(huì)自動(dò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);	//實(shí)例化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);		//實(shí)例化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é)束
	}
}

以上所述是小編給大家介紹的java_IO向文件中寫入和讀取內(nèi)容詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

龙游县| 公安县| 江都市| 安西县| 甘南县| 安西县| 秦皇岛市| 凤冈县| 莱州市| 全椒县| 巴中市| 禹州市| 开化县| 沛县| 皮山县| 蓬安县| 台北县| 余庆县| 禄劝| 东乡族自治县| 安达市| 沙洋县| 阿瓦提县| 获嘉县| 托里县| 恭城| 秀山| 精河县| 同心县| 惠水县| 台山市| 余江县| 夏河县| 宣化县| 自治县| 延寿县| 卢湾区| 阿拉善左旗| 两当县| 微山县| 岚皋县|