io流技术java_技术文章-java中的IO流
1.File類
Java中對文件有操作時(shí),可以實(shí)例化一個(gè)File對象,將文件路徑利用這樣的形式賦給File對象。
File f = new File(filePath);
File類的基本操作包括:
判斷是否存在:f.exists()
獲取文件名:f.getName()
獲取其絕對路徑:f.getAbsolutePath()
但是如果涉及文件的查看與存儲(chǔ),就要利用IO的輸入和輸出流進(jìn)行操作。
2.輸入流:
常用的有InputStream類。
對于txt類文件查看的操作如下:
// 創(chuàng)建輸入流
InputStream input = new FileInputStream(file);
// 開辟一塊存儲(chǔ)讀入數(shù)據(jù)的緩存區(qū)域
byte[] contentBytes = new byte[input.available()];
// 讀取所有的字節(jié)到緩存中
for (int i = 0; i < contentBytes.length; i++) {
contentBytes[i] = (byte) input.read();
}
// 根據(jù)字節(jié)數(shù)組創(chuàng)建字符串
content = new String(contentBytes);
// 關(guān)閉流
input.close();
這樣就將txt文件中的內(nèi)容通過輸入流存入到名為content的字符串中,可以利用java進(jìn)行下一步操作。
但是輸入流只解決了查看等問題,如果要利用java改變txt文檔中的內(nèi)容,并存儲(chǔ)到原來的txt中,就要采用輸出流。
4.輸出流:
常用的有OutputStream類等。
以改變txt文檔中的內(nèi)容再將其存儲(chǔ)到原來的文檔中為例,步驟參考輸入流如下:
//創(chuàng)建輸出流
OutputStream output = new FileOutputStream(file);
//創(chuàng)建一個(gè)用來存儲(chǔ)讀取字符串?dāng)?shù)據(jù)的數(shù)組
byte[] contentBytes = new byte[content.getBytes().length];
//將存儲(chǔ)在字符串里的數(shù)據(jù)存入字節(jié)數(shù)組
contentBytes = content.replaceAll("\n", "\r\n").getBytes();
//因?yàn)镺utputStream中的寫出方法只能接收字節(jié)類型或者整型數(shù)據(jù)
output.write(contentBytes);
//刷新輸出流,寫出所有還在緩存中的字節(jié)
output.flush();
//關(guān)閉流
output.close();
例如如何制作一個(gè)文件查找器:
1.設(shè)計(jì)并制作出一個(gè)界面,其中包括最基本的輸入文件路徑框、搜索按鈕、顯示查找到的文件列表框。
2.利用讀取的輸入文件路徑,創(chuàng)建一個(gè)File類對象。再創(chuàng)建一個(gè)File類型的動(dòng)態(tài)數(shù)組,準(zhǔn)備將符合對象的所有文件存入其中用來輸出。創(chuàng)建一個(gè)數(shù)組,將讀取到的f文件對象下所有的文件列表存入其中。
List files = new ArrayList();
File f = new File(filePath);
File[] fl = f.listFiles();
3.將File對象進(jìn)行判斷是否存在、判斷是文件夾還是文件等操作,如果是文件夾,則將該文件夾再次調(diào)用文件查找的方法FileSearch(String filePath)。將讀取到的文件存入files動(dòng)態(tài)數(shù)組中。
for (int i = 0; i < fl.length; i++) {
if (fl[i].isDirectory()) {
for (File child1 : FileSearch(fl[i].getAbsolutePath()))
{
files.add(child1);
}
}
}
for
(File child1 : fl) {
files.add(child1);
}
4.在搜索結(jié)果框中輸入得到的files動(dòng)態(tài)數(shù)組中元素的絕對路徑。
for (File child : FileSearch(fp)) {
if
(flag == false) {
jtextarea.setText(child.getAbsolutePath());
}
}
5.加入文件名搜索功能:
新增一個(gè)文本框用來輸入文件名,將得到的文件名與fl中列舉的文件名進(jìn)行比較,符合要求的才加入到files動(dòng)態(tài)數(shù)組中用來顯示。
if (fl[i].getName().contains(fn) &&
!fn.equals("")) {
files.add(fl[i]);
}
6.加入點(diǎn)擊結(jié)果列表中的文件就能打開的功能:
1.普通的JTextArea不支持點(diǎn)擊返回點(diǎn)擊選項(xiàng)的功能,所以要利用JList組件。JList中存入的數(shù)據(jù)為Vector類型,故需要一個(gè)Vector動(dòng)態(tài)數(shù)組來存儲(chǔ)得到的files動(dòng)態(tài)數(shù)組。
Vector list = new
Vector();
for
(File child : FileSearch(fp)) {
list.addElement(child.getAbsolutePath());
}
console.setListData(list);
打開選取的文件:利用監(jiān)聽器返回得到的顯示在JList上的對象后,新建一個(gè)Desktop類用來打開選取的文件。使用open(文件名)的方法打開。
Desktop op = new Desktop();
op.open(filepath);
7.序列化與反序列化
什么是序列化?
一個(gè)對象產(chǎn)生之后實(shí)際上是在內(nèi)存上開辟一個(gè)存儲(chǔ)空間,方便存儲(chǔ)信息。對象序列化,就是把對象變成二進(jìn)制數(shù)據(jù)流的一種方法,方便保存和傳輸。
一個(gè)對象想被序列化,就需要繼承Serialiazable接口。
Serialiazable接口沒有任何方法,此接口是一個(gè)標(biāo)識(shí)接口,標(biāo)識(shí)具有某種能力。例如一個(gè)Person類的序列化標(biāo)識(shí):
import java.io.Serializable ;
public class Person implements
Serializable{
private static final long serialVersionUID
= 1L;?????? //版本聲明
private
String name ;????? // 聲明name屬性,但是此屬性不被序列化
private
int age ;?????????????? // 聲明age屬性
public
Person(String name,int age){?????? // 通過構(gòu)造設(shè)置內(nèi)容
this.name
= name ;
this.age
= age ;
}
public
String toString(){? // 覆寫toString()方法
return
"姓名:" + this.name + ";年齡:" + this.age
;
}
};
以后此對象就能被序列化為二進(jìn)制的btye流。
ObjectOutputStream序列化對象
傳入輸出對象:ObjectOutputStream(ObjectStream o)
輸出對象:writeObject(Object o)
public class SerDemo01{
public
static void main(String args[]) throws Exception {
File
f = new File("D:" + File.separator + "test.txt") ;??????? // 定義保存路徑
ObjectOutputStream
oos = null ;??? // 聲明對象輸出流
OutputStream
out = new FileOutputStream(f) ;??? // 文件輸出流
oos
= new ObjectOutputStream(out) ;
oos.writeObject(new
Person("張三",30)) ;???? // 保存對象
oos.close()
;???? // 關(guān)閉
}
};
ObjectInputStream反序列化對象
構(gòu)造輸入對象:ObjectInputStream(ObjectStream o)
從指定位置讀取對象:readObject()
public class SerDemo02{
public
static void main(String args[]) throws Exception {
File
f = new File(“地址”) ;?????? // 定義保存路徑
ObjectInputStream
ois = null ;??????? // 聲明對象輸入流
InputStream
input = new FileInputStream(f) ;??????? //
文件輸入流
ois
= new ObjectInputStream(input) ;??? // 實(shí)例化對象輸入流
Object
obj = ois.readObject() ;??????? // 讀取對象
ois.close()
;????? // 關(guān)閉
System.out.println(obj)
;
}
};
序列化一組對象最好使用數(shù)組接收
public class SerDemo05{
public
static void main(String args[]) throws Exception{
Person
per[] = {new Person("張三",30),new Person("李四",31),
new
Person("王五",32)} ;
ser(per)
;
Object
o[] = (Object[])dser() ;
for(int
i=0;i
Person
p = (Person)o[i] ;
System.out.println(p)
;
}
}
public
static void ser(Object obj[]) throws Exception {
File
f = new File("D:" + File.separator + "test.txt") ;??????? // 定義保存路徑
ObjectOutputStream
oos = null ;??? // 聲明對象輸出流
OutputStream
out = new FileOutputStream(f) ;??? // 文件輸出流
oos
= new ObjectOutputStream(out) ;
oos.writeObject(obj)
;???? // 保存對象
oos.close()
;???? // 關(guān)閉
}
public
static Object[] dser() throws Exception {
File
f = new File("D:" + File.separator + "test.txt") ;??????? // 定義保存路徑
ObjectInputStream
ois = null ;??????? // 聲明對象輸入流
InputStream
input = new FileInputStream(f) ;??????? //
文件輸入流
ois
= new ObjectInputStream(input) ;??? // 實(shí)例化對象輸入流
Object
obj[] = (Object[])ois.readObject() ;????? //
讀取對象
ois.close()
;????? // 關(guān)閉
return
obj ;
}
};
總結(jié)
以上是生活随笔為你收集整理的io流技术java_技术文章-java中的IO流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: im源码 php,thinkphpim即
- 下一篇: 斗鱼弹幕服务器未响应,斗鱼看不到弹幕的解