字節(jié)數(shù)組拷貝之a(chǎn)vailable()方法
- A:案例演示
- int read(byte[] b):一次讀取一個(gè)字節(jié)數(shù)組
- write(byte[] b):一次寫出一個(gè)字節(jié)數(shù)組
- available()獲取讀的文件所有的字節(jié)個(gè)數(shù)
- 弊端:有可能會(huì)內(nèi)存溢出
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3_Copy {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//第二種拷貝掂林,不推薦使用柠辞,因?yàn)橛锌赡軙?huì)導(dǎo)致內(nèi)存溢出
FileInputStream fis = new FileInputStream("Math.jpg");
FileOutputStream fos = new FileOutputStream("copy.jpg");
byte[] arr = new byte[fis.available()]; //創(chuàng)建與文件大小一樣的字節(jié)數(shù)組
fis.read(arr);
fos.write(arr);
fis.close();
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("Math.jpg"); //創(chuàng)建輸入流對(duì)象焚虱,關(guān)聯(lián)Math.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg"); //創(chuàng)建輸出流對(duì)象匣砖,關(guān)聯(lián)copy.jpg
int b;
while((b = fis.read()) != -1) { //在不斷的讀取每一個(gè)字節(jié)
fos.write(b); //將每一個(gè)字節(jié)寫出
}
fis.close(); //關(guān)流釋放資源
fos.close();
}
}
定義小數(shù)組及小數(shù)組的標(biāo)準(zhǔn)格式
- write(byte[] b)
- write(byte[] b, int off, int len)將指定byte數(shù)組中從偏移量off開始的len個(gè)字節(jié)寫入此文件輸出流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4_ArrayCopy {
/**
* 第三種拷貝
* 定義小數(shù)組
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
byte[] arr = new byte[2]; //一般情況下會(huì)設(shè)置為1024的整數(shù)倍
int len;
while((len = fis.read(arr)) != -1) {//如果忘記加arr帮匾,返回的就不是讀取的字節(jié)個(gè)數(shù)偏灿,而是字節(jié)的碼表值
fos.write(arr,0,len); //將arr數(shù)組中從偏移量0開始的len個(gè)字節(jié)寫入此文件輸出流
}
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("xxx.txt"); //文件內(nèi)容為abc
byte[] arr = new byte[2];
int a = fis.read(arr); //將文件上的字節(jié)讀取到字節(jié)數(shù)組中,讀的時(shí)候指針會(huì)移位扣蜻,若沒有數(shù)據(jù)則返回-1
System.out.println(a); //讀到的有效字節(jié)個(gè)數(shù)
for (byte b : arr) { //第一次獲取到文件上的a和b
System.out.println(b); //97,98
}
int c = fis.read(arr);
System.out.println(c);
for (byte b : arr) {
System.out.println(b); //99,98(這個(gè)98是數(shù)組上次存留下來的量)
}
fis.close();
}
}
BufferedInputStream和BufferOutputStream拷貝
- A:緩沖思想
- 字節(jié)流一次讀寫一個(gè)數(shù)組的速度明顯比一次讀寫一個(gè)字節(jié)的速度快很多坐桩,
- 這是加入了數(shù)組這樣的緩沖區(qū)效果尺棋,java本身在設(shè)計(jì)的時(shí)候,
- 也考慮到了這樣的設(shè)計(jì)思想(裝飾設(shè)計(jì)模式后面講解)绵跷,所以提供了字節(jié)緩沖區(qū)流
- B.BufferedInputStream
- BufferedInputStream內(nèi)置了一個(gè)緩沖區(qū)(數(shù)組)
- 從BufferedInputStream中讀取一個(gè)字節(jié)時(shí)
- BufferedInputStream會(huì)一次性從文件中讀取8192個(gè), 存在緩沖區(qū)中, 返回給程序一個(gè)
- 程序再次讀取時(shí), 就不用找文件了, 直接從緩沖區(qū)中獲取
- 直到緩沖區(qū)中所有的都被使用過, 才重新從文件中讀取8192個(gè)
- C.BufferedOutputStream
- BufferedOutputStream也內(nèi)置了一個(gè)緩沖區(qū)(數(shù)組)
- 程序向流中寫出字節(jié)時(shí), 不會(huì)直接寫到文件, 先寫到緩沖區(qū)中
- 直到緩沖區(qū)寫滿, BufferedOutputStream才會(huì)把緩沖區(qū)中的數(shù)據(jù)一次性寫到文件
- E.小數(shù)組的讀寫和帶Buffered的讀取哪個(gè)更快?
- 定義小數(shù)組如果是8192個(gè)字節(jié)大小和Buffered比較的話
- 定義小數(shù)組會(huì)略勝一籌,因?yàn)樽x和寫操作的是同一個(gè)數(shù)組
- 而Buffered操作的是兩個(gè)數(shù)組
package com.heima.stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("Math.jpg"); //創(chuàng)建輸入流對(duì)象膘螟,關(guān)聯(lián)Math.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//創(chuàng)建輸出流對(duì)象,關(guān)聯(lián)copy.jpg
BufferedInputStream bis = new BufferedInputStream(fis); //創(chuàng)建緩沖區(qū)對(duì)象碾局,對(duì)輸入流進(jìn)行包裝讓其變得更加強(qiáng)大
BufferedOutputStream bos = new BufferedOutputStream(fos);//創(chuàng)建緩沖區(qū)對(duì)象荆残,對(duì)輸出流進(jìn)行包裝讓其變得更加強(qiáng)大
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
}
flush和close方法的區(qū)別
- flush()方法
- 用來刷新緩沖區(qū)的,刷新后可以再次寫出
- close()方法
- 用來關(guān)閉流釋放資源的的,如果是帶緩沖區(qū)的流對(duì)象的close()方法,不但會(huì)關(guān)閉流,還會(huì)再關(guān)閉流之前刷新緩沖區(qū),關(guān)閉后不能再寫出
package com.heima.stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5_BufferCopy {
/**
* close方法
* 具備刷新的功能,在關(guān)閉流之前净当,就會(huì)先刷新一次緩沖區(qū)内斯,將緩沖區(qū)的字節(jié)全部刷新到文件上,再關(guān)閉像啼。關(guān)閉后不能再寫
* flush方法
* 具備刷新的功能俘闯,刷新完之后還可以繼續(xù)寫。應(yīng)用QQ聊天的小程序
*/
public static void main(String[] args) throws IOException {
//flush和close方法的區(qū)別
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Math.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg"));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
//bos.flush();
bis.close();
bos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("Math.jpg"); //創(chuàng)建輸入流對(duì)象忽冻,關(guān)聯(lián)Math.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg");//創(chuàng)建輸出流對(duì)象真朗,關(guān)聯(lián)copy.jpg
BufferedInputStream bis = new BufferedInputStream(fis); //創(chuàng)建緩沖區(qū)對(duì)象,對(duì)輸入流進(jìn)行包裝讓其變得更加強(qiáng)大
BufferedOutputStream bos = new BufferedOutputStream(fos);//創(chuàng)建緩沖區(qū)對(duì)象甚颂,對(duì)輸出流進(jìn)行包裝讓其變得更加強(qiáng)大
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
}
字節(jié)流讀寫中文
- 字節(jié)流讀取中文的問題
- 字節(jié)流在讀中文的時(shí)候有可能會(huì)讀到半個(gè)中文,造成亂碼
- 字節(jié)流寫出中文的問題
- 字節(jié)流直接操作的字節(jié),所以寫出中文必須將字符串轉(zhuǎn)換成字節(jié)數(shù)組
- 寫出回車換行 write("\r\n".getBytes());
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo6_Chinese {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt");
fos.write("我讀書少痹束,你不要騙我".getBytes());
//"我讀書少回论,你不要騙我".getBytes() 轉(zhuǎn)化為字節(jié)數(shù)組
fos.write("\r\n".getBytes());
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("yyy.txt");
byte[] arr = new byte[3];
int len;
while((len = fis.read(arr)) != -1) {
System.out.println(new String(arr,0,len)); //輸出中文,字節(jié)流讀取中文再輸出會(huì)出現(xiàn)亂碼
}
//String(byte[] bytes, int offset, int length)通過使用平臺(tái)的默認(rèn)字符集解碼指定的 byte 子數(shù)組雨涛,構(gòu)造一個(gè)新的 String衍菱。
fis.close();
}
}
流的標(biāo)準(zhǔn)處理異常代碼1.6版本及其以前和流的標(biāo)準(zhǔn)處理異常代碼1.7版本
- 原理
- 在try()中創(chuàng)建的流對(duì)象必須實(shí)現(xiàn)了AutoCloseable這個(gè)接口,如果實(shí)現(xiàn)了,在try后面的{}(讀寫代碼)執(zhí)行后就會(huì)自動(dòng)調(diào)用,流對(duì)象的close方法將流關(guān)掉(1.7)
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
public static void main(String[] args) throws IOException {
//1.7自動(dòng)實(shí)現(xiàn)了自動(dòng)關(guān)閉流赶么,通過AutoCloseable實(shí)現(xiàn)
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
}
public static void demo1() throws FileNotFoundException, IOException {
//1.6以及以前
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt"); //有可能賦值失敗,所以fis要在上面初始化
fos = new FileOutputStream("yyy.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}finally {
try{
if(fis != null)
fis.close(); //null調(diào)用方法會(huì)出現(xiàn)空指針異常
}finally{ //try finally的嵌套目的是能關(guān)一個(gè)盡量關(guān)一個(gè)
if(fos != null)
fos.close();
}
}
}
}
class MyClose implements AutoCloseable {
public void close() {
System.out.println("我關(guān)了");
}
}
圖片加密
package com.heima.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
int b;
while((b = bis.read()) != -1) {
bos.write(b ^ 123); //加密脊串,每個(gè)字節(jié)異或一個(gè)數(shù)辫呻。解密,每個(gè)字節(jié)再異或上同一個(gè)數(shù)
}
bis.close();
bos.close();
}
}
拷貝文件
在控制臺(tái)錄入文件的路徑,將文件拷貝到當(dāng)前項(xiàng)目下
package com.heima.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test2 {
/**
* 在控制臺(tái)錄入文件的路徑琼锋,將文件拷貝到當(dāng)前項(xiàng)目下
*
* 1放闺,定義方法對(duì)鍵盤錄入的路徑進(jìn)行判斷,如果是文件就返回
* 2缕坎,在主方法中接收該文件
* 3怖侦,讀和寫該文件
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = getFile(); //獲取文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
/*
* 定義一個(gè)方法獲取鍵盤錄入的文件路徑,并封裝成File對(duì)象返回
* 1,返回值類型File
* 2匾寝,參數(shù)列表無
*/
public static File getFile() {
Scanner sc = new Scanner(System.in); //創(chuàng)建鍵盤錄入對(duì)象
System.out.println("請(qǐng)輸入一個(gè)文件的路徑");
while(true) {
String line = sc.nextLine(); //接收鍵盤錄入的路徑
File file = new File(line); //封裝成File對(duì)象搬葬,然后對(duì)其判斷
if(!file.exists()) {
System.out.println("您錄入的文件路徑不存在,請(qǐng)重新輸入:");
}else if(file.isDirectory()) {
System.out.println("您錄入的是文件夾路徑艳悔,請(qǐng)重新輸入:");
}else {
return file; //直接return急凰,整個(gè)方法結(jié)束,循環(huán)也結(jié)束了猜年。
}
}
}
}
錄入數(shù)據(jù)拷貝到文件
- 將鍵盤錄入的數(shù)據(jù)拷貝到當(dāng)前項(xiàng)目下的text.txt文件中,鍵盤錄入數(shù)據(jù)當(dāng)遇到quit時(shí)就退出
package com.heima.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test3 {
/**
* 1,創(chuàng)建鍵盤錄入對(duì)象
* 2抡锈,創(chuàng)建輸出流對(duì)象,關(guān)聯(lián)text.txt文件
* 3码倦,定義無限循環(huán)
* 4企孩,遇到quit就退出循環(huán)
* 5,如果不quit袁稽,就將內(nèi)容寫出
* 6勿璃,關(guān)閉流
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
//2,創(chuàng)建輸出流對(duì)象推汽,關(guān)聯(lián)text.txt文件
FileOutputStream fos = new FileOutputStream("text.txt");
System.out.println("請(qǐng)輸入數(shù)據(jù):");
//3补疑,定義無限循環(huán)
while(true) {
String line = sc.nextLine(); //將鍵盤錄入的數(shù)據(jù)存儲(chǔ)在line中
//4,遇到quit就退出循環(huán)
if("quit".equals(line)) {
break;
}
//5歹撒,如果不quit莲组,就將內(nèi)容寫出
fos.write(line.getBytes()); //字符串寫出必須轉(zhuǎn)換成字節(jié)數(shù)組
fos.write("\r\n".getBytes()); //將換行寫入
}
//6,關(guān)閉流
fos.close();
}
}
字符流FileReader
- 1.字符流是什么
- 字符流是可以直接讀寫字符的IO流
- 字符流讀取字符, 就要先讀取到字節(jié)數(shù)據(jù), 然后轉(zhuǎn)為字符. 如果要寫出字符, 需要把字符轉(zhuǎn)為字節(jié)再寫出.
- 2.FileReader
- FileReader類的read()方法可以按照字符大小讀取
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo1_FileReader {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("xxx.txt");
int c;
while((c = fr.read()) != -1) { //通過項(xiàng)目默認(rèn)的碼表一次讀取一個(gè)字符
System.out.println((char)c);
}
fr.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("xxx.txt");
int x = fr.read();
System.out.println(x);
char c = (char)x;
System.out.println(c);
fr.close();
}
}
字符流FileWriter
- FileWriter類的write()方法可以自動(dòng)把字符轉(zhuǎn)為字節(jié)寫出
package com.heima.chario;
import java.io.FileWriter;
import java.io.IOException;
public class Demo2_FileWriter {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("yyy.txt");
fw.write("大家好暖夭,基礎(chǔ)班快接近尾聲了");
fw.write(97); //寫入單個(gè)字符
fw.close();
}
}
字符流的拷貝
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("xxx.txt");
FileWriter fw = new FileWriter("zzz.txt");
int c;
while((c = fr.read()) != -1) {
fw.write(c);
}
fr.close();
fw.close(); //Writer類中有一個(gè)緩沖區(qū)锹杈,大小為1024的字符數(shù)組,如果不關(guān)流迈着,內(nèi)容會(huì)留在緩沖區(qū)竭望。
}
}
什么情況下使用字符流
- 字符流也可以拷貝文本文件, 但不推薦使用. 因?yàn)樽x取時(shí)會(huì)把字節(jié)轉(zhuǎn)為字符, 寫出時(shí)還要把字符轉(zhuǎn)回字節(jié).字節(jié)流可以不需要轉(zhuǎn)換過程。
- 程序需要讀取一段文本, 或者需要寫出一段文本的時(shí)候可以使用字符流(只讀或只寫)
- 讀取的時(shí)候是按照字符的大小讀取的,不會(huì)出現(xiàn)半個(gè)中文
-
寫出的時(shí)候可以直接將字符串寫出,不用轉(zhuǎn)換為字節(jié)數(shù)組
字符流是否可以拷貝非純文本的文件
- 不可以拷貝非純文本的文件
- 因?yàn)樵谧x的時(shí)候會(huì)將字節(jié)轉(zhuǎn)換為字符,在轉(zhuǎn)換過程中,可能找不到對(duì)應(yīng)的字符,就會(huì)用?代替,寫出的時(shí)候會(huì)將字符轉(zhuǎn)換成字節(jié)寫出去
- 如果是?,直接寫出,這樣寫出之后的文件就亂了,看不了了
自定義字符數(shù)組的拷貝和帶緩沖的字符流
- BufferedReader的read()方法讀取字符時(shí)會(huì)一次讀取若干字符到緩沖區(qū), 然后逐個(gè)返回給程序, 降低讀取文件的次數(shù), 提高效率
- BufferedWriter的write()方法寫出字符時(shí)會(huì)先寫到緩沖區(qū), 緩沖區(qū)寫滿時(shí)才會(huì)寫到文件, 降低寫文件的次數(shù), 提高效率
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
int c;
while((c = br.read()) != -1) {
bw.write(c);
}
br.close();
bw.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("xxx.txt");
FileWriter fw = new FileWriter("yyy.txt");
char[] arr = new char[1024];
int len;
while((len = fr.read(arr)) != -1) { //將文件上的數(shù)據(jù)讀取到字符數(shù)組中
fw.write(arr,0,len); //將字符數(shù)組中的數(shù)據(jù)寫到文件上
}
fr.close();
fw.close();
}
}
readLine()和newLine()方法
- BufferedReader的readLine()方法可以讀取一行字符(不包含換行符號(hào))
- BufferedWriter的newLine()可以輸出一個(gè)跨平臺(tái)的換行符號(hào)"\r\n"
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4_Buffered {
/**
* 帶緩沖區(qū)的流中的特殊方法
* readLine()
* newLine()與\r\n的區(qū)別
* newLine()跨平臺(tái)
* \r\n只支持windows系統(tǒng)
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt"));
String line;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine(); //寫出回車換行符
//bw.write("\r\n"); //寫出回車換行符
}
br.close();
bw.close();
}
public static void demo1() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
String line;
while((line = br.readLine()) != null) { //readLine返回的是字符串對(duì)象裕菠,如果沒讀到就會(huì)返回null
System.out.println(line);
}
br.close();
}
}
將文本反轉(zhuǎn)
- 將一個(gè)文本文檔上的文本反轉(zhuǎn),第一行和倒數(shù)第一行交換,第二行和倒數(shù)第二行交換
package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) throws IOException {
//流對(duì)象盡量晚開早關(guān)
// 1,創(chuàng)建輸入輸出流對(duì)象
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
// 2,創(chuàng)建集合對(duì)象
ArrayList<String> list = new ArrayList<>();
// 3,將讀到的數(shù)據(jù)存儲(chǔ)在集合中
String line;
while((line = br.readLine()) != null) {
list.add(line);
}
br.close();
// 4,倒著遍歷集合將數(shù)據(jù)寫在文件上
BufferedWriter bw = new BufferedWriter(new FileWriter("revzzz.txt"));
for (int i = list.size() - 1; i >= 0; i--) {
bw.write(list.get(i));
bw.newLine();
}
// 5,關(guān)流
bw.close();
}
}
LineNumberReader
- LineNumberReader是BufferedReader的子類, 具有相同的功能, 并且可以統(tǒng)計(jì)行號(hào)
- 調(diào)用getLineNumber()方法可以獲取當(dāng)前行號(hào)
- 調(diào)用setLineNumber()方法可以設(shè)置當(dāng)前行號(hào)
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class Demo5_LineNumberReader {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt"));
String line;
lnr.setLineNumber(100); //設(shè)置行號(hào),如果不設(shè)置咬清,初始值是0
while((line = lnr.readLine()) != null) { //調(diào)用readLine(),行號(hào)就會(huì)加一
System.out.println(lnr.getLineNumber() + ":" + line); 從100+1=101開始
}
lnr.close();
}
}
裝飾設(shè)計(jì)模式
package com.heima.chario;
public class Demo6_Wrap {
//裝飾設(shè)計(jì)模式的好處是:耦合性不強(qiáng)奴潘,被裝飾的類的變化與裝飾類的變化無關(guān)
public static void main(String[] args) {
HeiMaStudent hms = new HeiMaStudent(new Student());
hms.code();
}
}
interface Coder {
public void code();
}
class Student implements Coder {
public void code() {
System.out.println("javase");
System.out.println("javaweb");
}
}
class HeiMaStudent implements Coder {
//1,獲取被裝飾類的引用
private Student s; //獲取學(xué)生引用
//2旧烧,在構(gòu)造方法中傳入被裝飾類的對(duì)象
public HeiMaStudent(Student s) {
this.s = s;
}
//3,對(duì)原有的功能升級(jí)
public void code() {
s.code();
System.out.println("ssh");
System.out.println("數(shù)據(jù)庫");
System.out.println("大數(shù)據(jù)");
System.out.println("...");
}
}
使用指定的碼表讀寫字符
- FileReader是使用默認(rèn)碼表讀取文件, 如果需要使用指定碼表讀取, 那么可以使用InputStreamReader(字節(jié)流,編碼表)
- FileWriter是使用默認(rèn)碼表寫出文件, 如果需要使用指定碼表寫出, 那么可以使用OutputStreamWriter(字節(jié)流,編碼表)
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Demo7_TransIO {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader( //更高效的讀
new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8"));
BufferedWriter bw = new BufferedWriter( //更高效的寫
new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk"));
int c;
while((c = br.read()) != -1) {
bw.write(c);
}
br.close();
bw.close();
}
public static void demo2() throws UnsupportedEncodingException,
FileNotFoundException, IOException {
InputStreamReader isr = new //指定碼表讀字符
InputStreamReader(new FileInputStream("utf-8.txt"), "utf-8");
OutputStreamWriter osw = new //指定碼表寫字符
OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");
int c;
while((c = isr.read()) != -1) {
osw.write(c);
}
isr.close();
osw.close();
}
public static void demo1() throws FileNotFoundException, IOException {
//用默認(rèn)編碼表讀寫出現(xiàn)亂碼
FileReader fr = new FileReader("utf-8.txt");
FileWriter fw = new FileWriter("gbk.txt");
int c;
while((c = fr.read()) != -1) {
fw.write(c);
}
fr.close();
fw.close();
}
}
獲取文本上字符出現(xiàn)的次數(shù)
package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
public class Test3 {
/**
* 獲取一個(gè)文本上每個(gè)字符出現(xiàn)的次數(shù)画髓,將結(jié)果寫在times.txt上
* 分析:
* 1掘剪,創(chuàng)建帶緩沖的輸入流對(duì)象
* 2,創(chuàng)建雙列集合對(duì)象TreeMap
* 3,將讀到的字符存儲(chǔ)在雙列集合中奈虾,存儲(chǔ)的時(shí)候要做判斷夺谁,如果不包含這個(gè)鍵肆汹,就將鍵和1存儲(chǔ)
* 如果包含這個(gè)鍵,就將該鍵和值加1存儲(chǔ)
* 4予权,關(guān)閉輸入流昂勉,創(chuàng)建輸出流對(duì)象
* 5,遍歷集合將集合中的內(nèi)容寫到times.txt中
* 6扫腺,關(guān)閉輸出流
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1岗照,創(chuàng)建帶緩沖的輸入流對(duì)象
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
//2,創(chuàng)建雙列集合對(duì)象TreeMap
TreeMap<Character, Integer> tm = new TreeMap<>();
//3,將讀到的字符存儲(chǔ)在雙列集合中笆环,存儲(chǔ)的時(shí)候要做判斷攒至,如果不包含這個(gè)鍵,就將鍵和1存儲(chǔ)
//如果包含這個(gè)鍵躁劣,就將該鍵和值加1存儲(chǔ)
int ch;
while((ch = br.read()) != -1) {
char c = (char)ch; //強(qiáng)制類型轉(zhuǎn)換
/*if(!tm.containsKey(c)) {
tm.put(c, 1);
}else {
tm.put(c, tm.get(c) + 1);
}*/
tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1);
}
//4迫吐,關(guān)閉輸入流,創(chuàng)建輸出流對(duì)象
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
//5账忘,遍歷集合將集合中的內(nèi)容寫到times.txt中
for (Character key : tm.keySet()) {
switch (key) {
case '\t':
bw.write("\\t" + "=" + tm.get(key));
break;
case '\n':
bw.write("\\n" + "=" + tm.get(key));
break;
case '\r':
bw.write("\\r" + "=" + tm.get(key));
break;
default:
bw.write(key + "=" + tm.get(key)); //寫出鍵和值
break;
}
bw.newLine();
}
//6志膀,關(guān)閉輸出流
bw.close();
}
}
試用版軟件
package com.heima.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test4 {
/**
* * 當(dāng)我們下載一個(gè)試用版軟件,沒有購買正版的時(shí)候,每執(zhí)行一次就會(huì)提醒我們還有多少次使用機(jī)會(huì)用學(xué)過的IO流知識(shí),
* 模擬試用版軟件,試用10次機(jī)會(huì),執(zhí)行一次就提示一次您還有幾次機(jī)會(huì),如果次數(shù)到了提示請(qǐng)購買正版
* 1,創(chuàng)建帶緩沖的輸入流對(duì)象,因?yàn)橐褂胷eadLine方法鳖擒,這樣可以保證數(shù)據(jù)的原樣性
* 2溉浙,將讀到的字符串轉(zhuǎn)換為int數(shù)
* 3,對(duì)int數(shù)進(jìn)行判斷蒋荚,如果大于0期升,就將其一1寫回去播赁,如果不大于0祖秒,就提示請(qǐng)購買正版
* 4,在if判斷中要將-1的結(jié)果打印沼瘫,并將結(jié)果通過輸出流寫在文件上
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,創(chuàng)建帶緩沖的輸入流對(duì)象耿戚,因?yàn)橐褂胷eadLine方法坛猪,這樣可以保證數(shù)據(jù)的原樣性
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
//2皂股,將讀到的字符串轉(zhuǎn)換為int數(shù)
String line = br.readLine();
int times = Integer.parseInt(line); //將數(shù)字字符串轉(zhuǎn)換為數(shù)字
//3呜呐,對(duì)int數(shù)進(jìn)行判斷蘑辑,如果大于0,就將其一1寫回去绷旗,如果不大于0刁标,就提示請(qǐng)購買正版
if(times > 0) {
//4,在if判斷中要將-1的結(jié)果打印启搂,并將結(jié)果通過輸出流寫在文件上
System.out.println("您還有" + times-- + "次機(jī)會(huì)");
FileWriter fw = new FileWriter("config.txt");
fw.write(times + "");
fw.close();
}else {
System.out.println("您的試用次數(shù)已到,請(qǐng)購買正版");
}
//關(guān)閉流
br.close();
}
}
遞歸
- 遞歸:方法自己調(diào)用自己
- 遞歸的弊端:不能調(diào)用次數(shù)過多疑苫,容易導(dǎo)致棧內(nèi)存溢出
- 遞歸的好處:不用知道循環(huán)次數(shù)
- 構(gòu)造方法是否可以遞歸調(diào)用捍掺?
- 構(gòu)造方法不能使用遞歸調(diào)用
- 遞歸調(diào)用是否必須有返回值再膳?
- 不一定喂柒,可以有禾嫉,也可以沒有
練習(xí)
package com.heima.test;
import java.io.File;
import java.util.Scanner;
public class Test5 {
/**
* 需求:從鍵盤輸入接收一個(gè)文件夾路徑,打印出該文件夾下所有的.java文件名
*
* 分析:
* 從鍵盤接收一個(gè)文件夾路徑
* 1,如果錄入的是不存在蚊丐,給與提示
* 2,如果錄入的是文件路徑麦备,給予提示
* 3,如果是文件夾路徑泥兰,直接返回
*
* 打印出該文件夾下的所有.java文件名
* 1弄屡,獲取到該文件夾路徑下的所有的文件和文件夾鞋诗,存儲(chǔ)在File數(shù)組中
* 2膀捷,遍歷數(shù)組削彬,對(duì)每一個(gè)文件或文件夾做判斷
* 3全庸,如果是文件壶笼,并且后綴是.java的就打印
* 4沛励,如果是文件夾,就遞歸調(diào)用
*/
public static void main(String[] args) {
File dir = getDir();
printJavaFile(dir);
}
/*
* 獲取鍵盤錄入的文件夾路徑
* 1,返回值類型File
* 2企蹭,不需要有參數(shù)
*/
public static File getDir() {
Scanner sc = new Scanner(System.in); //創(chuàng)建鍵盤錄入對(duì)象
System.out.println("請(qǐng)輸入一個(gè)文件夾路徑");
while(true) {
String line = sc.nextLine(); //將鍵盤錄入的文件夾路徑存儲(chǔ)
File dir = new File(line); //封裝成File對(duì)象
if(!dir.exists()) {
System.out.println("您錄入的文件夾路徑不存在白筹,請(qǐng)重新輸入");
}else if(dir.isFile()) {
System.out.println("您錄入的是文件路徑,請(qǐng)重新輸入文件夾路徑");
}else {
return dir;
}
}
}
/*
* 獲取文件夾路徑下的所有.java文件
* 1谅摄,返回值類型void
* 2徒河,參數(shù)列表File dir
*/
public static void printJavaFile(File dir) {
//1,獲取到該文件夾路徑下的所有的文件和文件夾螟凭,存儲(chǔ)在File數(shù)組中
File[] subFiles = dir.listFiles();
//2虚青,遍歷數(shù)組它呀,對(duì)每一個(gè)文件或文件夾做判斷
for (File subFile : subFiles) {
//3螺男,如果是文件棒厘,并且后綴是.java的就打印
if(subFile.isFile() && subFile.getName().endsWith(".java")) {
System.out.println(subFile);
//4,如果是文件夾下隧,就遞歸調(diào)用
}else if (subFile.isDirectory()) {
printJavaFile(subFile);
}
}
}
}