還要多久才能成為JAVA大佬?第十六天

OutputStream 字節(jié)輸出流
FileOutputStream

常用API方法:
1.void write(int b) 將指定的字節(jié)寫入此文件輸出流赘理。
2.void write(byte[] b) 將 b.length個字節(jié)從指定的字節(jié)數(shù)組寫入此文件輸出流余佃。
3.void write(byte[] b, int off, int len) 將 len字節(jié)從位于偏移量 off的指定字節(jié)數(shù)組寫入此文件輸出流鞠呈。
//指定一個寫的目的地
String pathname="day20\src\aaa.txt";
File file = new File(pathname);
FileOutputStream fos = new FileOutputStream(file);
//寫字符
fos.write(97);
fos.write(98);
fos.write(99);
fos.close();
//寫字符數(shù)組
byte[] bytes={102,103,104};
fos.write(bytes);
fos.close();
//寫字符串
String str="智游教育";
byte[] strBytes = str.getBytes();
fos.write(strBytes);
fos.close();
//寫字符數(shù)組的一部分
fos.write(strBytes,0,6);
fos.close();
//換行\(zhòng)r\n
fos.write(100);
String str1="\r\n";
fos.write(str1.getBytes());
FileOutputStream fos1 = new FileOutputStream(file,true);
fos1.write(strBytes);
fos1.close();
//fos.write(101);
備注:
1.如果往文件中寫入漢字的時候通過字符串轉(zhuǎn)換成字節(jié)數(shù)組,我們需要從字節(jié)數(shù)組中取3的倍數(shù)(UTF-8)(2的倍數(shù)-GBK)才能正常地取出漢字
2.如果在windows系統(tǒng)當(dāng)中翔试,往文件中寫入一個換行符,可以通過\r\n,但是需要把\r\n轉(zhuǎn)換成字節(jié)數(shù)組
InputStream 字節(jié)輸入流
FileInputStream

常用API方法:
//指定一個路徑
String pathname="day20\src\aaa.txt";
File file = new File(pathname);
FileInputStream fis = new FileInputStream(file);

    System.out.println((char)fis.read());
    System.out.println((char)fis.read());
    System.out.println((char)fis.read());
    System.out.println(fis.read());

    //定義一個變量轻要,用來保存每次的數(shù)據(jù)
    int data;
    while (true){
        if ((data=fis.read())==-1){
            break;
        }
        System.out.println((char)data);
    }

    //每次讀取一個字符數(shù)組
    int data;
    byte[] bytes = new byte[2];
    while (true){
        if ((data=fis.read(bytes))==-1){
            break;
        }
        //字節(jié)數(shù)組轉(zhuǎn)換成字符串
        System.out.println(new String(bytes));
    }

    byte[] bytes = new byte[2];
    int length;
    while (true) {
        if ((length=fis.read(bytes)) == -1) {
            break;
        }
        //字節(jié)數(shù)組轉(zhuǎn)換成字符串
        System.out.println(new String(bytes, 0, length));
    }
    fis.close();

備注:
1.read()每次從文件中讀取一個字符,如果讀取到文件的末尾,就會返回一個-1
2.read(byte[] bytes)方法當(dāng)中的參數(shù)為一個字節(jié)輸入流的緩沖區(qū)垦缅,并且可以臨時存儲多個值冲泥,該方法的返回值是每次從文件中
Reader類
字符輸入流,用于文件中數(shù)據(jù)進(jìn)行讀取壁涎,每次讀取都以字符為單位進(jìn)行讀取
FileReader:文件字符輸入流 用于數(shù)據(jù)的讀取

構(gòu)造方法:
FileReader(File file) 創(chuàng)建一個新的 FileReader 凡恍,給出 File讀取。
常用API方法:
1.public int read() throws IOException 用于讀取文件中的字符數(shù)據(jù) 每次讀取一個字符 當(dāng)讀取到文件的末尾時怔球,返回-1
2.public int read(char[] char) throws IOException 用于讀取文件中的字符數(shù)據(jù) 每次讀取一個字符數(shù)組嚼酝,返回值是從文件中讀取到的有效字符個數(shù),當(dāng)返回值為-1竟坛,代表讀取到文件的末尾
public static void m1_read() throws Exception{
// 讀取D盤根目錄中的文件 "我的青春誰做主.txt"
String pathname = "D:\我的青春誰做主.txt";
File file = new File(pathname);
// 準(zhǔn)備一個文件字符輸入流對象
FileReader fr = new FileReader(file);
// 讀取數(shù)據(jù)
/int read = fr.read();
System.out.println((char)read);// 我
read = fr.read();
System.out.println((char)read);// 我
read = fr.read();
System.out.println((char)read);// 我
/
// 使用循環(huán)來寫
int data;
while (true) {
if (( data = fr.read()) == -1) {
break;
}
System.out.print((char)data);//我的青春我不能做主!!!
}
}
Writer類
字符輸出流 緩沖區(qū)---用來存儲字符輸出流中寫入的數(shù)據(jù)
子類FileWriter 文件字符輸出流

構(gòu)造方法:
1.FileWriter(File file) 給一個File對象構(gòu)造一個FileWriter對象闽巩。
2.FileWriter(File file, boolean append) 給一個File對象構(gòu)造一個FileWriter對象。
文件復(fù)制
public static void ctrlCV()throws IOException{
String string1="D:\我的青春誰做主.txt";
String string2="E:\Prime.txt";
File file1 = new File(string1);
File file2 = new File(string2);
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
byte[] bytes=new byte[1024];
int length;
while (true){
if ((length=fis.read(bytes))==-1){
break;
}
/String s = new String(bytes, 0, length);
byte[] sBytes = s.getBytes();
fos.write(sBytes);
/
fos.write(bytes,0,length);
fos.close();
fis.close();
}
}
讀取數(shù)據(jù)-字符數(shù)組為緩沖區(qū)

public static void m2_buffer() throws Exception {
    // 讀取D盤根目錄中的文件 "我的青春誰做主.txt"
    String pathname = "D:\\我的青春誰做主.txt";
    File file = new File(pathname);
    // 準(zhǔn)備一個文件字符輸入流對象
    FileReader fr = new FileReader(file);
    // 讀取數(shù)據(jù)
    char[] buffer = new char[5];
    // 記錄每次讀取到有效字符個數(shù)
    int length;
    while (true) {
        if ((length = fr.read(buffer)) == -1) {
            break;
        }
        // 展示下每次讀取到的字符值
        //String(char[] chars,int offset.int len);
        System.out.println(new String(buffer, 0, length));
        //System.out.println(Arrays.toString(buffer));
    }
    // 關(guān)閉流
    fr.close();
}

文件字符輸出流

public static void m2()throws IOException{

// 在項(xiàng)目的根目錄的src目錄下創(chuàng)建一個文件bbb.txt文件担汤,并向該文件中寫入一些字符值 下午考試
String pathname="day20\src\bbb.txt";
File file = new File(pathname);
// 準(zhǔn)備一個文件字符輸出流對象
FileWriter fw = new FileWriter(file,true);
//下午考試
String content="下午考試";
fw.write(content);
//刷新緩存區(qū)中的流數(shù)據(jù)
fw.flush();
//關(guān)閉流
fw.close();//內(nèi)部帶有刷新流的動作
}

轉(zhuǎn)換流:
當(dāng)讀取和寫入時程序環(huán)境和目標(biāo)文件的編碼字符集不一致涎跨,一般會出現(xiàn)亂碼,可以通過轉(zhuǎn)換流來實(shí)現(xiàn)如果是往目標(biāo)文件寫入數(shù)據(jù)時崭歧,把字符流轉(zhuǎn)換成字節(jié)流寫入到目標(biāo)文件當(dāng)中隅很,可以指定編碼字符集

意義:
一般情況下,開發(fā)工具當(dāng)中的編碼字符集一般設(shè)置的都是UTF-8如果在讀取源文件時率碾,源文件的編碼字符集是GBK叔营,,那么我們在讀取的時候也要使用GBK編碼字符集進(jìn)行解碼
如果在進(jìn)行寫文件的時候播掷,如果目標(biāo)文件使用GBK編碼字符表進(jìn)行編碼审编,那么我們也要使用GBK編碼字符集進(jìn)行寫入
備注:
不管使用字節(jié)流還是字符流,最終存儲到文件當(dāng)中的都是以字節(jié)的方式存入的
字符集:
ASCII字符集--->ASCII編碼表
GBK字符集--->GBK編碼表
Unicode字符集--->UTF-8編碼表歧匈、UTF-16編碼表、UTF-32編碼表...

//把字符流轉(zhuǎn)換為字節(jié)流
public static void m1()throws Exception{
    //創(chuàng)建一個字節(jié)輸出流
    String pathname="day21\\src\\abc.txt";

    FileOutputStream fos = new FileOutputStream(pathname);
    String str="河南鄭州";
    byte[] bytes = str.getBytes("UTF-8");
    fos.write(bytes);

    //創(chuàng)建一個轉(zhuǎn)換流
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "GBK");
    outputStreamWriter.write("智游教育");

    //使用InputStreamReader  來讀取源文件的數(shù)據(jù)
    FileInputStream fis = new FileInputStream(pathname);
    InputStreamReader inputStreamReader = new InputStreamReader(fis,"GBK");
    char[] chars=new char[10];
    inputStreamReader.read(chars);
    System.out.println(chars);
    outputStreamWriter.flush();
}

緩沖流
不使用緩沖流

public static void m1()throws Exception{
    String pathname="D:\\javaProject\\recv\\智游-張奎龍\\apache-tomcat-8.5.51-windows-x64.zip";
    FileInputStream fis = new FileInputStream(pathname);
    String pathname2="D:\\javaProject\\recv\\智游-張奎龍\\copy\\新建 文本文檔.zip";
    FileOutputStream fos = new FileOutputStream(pathname2);
    //每次讀取一個字節(jié)數(shù)組
    byte[] bytes=new byte[1024];
    int length;
    long start = System.currentTimeMillis();
    while ((length=fis.read(bytes))!=-1){
        fos.write(bytes,0,length);
    }
    long end = System.currentTimeMillis();
    System.out.println("花費(fèi)的時間"+(end-start));
    //關(guān)閉流
    fos.close();
    fis.close();
}

使用緩沖流

private static void m2()throws Exception{
    String pathname="D:\\javaProject\\recv\\智游-張奎龍\\apache-tomcat-8.5.51-windows-x64.zip";
    FileInputStream fis = new FileInputStream(pathname);
    String pathname2="D:\\javaProject\\recv\\智游-張奎龍\\copy\\新建 文本文檔.zip";
    FileOutputStream fos = new FileOutputStream(pathname2);
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    //每次讀取一個字節(jié)數(shù)組
    byte[] bytes=new byte[1024];
    int length;
    long start = System.currentTimeMillis();
    while ((length=bis.read(bytes))!=-1){
        bos.write(bytes,0,length);
    }
    long end = System.currentTimeMillis();
    System.out.println("花費(fèi)的時間"+(end-start));
    bos.close();
    bis.close();
}

模擬登錄 判斷用戶名和密碼是否正確

public static void m6() throws Exception{
    // 準(zhǔn)備一個路徑
    String pathname = "D:\javaProject\recv\智游-張奎龍\copy\新建 文本文檔.txt";
    // 準(zhǔn)備一個文件字符輸入流對象
    FileReader fr = new FileReader(pathname);
    // 準(zhǔn)備一個緩沖字符輸入流對象
    BufferedReader br = new BufferedReader(fr);
    // 輸入用戶名和面值
    Scanner scanner = new Scanner(System.in);
    System.out.println("請輸入用戶名");
    String username = scanner.nextLine();
    System.out.println("請輸入密碼");
    String password = scanner.nextLine();
    // 判斷用戶名和密碼值是否存在
    // 使用循環(huán)讀取
    String data = null;
    // 定義一個開關(guān)
    boolean flag = false;
    while ((data = br.readLine()) != null) {// readLine()返回值為null--->讀取到流的末尾
        //張三,123456
        String[] user = data.split(",");
        // 第一個值就是用戶名  第二個值就是密碼值
        if (user[0].equals(username) && user[1].equals(password)) {
            flag = true;
        }
    }
    if (flag) {
        System.out.println("登錄成功");
    } else {
        System.out.println("用戶名或者密碼不正確!!");
    }
}

Properties
1.它是一個屬性類砰嘁,屬于HashTable類的一個子類件炉,可以使用Map接口當(dāng)中所有的方法
2.它的鍵值對類型都是字符串
3.常用API方法
store() --->把屬性集中的數(shù)據(jù)存儲到文件中
load() ---->把文件中的數(shù)據(jù)讀取到Properties屬性集中
getProperty() ---->獲取屬性值
setProperty() ---->設(shè)置屬性值
//Properties 把屬性集中的數(shù)據(jù)存儲到properties文件中
private static void m7()throws Exception{
//創(chuàng)建一個屬性集
Properties properties = new Properties();
properties.setProperty("張三","123456");
properties.put("李四","111111");
//把properties屬性集當(dāng)中的值存儲到一個文件當(dāng)中
String pathname="day22\src\data.properties";
// 準(zhǔn)備一個文件字節(jié)輸出流對象
FileOutputStream fos = new FileOutputStream(pathname);
properties.store(fos,"data message");
fos.close();
}
//把properties文件中的數(shù)據(jù)讀取到Properties屬性集中
private static void m8()throws Exception{
//創(chuàng)建一個屬性集
Properties properties = new Properties();
// 準(zhǔn)備一個輸入流對象
FileInputStream fis = new FileInputStream("day22\src\data.properties");
properties.load(fis);
String v1 = properties.getProperty("張三");
String v2 = properties.getProperty("李四");
System.out.println(v1+"--"+v2);

}

對象序列化與反序列化
private static void m9()throws Exception{
Student s1=new Student(1,"張三1",98);
//把s1作為一個整體存儲到文件當(dāng)中 序列化
String pathname="day22\src\student.txt";
//創(chuàng)建一個字節(jié)輸出流對象
FileOutputStream fos = new FileOutputStream(pathname);
//可以進(jìn)行序列化對象
ObjectOutputStream oos = new ObjectOutputStream(fos);
//把對象數(shù)據(jù)寫入到到day22\src\student.txt當(dāng)中
oos.writeObject(s1);
//把文件當(dāng)中的對象數(shù)據(jù)讀取到內(nèi)存當(dāng)中 反序列化
// 創(chuàng)建一個輸入流對象
FileInputStream fis = new FileInputStream("day22\src\student.txt");
// 創(chuàng)建一個對象輸入流
ObjectInputStream ois = new ObjectInputStream(fis);
// 從反序列化流中讀取對象數(shù)據(jù)
Student s11 =(Student) ois.readObject();
System.out.println(s11);
// 關(guān)閉流對象
ois.close();
fis.close();
oos.close();
fos.close();
}
備注:如果類中定義的成員變量為靜態(tài)的勘究,那么該靜態(tài)成員變量是不能進(jìn)行序列化的
原因:因?yàn)殪o態(tài)成員變量不屬于對象,儲存的位置不在對象當(dāng)中斟冕,在方法區(qū)中的靜態(tài)區(qū)中存儲所以我們在進(jìn)行對象數(shù)據(jù)序列化的時候口糕,是不能把方法區(qū)中靜態(tài)區(qū)中的數(shù)據(jù)進(jìn)行序列化,最終靜態(tài)成員變量值是不會存儲到序列化的文件中
類對象在進(jìn)行序列化和反序列化的時候磕蛇,每次jvm都會先比對序列版本號景描,如果序列版本號不一致,那么程序就不允許通過反序列化讀取文件中的對象數(shù)據(jù)
解決辦法:
不讓jvm每次為我生成序列版本號秀撇,由開發(fā)者把序列版本號寫成一個常量值超棺,這樣后續(xù)再次改動類文件結(jié)構(gòu)時,jvm就不會再生成一個新的序列版本號
//使用FilenameUtils工具包
private static void m1()throws Exception{
String pathname="day22\src\io.txt";
String name = FilenameUtils.getName(pathname);
System.out.println(name);
//創(chuàng)建一個File實(shí)例
File file = new File(pathname);
String path = file.getName();
System.out.println(path);
//獲取文件的拓展名
String extension = FilenameUtils.getExtension(path);
System.out.println("文件的拓展名:"+extension);
//判斷是否是以指定的拓展名結(jié)尾
boolean result = FilenameUtils.isExtension(path, "txt");
System.out.println(result);
}

//使用FileUtils工具包
private static void m2()throws Exception{
    String pathname="D:\\javaProject\\recv\\智游-張奎龍\\三國演義 (2).txt";
    File file = new File(pathname);
    String string = FileUtils.readFileToString(new File(pathname), "UTF-8");
    System.out.println(string);
    //整行讀取,從流中全部讀完
    List<String> list = FileUtils.readLines(file, "UTF-8");
    System.out.println(list);
    //把字符串直接寫入到文件當(dāng)中
    String pathname2="day22\\src\\SanGuo2.txt";
    File file2 = new File(pathname2);
    FileUtils.writeStringToFile(file2,"你好呵燕,世界","UTF-8");
    String string2 = FileUtils.readFileToString(file2, "UTF-8");
    System.out.println(string2);
    FileUtils.writeStringToFile(file2,"你好棠绘,世界","UTF-8",true);
    //拷貝文件夾
    String pathname3="D:\\javaProject\\recv\\智游-張奎龍\\三國分章節(jié)";
    //目標(biāo)文件夾
    String pathname4="D:\\javaProject\\recv\\智游-張奎龍\\新建文件夾";
    FileUtils.copyDirectory(new File(pathname3),new File(pathname4));
    //拷貝文件
    String pathname5="D:\\javaProject\\recv\\智游-張奎龍\\三國分章節(jié)\\000 三國演義 上卷.txt";
    String pathname6="D:\\javaProject\\recv\\智游-張奎龍\\新建文件夾\\000.txt";
    FileUtils.copyFile(new File(pathname5),new File(pathname6));
}

打印流
//使用打印流
private static void m3()throws Exception{
PrintStream printStream = new PrintStream("day22\src\io.txt");
printStream.write("智游教育".getBytes());
printStream.print("智游java");
printStream.append('中');
}
異常:
1.繼承體系:Throwable 異常類(頂級父類):Error錯誤類/Exception異常類
2.Exception異常類
3.編譯器異常(受檢異常,checked):在高級開發(fā)工具中再扭,編寫代碼過程中氧苍,代碼中就包含的異常信息需要進(jìn)行異常處理,否則編譯異常
運(yùn)行期異常(非受檢異常泛范,runtime):在編譯過程中不會發(fā)生異常让虐,只有class文件加載到j(luò)vm虛擬機(jī)才能檢查出的異常
編譯期異常
IO異常--->Exception
運(yùn)行期異常 --->RuntimeException
ArithmeticException 算數(shù)異常
索引越界異常 StringIndexOutOfBoundsException
空指針異常 NullPointerException
4.結(jié)果:
異常在程序當(dāng)中發(fā)生后,會導(dǎo)致程序異常終止罢荡,異常后面的代碼是不會執(zhí)行或加載的
5.異常:
首先jvm會找到匹配的異常類(對象)來接收異常信息
異常對象包括:異常類型赡突、異常產(chǎn)生的位置、異常產(chǎn)生的原因柠傍、異常的解決辦法
6.異常鏈:一個異臭锓可能引發(fā)其他異常
7.解決異常:
通過它的提示信息來定位異常產(chǎn)生的位置--->原因---->日志

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市惧笛,隨后出現(xiàn)的幾起案子从媚,更是在濱河造成了極大的恐慌,老刑警劉巖患整,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拜效,死亡現(xiàn)場離奇詭異,居然都是意外死亡各谚,警方通過查閱死者的電腦和手機(jī)紧憾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昌渤,“玉大人赴穗,你說我怎么就攤上這事。” “怎么了般眉?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵了赵,是天一觀的道長。 經(jīng)常有香客問我甸赃,道長柿汛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任埠对,我火速辦了婚禮络断,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘项玛。我一直安慰自己貌笨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布稍计。 她就那樣靜靜地躺著躁绸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪臣嚣。 梳的紋絲不亂的頭發(fā)上净刮,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天,我揣著相機(jī)與錄音硅则,去河邊找鬼淹父。 笑死,一個胖子當(dāng)著我的面吹牛怎虫,可吹牛的內(nèi)容都是我干的暑认。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼大审,長吁一口氣:“原來是場噩夢啊……” “哼蘸际!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起徒扶,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤粮彤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后姜骡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體导坟,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年圈澈,在試婚紗的時候發(fā)現(xiàn)自己被綠了惫周。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡康栈,死狀恐怖递递,靈堂內(nèi)的尸體忽然破棺而出喷橙,到底是詐尸還是另有隱情,我是刑警寧澤漾狼,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布重慢,位于F島的核電站饥臂,受9級特大地震影響逊躁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜隅熙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一稽煤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧囚戚,春花似錦酵熙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拳芙,卻和暖如春察藐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舟扎。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工分飞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人睹限。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓譬猫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親羡疗。 傳聞我的和親對象是個殘疾皇子染服,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容