欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

io流怎么讀JAVA代碼,java使用io流讀寫文件

java怎么使用io流讀取一個文件夾里面

使用工具類Properties

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站制作、龍山網(wǎng)絡(luò)推廣、小程序制作、龍山網(wǎng)絡(luò)營銷、龍山企業(yè)策劃、龍山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供龍山建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:chinadenli.net

工具類有Load()方法 用于加載文件

首先將文件寫成流(輸入)

File file=new? File(confPath);

?in = new FileInputStream(file);

加載流load(in)如果需要操作則完成具體操作

輸出流并保存文件

out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

?cp.store(out2);

完成

具體實例代碼

public String updateConfParam(ConfParam cpl) throws IOException {

String error = null;

Properties cp=new Properties();

InputStream in= null;

OutputStreamWriter out1=null;

OutputStreamWriter out2=null;

JSONObject jObj = new JSONObject();

try {

?String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName());

?File file=new? File(confPath);

?in = new FileInputStream(file);? ?

?cp.load(in);

?out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK");

?cp.store(out1);

?cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()));

?cp.setProperty(cpl.getParaKey(), cpl.getParaValue());?? ?

?out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

?cp.store(out2);

?jObj.put("paraOldValue", cpl.getParaOldValue());

?jObj.put("paraValue", cpl.getParaValue());

} catch (FileNotFoundException e) {

?error=e.getMessage();

} catch (IOException e1) {

?error = e1.getMessage();

}

finally{

?if(in !=null){

??? ?in.close();

?}

?if(out1 !=null){

??? ?out1.close();

?}

?if(out2 !=null){

??? ?out2.close();

?}

?if(error != null){

??? ?jObj.put("error", error);

?}

}

return jObj.toString();

}

java的幾種IO流讀取文件方式

一、超類:

字節(jié)流: InputStream(讀入流) OutputStream(寫出流)

字符流: ? ?Reader(字符 讀入流) ? ?Writer (字符寫出流)

二、文件操作流

字節(jié)流: FileInputStream ?,F(xiàn)ileOutputStream

字符流: FileReader, FileWriter(用法與字節(jié)流基本相同,不寫)

//1.指定要讀 的文件目錄及名稱

File file =new File("文件路徑");

//2.創(chuàng)建文件讀入流對象

FileInputStream fis =new FileInputStream(file);

//3.定義結(jié)束標志,可用字節(jié)數(shù)組讀取

int i =0 ;

while((i = fis.read())!=-1){

//i 就是從文件中讀取的字節(jié),讀完后返回-1

}

//4.關(guān)閉流

fis.close();

//5.處理異常

//1.指定要寫到的文件目錄及名稱

File file =new File("文件路徑");

//2.創(chuàng)建文件讀入流對象

FileOutputStream fos =new FileOutputStream(file);

//3.定義結(jié)束標志

fos.write(要寫出的字節(jié)或者字節(jié)數(shù)組);

//4.刷新和關(guān)閉流

fos.flush();

fos.close();

//5.處理異常

三、緩沖流:

字節(jié)緩沖流: BufferedInputStream,BufferedOutputStream

字符緩沖流:BufferedReader ,BufferedWriter

緩沖流是對流的操作的功能的加強,提高了數(shù)據(jù)的讀寫效率。既然緩沖流是對流的功能和讀寫效率的加強和提高,所以在創(chuàng)建緩沖流的對象時應(yīng)該要傳入要加強的流對象。

//1.指定要讀 的文件目錄及名稱

File file =new File("文件路徑");

//2.創(chuàng)建文件讀入流對象

FileInputStream fis =new FileInputStream(file);

//3.創(chuàng)建緩沖流對象加強fis功能

BufferedInputStream bis =new BufferedInputStream(fis);

//4.定義結(jié)束標志,可用字節(jié)數(shù)組讀取

int i =0 ;

while((i = bis.read())!=-1){

//i 就是從文件中讀取的字節(jié),讀完后返回-1

}

//5.關(guān)閉流

bis.close();

//6.處理異常

//1.指定要寫到的文件目錄及名稱

File file =new File("文件路徑");

//2.創(chuàng)建文件讀入流對象

FileOutputStream fos =new FileOutputStream(file);

//3.創(chuàng)建緩沖流對象加強fos功能

BufferedOutputStream bos=new BufferedOutputStream(fos);

//4.向流中寫入數(shù)據(jù)

bos.write(要寫出的字節(jié)或者字節(jié)數(shù)組);

//5.刷新和關(guān)閉流

bos.flush();

bos.close();

//6.處理異常

四、對象流

ObjectInputStream ,ObjectOutputStream

不同于以上兩種類型的流這里只能用字節(jié)對對象進行操作原因可以看上篇的編碼表比照原理

ObjectOutputStream對象的序列化:

將java程序中的對象寫到本地磁盤里用ObjectOutputStream

eg:將Person類的對象序列化到磁盤

創(chuàng)建Person類

注1:此類要實現(xiàn)Serializable接口,此接口為標志性接口

注2:此類要有無參的構(gòu)造函數(shù)

注3:一旦序列化此類不能再修改

class Person implements Serializable{

public Person(){}

}

2.創(chuàng)建對象流對象

注:要增強功能可以將傳入文件緩沖流

ObjectOutputStream oos =new ObjectOutputStream(

new FileOutputStream(new File("文件路徑")));

3.寫入對象 ,一般會將對象用集合存儲起來然后直接將集合寫入文件

ListPerson list =new ArrayList();

list.add(new Person());

...(可以添加多個)

oos.writeObject(list);

4.關(guān)閉流,處理異常

oos.flush();

oos.close();

五、轉(zhuǎn)換流:

這類流是用于將字符轉(zhuǎn)換為字節(jié)輸入輸出,用于操作字符文件,屬于字符流的子類,所以后綴為reader,writer;前綴inputstream,outputstream;

注 :要傳入字節(jié)流作為參賽

InputStreamReader: 字符轉(zhuǎn)換輸出流

OutputStreamWriter:字符轉(zhuǎn)換輸入流

//1.獲取鍵盤輸入的字節(jié)流對象

inInputStream in =Stream.in;

/*2.用轉(zhuǎn)換流將字節(jié)流對象轉(zhuǎn)換為字符流對象,方便調(diào)用字符緩沖流的readeLine()方法*/

InputStreamReader isr =new InputStreamReader(in);

/*5.創(chuàng)建字符轉(zhuǎn)換輸出流對象osw,方便把輸入的字符流轉(zhuǎn)換為字節(jié)輸出到本地文件。*/

OutputStreamWriter osw =new OutputStreamWriter(new

FileOutputStream(new File("文件名")));

/*3.現(xiàn)在isr是字符流,可以作為參數(shù)傳入字符緩沖流中*/

BufferedReader br =new BufferedReader(isr);

/*4.可以調(diào)用字符緩沖流br的readLine()方法度一行輸入文本*/

String line =null;

while((line =br.readLine()){

osw.write(line);//osw是字符流對象,可以直接操作字符串

}

注:InputStreamReader isr =new InputStreamReader(new "各種類型的字節(jié)輸入流都行即是:后綴為InputStream就行");

OutputStreamWriter osw =new OutputStreamWriter(new

"后綴為OutputStream就行");

六、區(qū)別記憶

1.對象流是可以讀寫幾乎所有類型的只要是對象就行,而字節(jié)字符流,只能讀寫單個字節(jié)字符或者字節(jié)字符數(shù)組,以上沒有讀寫字節(jié)字符數(shù)組的;注意對象流只有字節(jié)流!

2.字符和字節(jié)循環(huán)讀入的結(jié)束條件int i=0; (i =fis.read())!=-1

用字符數(shù)組復(fù)制文件(fr 讀入流 ,fw寫出流),字節(jié)流也是相同的用法

int i = 0; ?char[] c = new char[1024];

while((i = fr.reade()) !=-1)){

fw.write(c,0,i);

}

123456

3.對象流里面套緩沖流的情景:

new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(“文件路徑”))));

4.記憶流及其功能的方法:

前綴表示功能,后綴表示流的類型;

比如說FileInputStream ?前綴:File,表示操作的磁盤,后綴:intputstream,表示是字節(jié)輸入流。

同理 FileReader:表示操作文件的字符流

ObjectInputStream :操作對象的字節(jié)輸入流

5.拓展:獲取鍵盤輸入的字符的緩沖流的寫法:

new BufferedReader(new InputStreamReader(System.in)));

將字節(jié)以字符形式輸出到控制臺的字符緩沖流的寫法:

new BufferedWriter( new OutputStreamWriter(System.out))

java中怎么用io流讀寫文件

import?java.io.BufferedReader;

import?java.io.BufferedWriter;

import?java.io.FileReader;

import?java.io.FileWriter;

import?java.io.IOException;

public?class?Test14?{

public?static?void?main(String[]?args)?throws?IOException?{

String?fPath?=?"C:/test.txt";

//?讀

BufferedReader?br?=?new?BufferedReader(new?FileReader(fPath));

System.out.println(br.readLine());

br.close();//?//?使用后記得關(guān)閉

//?寫

BufferedWriter?bw?=?new?BufferedWriter(new?FileWriter(fPath));

bw.write("寫一段話到文件里");

bw.flush();

bw.close();//?使用后記得關(guān)閉

}

}

分享題目:io流怎么讀JAVA代碼,java使用io流讀寫文件
分享路徑:http://chinadenli.net/article36/hdoisg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信公眾號、品牌網(wǎng)站制作、網(wǎng)站設(shè)計網(wǎng)頁設(shè)計公司、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作