當前位置:首頁 » 編程語言 » java流的讀寫

java流的讀寫

發布時間: 2022-12-06 01:29:12

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

一、超類:


位元組流: InputStream(讀入流) OutputStream(寫出流)


字元流: Reader(字元 讀入流) Writer (字元寫出流)

二、文件操作流


位元組流: FileInputStream ,FileOutputStream


字元流: FileReader, FileWriter(用法與位元組流基本相同,不寫)

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


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


//2.創建文件讀入流對象


FileInputStream fis =new FileInputStream(file);


//3.定義結束標志,可用位元組數組讀取


int i =0 ;


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


//i 就是從文件中讀取的位元組,讀完後返回-1


}


//4.關閉流


fis.close();


//5.處理異常

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


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


//2.創建文件讀入流對象


FileOutputStream fos =new FileOutputStream(file);


//3.定義結束標志


fos.write(要寫出的位元組或者位元組數組);


//4.刷新和關閉流


fos.flush();


fos.close();


//5.處理異常

三、緩沖流:


位元組緩沖流: BufferedInputStream,BufferedOutputStream


字元緩沖流:BufferedReader ,BufferedWriter


緩沖流是對流的操作的功能的加強,提高了數據的讀寫效率。既然緩沖流是對流的功能和讀寫效率的加強和提高,所以在創建緩沖流的對象時應該要傳入要加強的流對象。

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


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


//2.創建文件讀入流對象


FileInputStream fis =new FileInputStream(file);


//3.創建緩沖流對象加強fis功能


BufferedInputStream bis =new BufferedInputStream(fis);


//4.定義結束標志,可用位元組數組讀取


int i =0 ;


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


//i 就是從文件中讀取的位元組,讀完後返回-1


}


//5.關閉流


bis.close();


//6.處理異常

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


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


//2.創建文件讀入流對象


FileOutputStream fos =new FileOutputStream(file);


//3.創建緩沖流對象加強fos功能


BufferedOutputStream bos=new BufferedOutputStream(fos);


//4.向流中寫入數據


bos.write(要寫出的位元組或者位元組數組);


//5.刷新和關閉流


bos.flush();


bos.close();


//6.處理異常

四、對象流


ObjectInputStream ,ObjectOutputStream


不同於以上兩種類型的流這里只能用位元組對對象進行操作原因可以看上篇的編碼表比照原理

ObjectOutputStream對象的序列化:


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


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

  1. 創建Person類


    注1:此類要實現Serializable介面,此介面為標志性介面


    注2:此類要有無參的構造函數


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


    class Person implements Serializable{


    public Person(){}


    }


    2.創建對象流對象


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


    ObjectOutputStream oos =new ObjectOutputStream(


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


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


    List<Person> list =new ArrayList<>();


    list.add(new Person());


    ...(可以添加多個)


    oos.writeObject(list);


    4.關閉流,處理異常


    oos.flush();


    oos.close();

五、轉換流:

這類流是用於將字元轉換為位元組輸入輸出,用於操作字元文件,屬於字元流的子類,所以後綴為reader,writer;前綴inputstream,outputstream;

注 :要傳入位元組流作為參賽


InputStreamReader: 字元轉換輸出流


OutputStreamWriter:字元轉換輸入流

//1.獲取鍵盤輸入的位元組流對象

inInputStream in =Stream.in;

/*2.用轉換流將位元組流對象轉換為字元流對象,方便調用字元緩沖流的readeLine()方法*/


InputStreamReader isr =new InputStreamReader(in);


/*5.創建字元轉換輸出流對象osw,方便把輸入的字元流轉換為位元組輸出到本地文件。*/


OutputStreamWriter osw =new OutputStreamWriter(new
FileOutputStream(new File("文件名")));



/*3.現在isr是字元流,可以作為參數傳入字元緩沖流中*/


BufferedReader br =new BufferedReader(isr);

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


String line =null;


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


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

}



註:InputStreamReader isr =new InputStreamReader(new "各種類型的位元組輸入流都行即是:後綴為InputStream就行");


OutputStreamWriter osw =new OutputStreamWriter(new
"後綴為OutputStream就行");

六、區別記憶


1.對象流是可以讀寫幾乎所有類型的只要是對象就行,而位元組字元流,只能讀寫單個位元組字元或者位元組字元數組,以上沒有讀寫位元組字元數組的;注意對象流只有位元組流!


2.字元和位元組循環讀入的結束條件int i=0; (i =fis.read())!=-1
用字元數組復制文件(fr 讀入流 ,fw寫出流),位元組流也是相同的用法

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,表示是位元組輸入流。


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


ObjectInputStream :操作對象的位元組輸入流

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


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


將位元組以字元形式輸出到控制台的字元緩沖流的寫法:


new BufferedWriter( new OutputStreamWriter(System.out))

② java中怎麼用io流讀寫文件

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;

publicclassTest14{
publicstaticvoidmain(String[]args)throwsIOException{
StringfPath="C:/test.txt";
//讀
BufferedReaderbr=newBufferedReader(newFileReader(fPath));
System.out.println(br.readLine());
br.close();////使用後記得關閉
//寫
BufferedWriterbw=newBufferedWriter(newFileWriter(fPath));
bw.write("寫一段話到文件里");
bw.flush();
bw.close();//使用後記得關閉
}
}

③ java中關於文件流的讀寫(Writer Reader)

public class BufferedReader extends Reader
{
public BufferedReader (Reader in)//構造方法
public class BufferedReader (Reader in,int sz)//sz 指定字元緩沖區長度
public String readLine()throws IOException//讀取一行字元串,輸入流結束時返回null
}

public class BufferedWriter extends Writer
{
public BufferedWriter (Writer out)//構造方法
public class BufferedWriter(Writer out,int sz)//sz 指定字元緩沖區長度
public void newLine()throws IOException//寫入一個換行符
}

④ java中關於文件流的讀寫(Writer Reader)

你好 我剛剛做了個例子 方便你看
class_writer class 這個例子是從 一個文件中讀取數據然後插入了資料庫中 你可以只看讀取與插入的過程 希望能幫到你.
public class writer {
public boolean writ(String str) {
boolean success=false;
str = str.replaceAll(" ", "").replaceAll("\"", "");
String[] strs = str.split(",");
BaseJDBC base = new BaseJDBC();
String ML = "";
Statement stmt;
Connection conn;
ML = "'"+strs[0].replace(" ", "").trim() + "','"
+ strs[1].replace(" ", "").trim() + "','"
+ strs[2].replace(" ", "").trim() + "','"
+ strs[3].replace(" ", "").trim() + "','"
+ strs[4].replace(" ", "").trim()+ "','"
+ strs[5].replace(" ", "").trim()+"','"
+ strs[6].replace(" ", "").trim()+"'";
String query = "INSERT INTO BANK_INFO VALUES(" + ML + ")";
if (!strs[0].equals("參與者行號")) {
try {
conn=base.genConn();
stmt = conn.createStatement();
int num=stmt.executeUpdate(query);
if(num==1)success=true;
stmt.close();
conn.close();
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return success;
}

//class readingclass
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
writer w=new writer();
try {
tempString.getBytes("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!w.writ(tempString)){
System.out.println("第"+line+"行出現異常:"+tempString);
}else{
System.out.println("第"+line+"行初始化成功!");
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\BankInfo_20110714094211.csv";
readFileByLines(fileName);

}

⑤ java 中簡述使用流進行讀寫文本文件的步驟

InputStream
三個基本的讀方法
abstract int read() : 讀取一個位元組數據,並返回讀到的數據,如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b) : 將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b, int off, int len) :將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。off指定在數組b中存放數據的起始偏移位置;len指定讀取的最大位元組數。
OutputStream
三個基本的寫方法
abstract void write(int b) :往輸出流中寫入一個位元組。
void write(byte[] b) :往輸出流中寫入數組b中的所有位元組。
void write(byte[] b, int off, int len) :往輸出流中寫入數組b中從偏移量off開始的len個位元組的數據。
其它方法
void flush() :刷新輸出流,強制緩沖區中的輸出位元組被寫出。
void close() :關閉輸出流,釋放和這個流相關的系統資源。

⑥ java緩沖流讀寫數據

---下面都是以位元組流方式操作---
//讀數據:
BufferedInputStreambis=newBufferedInputStream(newFileInputStream("xx.xx"));
byte[]b=newbyte[1024];
intlen=0;
while((len=bis.read(b))!-1){
//這樣就讀取並輸出了,如果是別的文件的話亂碼,因為二進制文件
System.out.println(newString(b,0,len));
}
bis.close();//關閉流,節省資源


//寫數據:
BufferedOutputStreambos=newBufferedOutputStream(newFileOuputStream("xx.xx"));
//使用緩沖區寫二進制位元組數據
bos.write("xxxxx".getBytes());
bos.close();//關閉流,節省資源

如果字元流的話就是:

BufferedReader //讀取

BufferedWriter //寫入

⑦ java中位元組流讀寫和字元流讀寫怎麼理解

位元組流與字元流主要的區別是他們的的處理方式
位元組流是最基本的,所有的InputStream和OutputStream的子類都是,主要用在處理二進制數據,它是按位元組來處理的
但實際中很多的數據是文本,又提出了字元流的概念,它是按虛擬機的encode來處理,也就是要進行字元集的轉化
這兩個之間通過 InputStreamReader,OutputStreamWriter來關聯,實際上是通過byte[]和String來關聯
在實際開發中出現的漢字問題實際上都是在字元流和位元組流之間轉化不統一而造成的

在從位元組流轉化為字元流時,實際上就是byte[]轉化為String時,
public String(byte bytes[], String charsetName)
有一個關鍵的參數字元集編碼,通常我們都省略了,那系統就用操作系統的lang
而在字元流轉化為位元組流時,實際上是String轉化為byte[]時,
byte[] String.getBytes(String charsetName)
也是一樣的道理

⑧ 如何使用Java 輸出/輸出流進行讀寫數據

在「面向對象編程:Java collection更有效管理elements」一文中,我們討論了Java 集合類架構中的類和功能並介紹了它的排序功能。在本文中,我們將學習Java 平台提供的這些I/O類,介面和操作。讓我們先從了解Java 數據流開始。 數據流 Java所有的I/O機制都是基於數據流的,這些數據流表示了字元或者位元組數據的流動序列。Java的I/O流提供了讀寫數據的標准方法。任何Java中表示數據源的對象都會提供以數據流的方式讀寫它的數據的方法。 Java.io是大多數面向數據流的輸入/輸出類的主要軟體包。這個軟體包包含了兩個抽象類,InputStream和OutputStream。所有其它面象數據流的輸入/輸出類都要擴展這兩個基類。 java.io軟體包提供了一些類和介面,它們在由InputStream和OuputStream類提供的讀寫操作的頂端定義了一些有用的抽象。例如,ObjectInputStream類提供了讓你把輸入/輸出流中的數據當成對象來讀取的方法,而ObjectOutputStream類提供了讓你能夠把Java對象寫入數據流中的方法。 優化讀寫過程 JDK 1.1 增加了一套讀寫類,它們提供了比現有數據流類更有用的抽象和更好的輸入/輸出性能。例如,BufferedReader和BufferedWriter 類被用來從基於字元的輸入和輸出流中讀取和寫入文本。BufferdReader 類緩存字元以更高效的讀取字元串,數組和文本行。BufferedWriter類緩存字元以更高效的寫入字元串,數組和文本行。BufferedReader和BufferedWriter 類可以按需求進行設置。 Java輸入/輸出架構提供的讀取器和寫入器類包括 LineNumberReader 類,CharArrayReader類,FileReader類,FilterReader類,PushbackReader類,PipedReader類,StringReader類以及其它一些類。這些類是在InputStream和OuputStream類頂部的包裹類因此提供了與InputStream和OuputStream類相似的方法。但是,這些類為讀寫特定的對象,比方文件,字元數組和字元串等等提供了更高效而有用的抽象。 讀取數據 當你從一個相應的數據源對象里提取輸入流或者是創建一個讀取器對象的時候就會自動打開一個輸入流。例如,要為一個文件打開輸入流,我們只需要以下面的方式把文件名傳遞給Java.io.FileReader對象的構造函數: java.io.FileReader fileReader = new java.io.FileReader("/home/me/myfile.txt".net/forum/images/smiles/icon_wink.gif border=0>; 要按順序讀取FileReader底層的輸入流中的一個位元組數據,只需要使用不帶參數的read方法。表A中的代碼段從一個文件讀取文本數據,一次一個字元,然後把它寫入System.out里。 要從輸入流讀取指定數目的位元組數據到char數組里,只需要使用帶一個char[]參數的read方法。數組的長度被用來確定應該讀取的字元的個數。表B演示了這個技術。 要關閉一個輸入流以及這個流使用的所有系統資源,你只需要以下面的方式調用close方法: fileReader.close(); 寫入數據 象一個輸入流一樣,輸出流通常在你從相應的數據源提取它或者是在你創建一個寫入對象的時候被自動的打開。例如,要為一個文件打開輸出流,我們把文件的名字傳遞給java.io.FileWriter對象的構造函數,如下所示: java.io.FileWriter fileWriter = new java.io.FileWriter("/home/me/out.txt".net/forum/images/smiles/icon_wink.gif border=0>; 要將一個特定的字元寫入到輸出流中,可以使用帶一個int參數的write方法,int參數代表要定入的字元。 int aChar = (int)'X'; fileWriter.write(aChar); 要在輸出流給定的偏移地址寫入一個char數組中特定數目的字元,你可以使用帶一個char[]參數,一個int 偏移量參數和一個int長度參數的write方法,如下面的例子所示: fileWriter.write(buffer, 0, byteCount); 要關閉一個輸出流並釋放所有與之相關的系統資源,可以使用close方法,就象這樣: fileWriter.close(); 要強迫寫出一個輸出流中的所有數據,可以使用下面的flush方法: fileWriter.flush(); 把它們全部綜合起來 我們可以使用我們學習過的這些函數從一個文件中讀取數據並同時寫到另一個文件中去,如表C所示。 總結Java的輸入/輸出機制為從不同的數據源讀取和寫入字元增加了一套簡單而標准化的API。你對一種數據源使用Java流的經驗能夠讓你容易的使用其它由Java提供的數據源類型。 在我們下一篇文章中,我們將會開始學習Java平台的聯網和遠程通訊架構。我們將會把我們對Java流的討論擴展到這些環境並演示如何打開遠程數據源,並象操作本地數據源,比方文件一樣,寫入數據和讀取數據

⑨ java 文件讀寫流

讀寫是兩個不同的分支,通常都是分開單獨使用的。
可以通過BufferedReader 流的形式進行流緩存,之後通過readLine方法獲取到緩存的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容
};
備註: 流用完之後必須close掉,如上面的就應該是:bre.close(),否則bre流會一直存在,直到程序運行結束。
可以通過「FileOutputStream」創建文件實例,之後過「OutputStreamWriter」流的形式進行存儲,舉例:
OutputStreamWriter pw = null;//定義一個流
pw = new OutputStreamWriter(new FileOutputStream(「D:/test.txt」),"GBK");//確認流的輸出文件和編碼格式,此過程創建了「test.txt」實例
pw.write("我是要寫入到記事本文件的內容");//將要寫入文件的內容,可以多次write
pw.close();//關閉流
備註:文件流用完之後必須及時通過close方法關閉,否則會一直處於打開狀態,直至程序停止,增加系統負擔。

⑩ java 中簡述使用流進行讀寫文本文件的步驟

一、Java IO學習基礎之讀寫文本文件

Java的IO操作都是基於流進行操作的,為了提高讀寫效率一般需要進行緩沖。

簡單的示常式序如下:

/**
* 讀出1.txt中的內容,寫入2.txt中
*
*/

import java.io.*;

public class ReadWriteFile{
public static void main(String[] args){
try{

File read = new File("c:\\1.txt");
File write = new File("c:\\2.txt");

BufferedReader br = new BufferedReader(
new FileReader(read));
BufferedWriter bw = new BufferedWriter(
new FileWriter(write));
String temp = null;
temp = br.readLine();
while(temp != null){
//寫文件
bw.write(temp + "\r\n"); //只適用Windows系統
//繼續讀文件
temp = br.readLine();
}

bw.close();
br.close();

}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
}

以上是一個比較簡單的基礎示例。本文上下兩部分都是從網上摘抄,合並在一起,方便下自己以後查找。

二、Java IO學習筆記+代碼

文件對象的生成和文件的創建

/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:10
*
* 文件對象的生成和文件的創建
*/
package study.iostudy;
import java.io.*;
public class GenerateFile
{
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject1 = new File("oneFirst.txt");
File fileObject2 = new File("d:\\mydir", "firstFile.txt");
System.out.println(fileObject2);
try
{
dirObject.mkdir();
}catch(SecurityException e)
{
e.printStackTrace();
}
try
{
fileObject2.createNewFile();
fileObject1.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
}
}

文件名的處理
/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:29
*
* 文件名的處理
*/
package study.iostudy;
import java.io.*;
/*
* 文件名的處理
* String getName(); 獲得文件的名稱,不包含文件所在的路徑。
* String getPath(); 獲得文件的路徑。
* String getAbsolutePath(); 獲得文件的絕對路徑。
* String getParent(); 獲得文件的上一級目錄的名稱。
* String renameTo(File newName); 按參數中給定的完整路徑更改當前的文件名。
* int compareTo(File pathName); 按照字典順序比較兩個文件對象的路徑。
* boolean isAbsolute(); 測試文件對象的路徑是不是絕對路徑。
*/
public class ProcesserFileName
{
public static void main(String[] args)
{
File fileObject1 = new File("d:\\mydir\\firstFile.txt");
File fileObject2 = new File("d:\\firstFile.txt");
boolean pathAbsolute = fileObject1.isAbsolute();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("There are some information of fileObject1's file name:");
System.out.println("fileObject1: " + fileObject1);
System.out.println("fileObject2: " + fileObject2);
System.out.println("file name: " + fileObject1.getName());
System.out.println("file path: " + fileObject1.getPath());
System.out.println("file absolute path: " + fileObject1.getAbsolutePath());
System.out.println("file's parent directory: " + fileObject1.getParent());
System.out.println("file's absoulte path: " + pathAbsolute);
int sameName = fileObject1.compareTo(fileObject2);
System.out.println("fileObject1 compare to fileObject2: " + sameName);
fileObject1.renameTo(fileObject2);
System.out.println("file's new name: " + fileObject1.getName());
}
}

測試和設置文件屬性
/*
* SetterFileAttribute.java
*
* Created on 2006年8月22日, 下午3:51
*
* 測試和設置文件屬性
*/
package study.iostudy;
import java.io.*;
public class SetterFileAttribute
{
/*
* File類中提供的有關文件屬性測試方面的方法有以下幾種:
* boolean exists(); 測試當前文件對象指示的文件是否存在。
* boolean isFile(); 測試當前文件對象是不是文件。
* boolean isDirectory(); 測試當前文件對象是不是目錄。
* boolean canRead(); 測試當前文件對象是否可讀。
* boolean canWrite(); 測試當前文件對象是否可寫。
* boolean setReadOnly(); 將當前文件對象設置為只讀。
* long length(); 獲得當前文件對象的長度。
*/
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject = new File("d:\\mydir\\firstFile.txt");
try
{
dirObject.mkdir();
fileObject.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("there are some information about property of file object:");
System.out.println("file object : " + fileObject);
System.out.println("file exist? " + fileObject.exists());
System.out.println("Is a file? " + fileObject.isFile());
System.out.println("Is a directory?" + fileObject.isDirectory());
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
long fileLen = fileObject.length();
System.out.println("file length: " +fileLen);
boolean fileRead = fileObject.setReadOnly();
System.out.println(fileRead);
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
}
}

文件操作方法
/*
* FileOperation.java
*
* Created on 2006年8月22日, 下午4:25
*
* 文件操作方法
*/

package study.iostudy;
import java.io.*;
/*
* 有關文件操作方面的方法有如下幾種:
* boolean createNewFile(); 根據當前的文件對象創建一個新的文件。
* boolean mkdir(); 根據當前的文件對象生成一目錄,也就是指定路徑下的文件夾
* boolean mkdirs(); 也是根據當前的文件對象生成一個目錄,
* 不同的地方在於該方法即使創建目錄失敗,
* 也會成功參數中指定的所有父目錄。
* boolean delete(); 刪除當前的文件。
* void deleteOnExit(); 當前Java虛擬機終止時刪除當前的文件。
* String list(); 列出當前目錄下的文件。
*/
public class FileOperation
* 找出一個目錄下所有的文件
package study.iostudy;
import java.io.*;
public class SearchFile
{
public static void main(String[] args)
{
File dirObject = new File("D:\\aa");
Filter1 filterObj1 = new Filter1("HTML");
Filter2 filterObj2 = new Filter2("Applet");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("list HTML files in directory: " + dirObject);
String[] filesObj1 = dirObject.list(filterObj1);
for (int i = 0; i < filesObj1.length; i++)
{
File fileObject = new File(dirObject, filesObj1[i]);
System.out.println(((fileObject.isFile())
? "HTML file: " : "sub directory: ") + fileObject);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String[] filesObj2 = dirObject.list(filterObj2);
for (int i = 0; i < filesObj2.length; i++)
{
File fileObject = new File(dirObject, filesObj2[i]);
System.out.println(((fileObject.isFile())
? "htm file: " : "sub directory: ") + fileObject);
}
}
}

class Filter1 implements FilenameFilter
{
String fileExtent;
Filter1(String extentObj)
{
fileExtent = extentObj;
}

public boolean accept(File dir, String name)
{
return name.endsWith("." + fileExtent);
}
}

class Filter2 implements FilenameFilter
{
String fileName;
Filter2(String fileName)
{
this.fileName = fileName;
字元流處理
* ProcesserCharacterStream.java
* 字元流處理
*
* java.io包中加入了專門用於字元流處理的類,這些類都是Reader和Writer類的子類,
* Reader和Writer是兩個抽象類,只提供了一系列用於字元流處理的介面,不能生成這
* 兩個類的實例。
* java.io包中用於字元流處理的最基本的類是InputStreamReader和OutputStreamWriter,
* 用來在位元組流和字元流之間作為中介。
*
* 下面是InputStreamReader類和OutputStreamWriter類的常用方法:
*
* public InputStreamReader(InputStream in)
* 根據當前平台預設的編碼規范,基於位元組流in生成一個輸入字元流。
* public InputStreamReader(InputStream in, String sysCode)throws UnSupportedEncodingException
* 按照參數sysCode指定的編碼規范,基於位元組流in構造輸入字元流,如果不支持參數sysCode中指定的編碼規范,就會產生異常。
* public OutputStreamWriter(OutputStream out)
* 根據當前平台預設的編碼規范,基於位元組流out生成一個輸入字元流。
* public OutputStreamWriter(OutputStream out, String sysCode) throws UnsupportedEncodingException
* 按照參數sysCode指定的編碼規范,基於位元組流out構造輸入字元流,如果不支持參數sysCode中指定的編碼規范,就會產生異常。
* public String getEncoding()
* 獲得當前字元流使用的編碼方式。
* public void close() throws IOException
* 用於關閉流。
* public int read() throws IOException
* 用於讀取一個字元。
* public int read(char[] cbuf, int off, int len)
* 用於讀取len個字元到數組cbuf的索引off處。
* public void write(char[] cbuf, int off, int len) throws IOException
* 將字元數組cbuf中從索引off處開始的len個字元寫入輸出流。
* public void write(int c) throws IOException
* 將單個字元寫入輸入流。
* public void write(String str, int off, int len) throws IOException
* 將字元串str中從索引off位置開始的ltn個字元寫入輸出流。
*
* 此外,為了提高字元流處理的效率,在Java語言中,引入了BufferedReader和BufferWriter類,這兩個類對字元流進行塊處理。
* 兩個類的常用方法如下:
* public BufferedReader(Reader in)
* 用於基於普通字元輸入流in生成相應的緩沖流。
* public BufferedReader(Reader in, int bufSize)
* 用於基於普通字元輸入流in生成相應的緩沖流,緩沖區大小為參數bufSize指定。
* public BufferedWriter(Writer out)
* 用於基於普通字元輸入流out生成相應的緩沖流。
* public BufferedWriter(Writer out, int bufSize)
* 用於基於普通字元輸入流out生在相應緩沖流,緩沖流大小為參數bufSize指定。
* public String readLine() throws IOException
* 用於從輸入流中讀取一行字元。
* public void newLine() throws IOException
* 用於向字元輸入流中寫入一行結束標記,值得注意的是,該標記不是簡單的換行符"\n",而是系統定義的屬性line.separator。
在上面的程序中,我們首先聲明了FileInputStream類對象inStream和
* FileOutputStream類的對象outStream,接著聲明了BufferInputStream
* 類對象bufObj、BufferedOutputStream類對象bufOutObj、
* DataInputStream類對象dataInObj以及PushbackInputStream類對象pushObj,
* 在try代碼塊中對上面這些對象進行初始化,程序的目的是通過BufferedInputStream
* 類對象bufInObj和BufferedOutputStream類對象bufOutObj將secondFile.txt
* 文件中內容輸出到屏幕,並將該文件的內容寫入thirdFile.txt文件中,值得注意的是,
* 將secondFile.txt文件中的內容輸出之前,程序中使用
* "System.out.println(dataInObj.readBoolean());" 語句根據readBoolean()結果
* 輸出了true,而secondFile.txt文件開始內容為「Modify」,和一個字元為M,
* 因此輸出的文件內容沒有「M」字元,thirdFile.txt文件中也比secondFile.txt
* 文件少第一個字元「M」。隨後,通過PushbackInputStream類對象pushObj讀取
* thirdFile.txt文件中的內容,輸出讀到的字元,當讀到的不是字元,輸出回車,將字元
* 數組pushByte寫回到thirdFile.txt文件中,也就是「ok」寫迴文件中。
* 對象串列化
* 對象通過寫出描述自己狀態的數值來記錄自己,這個過程叫做對象串列化。對象的壽命通
* 常是隨著生成該對象的程序的終止而終止,在有些情況下,需要將對象的狀態保存下來,然後
* 在必要的時候將對象恢復,值得注意的是,如果變數是另一個對象的引用,則引用的對象也要
* 串列化,串列化是一個遞歸的過程,可能會涉及到一個復雜樹結構的串列化,比如包括原有對
* 象,對象的對象等。
* 在java.io包中,介面Serializable是實現對象串列化的工具,只有實現了Serializable
* 的對象才可以被串列化。Serializable介面中沒有任何的方法,當一個類聲明實現Seriali-
* zable介面時,只是表明該類遵循串列化協議,而不需要實現任何特殊的方法。
* 在進行對象串列化時,需要注意將串列化的對象和輸入、輸出流聯系起來,首先通過對
* 象輸出流將對象狀態保存下來,然後通過對象輸入流將對象狀態恢復。

熱點內容
怎麼更改手機屏幕密碼 發布:2024-04-18 13:22:59 瀏覽:764
長期免費雲伺服器 發布:2024-04-18 13:15:05 瀏覽:626
java解析器 發布:2024-04-18 13:08:27 瀏覽:106
廣州公積金存摺密碼是多少 發布:2024-04-18 12:53:38 瀏覽:167
android第三方視頻 發布:2024-04-18 12:46:24 瀏覽:567
抽油機壓縮機 發布:2024-04-18 12:18:51 瀏覽:851
隱私圖片加密軟體 發布:2024-04-18 12:14:38 瀏覽:965
姓名筆畫算命怎麼演算法 發布:2024-04-18 11:46:13 瀏覽:99
像素工廠伺服器地址推薦 發布:2024-04-18 11:44:50 瀏覽:518
貴州電腦伺服器託管雲伺服器 發布:2024-04-18 11:26:36 瀏覽:738