Day 8
輸入和輸出處理
File類
文件
什么是文件?
相關(guān)記錄或放在一起的數(shù)據(jù)的集合
Java程序如何訪問(wèn)文件屬性?
通過(guò)Java API:java.io.File類
File類訪問(wèn)文件屬性
?File類的常用方法
方法名稱? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 說(shuō)明
boolean exists()? ? ? ? ? ? ? ? ? ?判斷文件或者目錄是否存在
boolean isFile()? ? ? ? ? ? ? ? ? ? 判斷是否是文件
boolean isDIrectory? ? ? ? ? ? ? 判斷是否是目錄
String getPath()? ? ? ? ? ? ? ? ? ? 返回此對(duì)象表示的文件的相對(duì)路徑名
String getAbsolutePath? ? ? ? 返回此對(duì)象表示的文件的絕對(duì)路徑名
String getName()? ? ? ? ? ? ? ? ? 返回此對(duì)象表示的文件或目錄的名稱
boolean delete()? ? ? ? ? ? ? ? ? ?刪除此對(duì)象指定的文件或目錄
boolean createNewFiel()? ? ? 創(chuàng)建名稱的空文件,不創(chuàng)建文件夾
long length? ? ? ? ? ? ? ? ? ? ? ? ? ?返回文件的長(zhǎng)度,單位為字節(jié),如果文件不存在,則返回0L
流
什么是流
流是一組有序的數(shù)據(jù)序列,以先進(jìn)先出方式發(fā)送信息的通道
Java流的分類
按流向區(qū)分:
分為輸出流和輸入流,其中輸入流以InputStream和Reader作為基類,輸出流以O(shè)utputStream和Writer作為基類。(輸入輸出流是相對(duì)于計(jì)算機(jī)內(nèi)存來(lái)說(shuō)的)
按照處理數(shù)據(jù)單元?jiǎng)澐?#xff1a;
分為字節(jié)流和字符流,其中字節(jié)流又可分為字節(jié)輸入流InputStream和字節(jié)輸出流OutputStream,字符流分為字符輸入流Reader和字符輸出流Writer。
字節(jié)流是8位通用字節(jié)流,字符流是16位Unicode字符流。
FileInputStream
InputStream類常用方法
1、int read()
從輸入流一個(gè)字節(jié)一個(gè)字節(jié)的讀,返回的是該字節(jié)的整數(shù)表示形式,如果讀到了輸入流的末尾,返回-1。
2、int read(byte[] b)
從輸入流讀取若干字節(jié),把這些字節(jié)保存到字節(jié)數(shù)組中。返回的是讀取到的字節(jié)數(shù),如果讀到了輸入流的末尾,返回-1。
3、int read(byte[] b,int off, int len)
從輸入流讀取若干字節(jié),把這些自己保存到字節(jié)數(shù)組b中。off指的是字節(jié)數(shù)組中開(kāi)始保存數(shù)據(jù)的起始位置下標(biāo)。len指讀取的自己數(shù)目。返回的是讀取到的字節(jié)數(shù),如果讀到了輸入流的末尾,返回-1。
4、void close()
關(guān)閉流
5、int available()
可以從輸入流中讀取的字節(jié)數(shù)目
子類FileInputStream常用的構(gòu)造方法
1、FileInputStream(File file)
2、FileInputStream(String name)//完整的路徑
使用FileInputStream讀文本文件
步驟:
1、引入相關(guān)的類
import java.io.IOException;
import java.io.FileInputStream;
2、構(gòu)造文件輸入流FileInputStream對(duì)象
FileInputStream fis=new FileInputStream("D:/test.txt");
3、讀取文本文件的數(shù)據(jù)
fis.available();
fis.read();
4、關(guān)閉文件流對(duì)象
fis.close();
1、借助FileInputStream對(duì)象的read()方法讀取文件
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;public class TestFileInputStream {public static void main(String[] args) {FileInputStream fis=null;try {fis=new FileInputStream("D:/test.txt");int data;while((data=fis.read())!=-1){System.out.print((char)data);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}}} }2、借助FileInputStream對(duì)象的read(byte[] b)方法讀取文件
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;public class TestFileInputStream {public static void main(String[] args) {FileInputStream fis=null;try {fis=new FileInputStream("D:/test.txt");byte[] b=new byte[fis.available()];int data;while((data=fis.read(b))!=-1){for (int i = 0; i < b.length; i++) {System.out.println((char)b[i]);}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}}} }OutputStream
OutputStream類常用方法
1、void write(int c)
往輸出流中寫(xiě)入一個(gè)個(gè)的字節(jié)
2、void write(byte[] b)
往輸出流中寫(xiě)入一個(gè)字節(jié)數(shù)組
3、void write(byte[] b, int off, int len)
往輸出流中寫(xiě)入一個(gè)字節(jié)數(shù)組,off代表開(kāi)始從字節(jié)數(shù)組的off位置開(kāi)始往外寫(xiě),len代表往外寫(xiě)len長(zhǎng)度的字節(jié)
4、void close()
關(guān)閉輸出流
5、void flush()
強(qiáng)制把緩沖區(qū)中的數(shù)據(jù)全部寫(xiě)到輸出流中
子類FileOutputStream常用的構(gòu)造方法
1、FileOutputStream(File file)
2、FileOutputStream(String name)//路徑
3、FileOutputStream(String name, boolean append) //當(dāng)append的值為true時(shí),追加寫(xiě)入
注意:
1、前兩種構(gòu)造方法在向文件寫(xiě)數(shù)據(jù)時(shí)將覆蓋文件中的原有內(nèi)容
2、創(chuàng)建FileOutputStream實(shí)例時(shí),如果相應(yīng)的文件并不存在,則會(huì)自動(dòng)創(chuàng)建一個(gè)空的文件
使用FileOutputStream寫(xiě)入文件
1、引入相關(guān)的類
import java.io.IOException;
import java.io.FileOutputStream;
2、構(gòu)造文件輸出流FileOutputStream對(duì)象
FileOutputStream fos=new FileOutputStream("D:/test.txt");
3、把數(shù)據(jù)寫(xiě)入文本文件
String str="好好學(xué)習(xí) 天天向上";
byte[] b=str.getBytes(str);
fos.write(b);
4、關(guān)閉文件流對(duì)象
fos.close();
完整代碼:
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class TestFileOutputStream {public static void main(String[] args) {FileOutputStream fos=null;try {fos=new FileOutputStream("D:/test.txt");String str="好好學(xué)習(xí) 天天向上";byte[] b=str.getBytes();fos.write(b);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}}} }
?
總結(jié)
- 上一篇: matlab正弦函数fft,正弦函数及其
- 下一篇: Twisted-17.1.0-cp36-