㈠ 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的幾種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類的對象序列化到磁碟
創建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字元流的讀取和寫入是什麼
輸入和輸出操作的是字元,寫入和讀取的類是Writer和Reader,是抽象類,分別對相對應的子類FielWrite和FileReader類
關注我,跟一群朋友一起學習;
㈣ 幾種Java讀寫數據流性能對比
public static int FileOutputStreamTime = 0; 2 public static int BufferedOutputStreamTime = 0; 3 public static int FileWriterTime = 0; 4 public static int FileInputStreamTime = 0; 5 public static int BufferedInputStreamTime = 0; 6 public static int FileReaderTime = 0; 7 8 public static void write(String filePath, String content){ 9 FileOutputStream out = null; 10 FileOutputStream outStr = null; 11 BufferedOutputStream buf = null; 12 FileWriter fw = null; 13 File f = new File(filePath); 14 15 try { 16 //Test FileOutputStream 17 long begin1 = System.currentTimeMillis();
18 out = new FileOutputStream(f); 19 out.write(content.getBytes()); 20 out.close(); 21 long end1 = System.currentTimeMillis(); 22 FileOutputStreamTime += end1 - begin1; 23 24 //Test BufferedOutputStream 25 long begin2 = System.currentTimeMillis(); 26 outStr = new FileOutputStream(f); 27 buf = new BufferedOutputStream(outStr); 28 buf.write(content.getBytes()); 29 buf.flush(); 30 buf.close(); 31 long end2 = System.currentTimeMillis(); 32 BufferedOutputStreamTime += end2 - begin2; 33 34 //Test FileWriter 35 long begin3 = System.currentTimeMillis(); 36 // the second parameter "true",Whether or not a file will be covered 37 // or appended. 38 fw = new FileWriter(f); 39 // fw = new FileWriter("d:/test/testwrite/add2.txt",true); 40 fw.write(content); 41 fw.close(); 42 long end3 = System.currentTimeMillis(); 43 FileWriterTime += end3 - begin3; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } finally { 47 try { 48 fw.close(); 49 buf.close(); 50 outStr.close(); 51 out.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void read(String filePath){ 59 FileInputStream in = null; 60 BufferedInputStream buf = null; 61 FileReader reader = null; 62 BufferedReader br = null; 63 StringBuffer sb = new StringBuffer(); 64 65 try { 66 //Test FileInputStream 67 long begin1 = System.currentTimeMillis(); 68 File f = new File(filePath); 69 in = new FileInputStream(f); 70 int len1 = 512; 71 byte[] bytes1 = new byte[len1]; 72 73 while ((len1 = in.read(bytes1, 0, len1)) != -1) { 74 if(len1 < 512){ 75 byte[] tmpBuf = new byte[len1]; 76 System.array(bytes1, 0, tmpBuf, 0, len1);
77 sb.append(new String(tmpBuf)); 78 tmpBuf = null; 79 }else{ 80 sb.append(new String(bytes1)); 81 } 82 } 83 84 in.close(); 85 long end1 = System.currentTimeMillis(); 86 FileInputStreamTime += end1 - begin1; 87 88 //Test BufferedInputStream 89 long begin2 = System.currentTimeMillis(); 90 int len2 = 512; 91 byte[] bytes2 = new byte[len2]; 92 buf = new BufferedInputStream(new FileInputStream(f)); 93 while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94 if(len2 < 512){ 95 byte[] tmpBuf = new byte[len2]; 96 System.array(bytes2, 0, tmpBuf, 0, len2);
97 sb.append(new String(tmpBuf)); 98 tmpBuf = null; 99 }else{100 sb.append(new String(bytes2));101 }102 }103 104 buf.close();105 long end2 = System.currentTimeMillis();106 BufferedInputStreamTime += end2 - begin2;107 108 //Test FileReader109 long begin3 = System.currentTimeMillis();110 reader = new FileReader(f);111 br = new BufferedReader(reader);112 String str;113 while ((str = br.readLine()) != null) {114 sb.append(str);115 }116 br.close();117 reader.close();118 long end3 = System.currentTimeMillis();119 FileReaderTime += end3 - begin3;120 121 } catch (Exception e) {122 e.printStackTrace();123 } finally {124 try {125 br.close();126 reader.close();127 in.close();128 buf.close();129 } catch (Exception e) {130 e.printStackTrace();131 }132 }133 }
㈤ 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 中簡述使用流進行讀寫文本文件的步驟
一、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介面時,只是表明該類遵循串列化協議,而不需要實現任何特殊的方法。
* 在進行對象串列化時,需要注意將串列化的對象和輸入、輸出流聯系起來,首先通過對
* 象輸出流將對象狀態保存下來,然後通過對象輸入流將對象狀態恢復。
㈦ 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 文件讀寫流
讀寫是兩個不同的分支,通常都是分開單獨使用的。
可以通過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語言中向文件的讀寫中有好多流都搞不懂
記住: 萬變不離其宗!
兩個最原始最基礎的流為 InputStream 和OutputStream
分別用於輸入和輸出。
缺點是只能讀寫位元組 byte 或 byte[]
所以為了方便大家使用,sun又安排了很多具有高級功能的流
如DataInputStream 等,它們可以讀取一些高級的東西,如readUTF(),readLong()等等。
它們其實就是對InputStream進行的封裝,來源於這兩個原始的流。或繼承於它們。
所以你在學習的時候,建議先把最原始的兩個流搞清楚! 然後分析問題的時候,想想你需要什麼樣的流,有什麼樣的流可以簡化你的工作,他們分別比基本流新增了哪些特殊功能。 這樣對症下葯,樓主你的java功夫就能很快進步
㈩ 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();//使用後記得關閉
}
}