java管道流文件的复制_JavaIO 总结笔记三 基本字节字符输入输出流和文件复制...
一、IO體系
1.流主要分兩大類:字節(jié)流 字符流
2.在硬盤(pán)上的文件,都是以二進(jìn)制字節(jié)形式存儲(chǔ)的,所以不管啥文件,讀寫(xiě)時(shí)都應(yīng)該用字節(jié)流
3.在java的早期版本中,的確只有字節(jié)流,沒(méi)有字符流
4.一個(gè)數(shù)字或字母占1個(gè)字節(jié),一個(gè)漢字占了2個(gè)字節(jié),而字節(jié)流一次讀寫(xiě)1個(gè)字節(jié),很容易產(chǎn)生中文亂碼問(wèn)題
5.字符流專門用來(lái)讀寫(xiě)文本類的文件,txt doc
6.字節(jié)流適合用來(lái)讀寫(xiě)非文本類的文件,mp3 mp4 avi rm rmvb mkv jpg png bmp
二、常用類(管道類)
1.字節(jié)流 FileInputStream FileOutputStream
2.字符流 FileReader FileWriter
三、字節(jié)流的輸出流 FileOutputStream
1.作用 把內(nèi)存中的數(shù)據(jù)輸出到(寫(xiě)到)硬盤(pán)的某個(gè)文件中
2.構(gòu)造方法
FileOutputStream(File file) 不存在會(huì)自動(dòng)鍵
FileOutputStream(String name) 不存在會(huì)自動(dòng)鍵
3.功能方法
public void write(int b) 參數(shù)是ASCII表中的碼值,不是普通數(shù)字
public void write(byte[] b) String對(duì)象的getBytes方法有編碼轉(zhuǎn)換功能
public void write(byte[] b, int off, int len)
4.使用字節(jié)流怎么輸出一個(gè)換行?可以使用System.getProperty("line.separator")來(lái)獲得當(dāng)前系統(tǒng)的換行符。
5.如何追加和覆蓋? 默認(rèn)就是覆蓋
代碼舉例:
public static void writeFile() throws IOException {
//演示FileOutputStream類
//1.建管道
FileOutputStream fos1 = new FileOutputStream(new File("A03_FileOutputStreamTest.txt"));
//FileOutputStream fos2=new FileOutputStream("A03_FileOutputStreamTest.txt");
//2.寫(xiě)數(shù)據(jù)
fos1.write(97);
fos1.write(98);
fos1.write(99);
fos1.write(100);
byte[] t = {97, 98, 'c'};
fos1.write(t);
fos1.write("hello".getBytes());
fos1.write("world".getBytes());
fos1.write("\r\n".getBytes());
fos1.write(t, 0, 2);
fos1.close();
}
四、字節(jié)流的輸入流 FileInputStream類
1.作用 把硬盤(pán)中的數(shù)據(jù)輸入(讀取)到內(nèi)存中
2.構(gòu)造方法
FileInputStream(File file)
FileInputStream(String name)
3.功能方法
public int read() 一次讀取一個(gè)字節(jié)并返回,如果到文件最后了,返回-1
public int read(byte[] b) 一次讀取一個(gè)數(shù)組長(zhǎng)度的數(shù)據(jù) ,返回的是讀取到的數(shù)據(jù)的長(zhǎng)度 ,讀不到數(shù)據(jù)就返回-1
new String(byte[] b)
代碼舉例:
public static void readFile() throws IOException {
//1.建管道
File f = new File("src\\com\\afinalstone\\file\\IOUtil.java");
InputStream fis = new FileInputStream(f);
//FileInputStream fis=new FileInputStream("src\com\afinalstone\file\IOUtil.java");
//2.讀取數(shù)據(jù)
// public int read() 一次讀取一個(gè)字節(jié)并返回,如果到文件最后了,返回-1
int res;
do {
res = fis.read();
System.out.println((char) res);
} while (res != -1);
//3.關(guān)閉
fis.close();
}
public static void readFileByBufferByte() throws IOException {
//1.建管道
File f = new File("src\\com\\afinalstone\\file\\IOUtil.java");
InputStream fis = new FileInputStream(f);
//FileInputStream fis=new FileInputStream("c:\\test\\hello.txt");
// public int read(byte[] b) 一次讀取一個(gè)數(shù)組長(zhǎng)度的數(shù)據(jù) ,返回的是讀取到的數(shù)據(jù)的長(zhǎng)度
//2.來(lái)個(gè)緩存區(qū)讀取數(shù)據(jù)
System.out.println(f.length());
byte[] t = new byte[(int) f.length()];
//3.裝車
fis.read(t);
System.out.println(new String(t).trim());
//4.關(guān)閉
fis.close();
}
五、復(fù)制文件
1.復(fù)制 先讀取目標(biāo)文件的數(shù)據(jù),然后再把數(shù)據(jù)寫(xiě)入到另一個(gè)文件中
2.復(fù)制的方式
a.對(duì)于較小文件 一次性讀,然后一次性寫(xiě)
b.對(duì)于較大文件 邊讀邊寫(xiě) 讀一點(diǎn),寫(xiě)一點(diǎn),緩存的使用
代碼舉例:
public static void copy01_FileIOStream() throws IOException {
//a.對(duì)于較小文件 一次性讀,然后一次性寫(xiě)
//1.建管道
File f = new File(fileName_source);
FileInputStream fis = new FileInputStream(f);
//2.來(lái)輛車
byte[] t = new byte[(int) f.length()];
//3.裝車
fis.read(t);
//4.關(guān)閉
fis.close();
//1.建管道
FileOutputStream fos = new FileOutputStream(fileName_destination);
//2.寫(xiě)數(shù)據(jù)
fos.write(t);
//3.關(guān)閉
fos.close();
}
public static void copy01_FileIOStreamBufferByte() throws IOException {
//b.對(duì)于較大文件 邊讀邊寫(xiě) 讀一點(diǎn),寫(xiě)一點(diǎn)
//1.建兩個(gè)管道
FileInputStream fis=new FileInputStream(fileName_source);
FileOutputStream fos=new FileOutputStream(fileName_destination);
//2.來(lái)輛車
// byte[] t=new byte[1*1024*1024]; //1M
byte[] t=new byte[8*1024]; //8k
//3.裝車卸車 讀1M寫(xiě)1M
while(true)
{
int res=fis.read(t); //讀
if(res==-1)
break;
fos.write(t,0,res); //寫(xiě)(最后一趟車可能裝不滿)
}
//4.關(guān)閉
fis.close();
fos.close();
}
六、字符流的輸出流FileWriter類
1.作用 把內(nèi)存中的數(shù)據(jù)輸出(寫(xiě))到硬盤(pán)的某個(gè)文件中 只適合文本類的文件
2.構(gòu)造方法
public FileWriter(File file)
public FileWriter(String fileName)
public FileWriter(File file,boolean append)
public FileWriter(String fileName,boolean append)
3.功能方法
public void write(String str)
public void write(char[] cbuf)
public void write(char[] b,int off, int len)
代碼舉例:
public static void writeFile() throws IOException {
//演示FileWriter類
//1.建管道
FileWriter fw=new FileWriter(new File("F03_FileReaderAndWriter.txt"));
//FileWriter fw=new FileWriter("c:\\test\\hello.txt");
//2.寫(xiě)數(shù)據(jù)
fw.write("hello\r\n");
char[] c={'a','b','c',100};
fw.write(c);
fw.write("\r\n");
fw.write(c, 0, 2);
//3.關(guān)閉
fw.close();
}
七、字符流的輸入流FileReader類
1.作用 把硬盤(pán)中某個(gè)文件的數(shù)據(jù)輸入(讀取)到內(nèi)存中
2.構(gòu)造方法
public FileReader(File file)
public FileReader(String fileName)
3.功能方法
public int read() 一次讀一個(gè)字符 讀到數(shù)據(jù)返回的就是ASCII碼,讀不到就返回-1
public int read(char[] cbuf) 讀到數(shù)據(jù)返回的就是數(shù)據(jù)的長(zhǎng)度,讀不到就返回-1
代碼舉例:
public static void readFile() throws IOException {
//演示FileReader類
//1.建管道
File f=new File("fileName");
FileReader fr=new FileReader(f);
//FileReader fr=new FileReader("src\com\afinalstone\file\F03_FileReaderAndWriter.java");
//2.讀數(shù)據(jù)
int res;
do {
res=fr.read();
System.out.println((char)res);
} while (res!=-1);
//3.關(guān)閉
fr.close();
}
public static void readFileByBufferChar() throws IOException {
//演示FileReader類
//1.建管道
File f=new File(fileName);
FileReader fr=new FileReader(f);
//FileReader fr=new FileReader("c:\\test\\hello.txt");
//2.來(lái)輛車
System.out.println(f.length());
char[] c=new char[(int)f.length()];
//3.裝車
fr.read(c);
System.out.println(new String(c).trim());
//4.關(guān)閉
fr.close();
}
八、字符流的緩沖區(qū)
1.字符流有緩沖區(qū),字節(jié)流是沒(méi)有緩沖區(qū)的
2.緩沖區(qū):它是內(nèi)存中的一塊區(qū)域
3.作用:提高效率 減少對(duì)硬盤(pán)的頻繁讀寫(xiě) 保護(hù)硬盤(pán)
4.字符流緩沖區(qū)的默認(rèn)大小是8K
5.當(dāng)關(guān)閉管道時(shí),會(huì)強(qiáng)制把緩沖區(qū)中的數(shù)據(jù)全部寫(xiě)到硬盤(pán)中
6.public void flush() //清緩沖區(qū),強(qiáng)制寫(xiě)入
九、用字符流實(shí)現(xiàn)文件復(fù)制
復(fù)制的方式
a.對(duì)于較小文件 一次性讀,然后一次性寫(xiě)
b.對(duì)于較大文件 邊讀邊寫(xiě) 讀一點(diǎn),寫(xiě)一點(diǎn)
public static void copy02_FileReaderWriter() throws IOException {
//1.建兩個(gè)管道
FileReader fr=new FileReader(fileName_source);
FileWriter fw=new FileWriter(fileName_destination);
//3.裝車卸車 邊讀邊寫(xiě)
while(true)
{
int res=fr.read();
if(res==-1)
break;
fw.write(res);
}
//4.關(guān)閉
fr.close();
fw.close();
}
public static void copy02_FileReaderWriterBufferChar() throws IOException {
//演示用字符流實(shí)現(xiàn)較大文件的復(fù)制 邊讀邊寫(xiě)
//1.建兩個(gè)管道
FileReader fr=new FileReader(fileName_source);
FileWriter fw=new FileWriter(fileName_destination);
//2.來(lái)輛車
char[] c=new char[1*1024]; //1K
//3.裝車卸車 邊讀邊寫(xiě)
while(true)
{
int res=fr.read(c);
if(res==-1)
break;
fw.write(c,0,res);
}
//4.關(guān)閉
fr.close();
fw.close();
}
項(xiàng)目地址:傳送門
總結(jié)
以上是生活随笔為你收集整理的java管道流文件的复制_JavaIO 总结笔记三 基本字节字符输入输出流和文件复制...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux amd显卡调风扇转速,显卡风
- 下一篇: 数据结构之栈实现中缀转后缀并计算结果