內(nèi)容
1.緩沖流
2.輸入輸出重定向
3.對(duì)象的讀寫(xiě)
4.RandomAccessFile
一.緩沖流


1.緩沖流作用
緩沖流的作用簡(jiǎn)單來(lái)說(shuō)就是能夠增加讀寫(xiě)效率。
2.緩沖流都有哪些
BufferedInputStream BufferedReader
BufferedOutputStream BufferedWriter
3.緩沖流和節(jié)點(diǎn)流在使用上的聯(lián)系/使用順序
1.使用節(jié)點(diǎn)流從磁盤(pán)將數(shù)據(jù)讀取到內(nèi)存的緩沖區(qū)
2.將內(nèi)存緩沖區(qū)的數(shù)據(jù)讀到緩沖流對(duì)應(yīng)的緩沖區(qū)
3.緩沖流從緩沖流的緩沖區(qū)將數(shù)據(jù)讀取到對(duì)應(yīng)的地方去
4.注意點(diǎn)
當(dāng)使用處理流輸出時(shí),需要使用flush來(lái)刷新流
5.1使用緩沖流完成字符文件的讀和寫(xiě)(比較低級(jí)的)
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\Users\\劉金豪\\Desktop\\temp";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\temp";
//使用處理流比較快
//1.使用節(jié)點(diǎn)流從磁盤(pán)將數(shù)據(jù)讀取到內(nèi)存的緩沖區(qū)
//2.將內(nèi)存緩沖區(qū)的數(shù)據(jù)讀到處理流對(duì)應(yīng)的緩沖區(qū)
//3.處理流從處理流的緩沖區(qū)將數(shù)據(jù)讀取到對(duì)應(yīng)的地方去
try(
//創(chuàng)建輸入流
FileInputStream fis = new FileInputStream(src);
BufferedReader br = new BufferedReader(fis);
//創(chuàng)建輸出流
FileOutputStream fos = new FileOutputStream(des);
OutputStreamWriter osw = new OutputStreamWriter(fos);//將字節(jié)輸出流轉(zhuǎn)換為字符輸出流
BufferedWriter bw = new BufferedWriter(osw);){
//一個(gè)字符一個(gè)字符地讀
int ch = 0;
while(true) {
//讀取數(shù)據(jù)
ch = br.read();
if(ch == -1) {
break;
}
//寫(xiě)入數(shù)據(jù)
bw.write(ch);
}
//刷新流
bw.flush();
}catch(Exception e) {
e.printStackTrace();
}
}
}
5.2高級(jí)寫(xiě)法
//一個(gè)字符一個(gè)字符地讀
int ch = 0;
while((ch = br.read()) != -1) {
bw.write(ch);
}
6.使用字節(jié)緩沖流并用數(shù)組 裝乘 完成文件的讀和寫(xiě)
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\Users\\劉金豪\\Desktop\\撲克游戲UML圖.jpg";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\撲克游戲UML圖.jpg";
try(
//字節(jié)緩沖輸入流
FileInputStream fis = new FileInputStream(src);
BufferedInputStream bis = new BufferedInputStream(fis);
//字節(jié)緩沖輸出流
FileOutputStream fos = new FileOutputStream(des);
BufferedOutputStream bos = new BufferedOutputStream(fos);){
byte[] buffer = new byte[1024];
int len = 0;
while((len = bis.read(buffer)) != -1) {
bos.write(buffer);
}
bos.flush();
}catch(Exception e) {
e.printStackTrace();
}
}
}
二.輸入輸出重定向。
1.簡(jiǎn)單介紹
比如本來(lái)輸出到終端,那么我們可以重定向,使它輸出到特定的文件中。
同樣地,本來(lái)是從終端輸入,但是我們可以重定向,使其從特定的文件輸入。
2.輸入輸出重定向使用示例
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
//輸出重定向
/* try (FileOutputStream fos = new FileOutputStream(des);
PrintStream ps = new PrintStream(fos); )
{
System.setOut(ps);
System.out.println("helloworld");//對(duì)應(yīng)的文件就會(huì)有helloworld,如果沒(méi)有這個(gè)文件,系統(tǒng)會(huì)自動(dòng)創(chuàng)建
} catch (Exception e) {
e.printStackTrace();
}
*/
//輸入重定向
try(FileInputStream fis = new FileInputStream(src);){
Scanner scanner = new Scanner(fis);
while(scanner.hasNext()) {
System.out.println(scanner.next());//控制臺(tái)就會(huì)輸出 helloworld
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
三.對(duì)象的讀寫(xiě)
1.注意點(diǎn)
①如果需要將自己定義的類(lèi)的某個(gè)對(duì)象使用文件保存。那么這個(gè)類(lèi)必須是實(shí)現(xiàn)了Sreializable接口。
(關(guān)于Serializable接口的詳細(xì)介紹請(qǐng)參看我另一篇博客:http://www.itdecent.cn/p/be6313ddba50)
②勿忘刷新。
2.使用示例
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
/* //寫(xiě)入對(duì)象
try(FileOutputStream fos = new FileOutputStream(des);
ObjectOutputStream oos = new ObjectOutputStream(fos);){
//保存對(duì)象
//如果需要將自己定義的類(lèi)的某個(gè)對(duì)象使用文件保存
//那么這個(gè)類(lèi)必須是實(shí)現(xiàn)了Sreializable接口
Person person = new Person();
oos.writeObject(person);
//刷新
oos.flush();
}catch(Exception e) {
e.printStackTrace();
}
*/
//讀取對(duì)象
try(
FileInputStream fis = new FileInputStream(src);
ObjectInputStream ois = new ObjectInputStream(fis);){
Person p = (Person)ois.readObject();
System.out.println(p);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
class Person implements Serializable{
}
四.RandomAccessFile
1.引
當(dāng)一個(gè)文件存在時(shí),如果按照之前的寫(xiě)法,那么一般在寫(xiě)入數(shù)據(jù)時(shí)會(huì)把之前的數(shù)據(jù)刪除,也就是從頭寫(xiě)。這樣有時(shí)候不能滿足我們的需求,如果想在文件的某個(gè)位置讀或者寫(xiě),那么我們可以使用RandomAccessFile。
2.最基本的使用示例(使用數(shù)組裝乘)
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
try {
RandomAccessFile raf = new RandomAccessFile(src,"r");//只讀取
byte[] buffer = new byte[50];
raf.read(buffer);//這是字節(jié)
//打印的話需要轉(zhuǎn)化成String
System.out.println(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.“從第幾個(gè)開(kāi)始讀取”使用示例
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
try {
RandomAccessFile raf = new RandomAccessFile(src,"r");//只讀取
raf.seek(2);//從第三個(gè)開(kāi)始讀取
byte[] buffer = new byte[50];
raf.read(buffer);//這是字節(jié)
//打印的話需要轉(zhuǎn)化成String
System.out.println(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.“從第幾個(gè)開(kāi)始寫(xiě)”使用示例
import java.io.*;
import java.util.*;
public class 測(cè)試程序 {
public static void main(String[] args){
String src = "C:\\\\Users\\\\劉金豪\\\\Desktop\\\\newfile\\\\重定向";
String des = "C:\\Users\\劉金豪\\Desktop\\newfile\\重定向";
try {
RandomAccessFile raf = new RandomAccessFile(src,"rw");//可讀可寫(xiě)
raf.seek(2);//從第三個(gè)開(kāi)始寫(xiě),會(huì)覆蓋后面的內(nèi)容
raf.writeChars("helllll");
//System.out.println(new String());
} catch (Exception e) {
e.printStackTrace();
}
}
}
總結(jié)
內(nèi)容比較多,雖然比第一遍學(xué)習(xí)更清晰,但是還是有很多混了的地方。不慌!等明天再學(xué)一遍,把有關(guān)文件的博客再看一遍,絕對(duì)沒(méi)問(wèn)題!