中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

設(shè)計(jì)logo圖標(biāo)抖音seo推薦算法

設(shè)計(jì)logo圖標(biāo),抖音seo推薦算法,做網(wǎng)站的圖片Pc端和手機(jī)端的區(qū)別,建設(shè)一個(gè)地方門戶網(wǎng)站IO流 凡是與輸入,輸出相關(guān)的類,接口等都定義在java.io包下 1.File類的使用 File類可以有構(gòu)造器創(chuàng)建其對(duì)象,此對(duì)象對(duì)應(yīng)著一個(gè)文件(.txt,.avi,.doc,.mp3等)或文件目錄 File類對(duì)象是與平臺(tái)無(wú)關(guān)的 File中的方法僅涉及到如何創(chuàng)建,…

IO流

凡是與輸入,輸出相關(guān)的類,接口等都定義在java.io包下
在這里插入圖片描述

1.File類的使用

  • File類可以有構(gòu)造器創(chuàng)建其對(duì)象,此對(duì)象對(duì)應(yīng)著一個(gè)文件(.txt,.avi,.doc,.mp3等)或文件目錄

  • File類對(duì)象是與平臺(tái)無(wú)關(guān)的

  • File中的方法僅涉及到如何創(chuàng)建,刪除,重命名等操作,不涉及文件內(nèi)容的修改(需IO流來(lái)操作)

  • File類對(duì)象常作為io流的具體類的構(gòu)造器的形參

常見(jiàn)的方法
在這里插入圖片描述
熟練掌握紅色標(biāo)記的方法
例:

import java.io.File;
import java.sql.Date;import org.junit.Test;
public class test10{@Testpublic void test1(){//絕對(duì)路徑File f1 = new File("C:/Users/Cat God 007/Desktop/hello.txt"); //文件File f2 = new File("C:/Users/Cat God 007/Desktop");//文件目錄//相對(duì)路徑File f3 = new File("hello.txt");System.out.println("=============訪問(wèn)文件名================");System.out.println(f3.getName());//返回文件名          System.out.println(f3.getPath());//返回文件路徑System.out.println(f3.getAbsoluteFile());//返回文件的絕對(duì)路徑System.out.println(f3.getParent());//返回上一級(jí)文件目錄System.out.println(f3.getAbsolutePath());//返回完整的文件路徑System.out.println("=============================");System.out.println(f2.getName());//返回文件目錄名System.out.println(f2.getPath());//返回文件目錄路徑System.out.println(f2.getAbsoluteFile());//返回文件目錄的絕對(duì)路徑System.out.println(f2.getParent());//返回上一級(jí)文件目錄System.out.println(f2.getAbsolutePath());//返回完整的文件目錄路徑System.out.println("============文件檢測(cè)=========");System.out.println(f1.exists());//檢測(cè)文件是否存在System.out.println(f1.canRead());//檢測(cè)文件是否可讀System.out.println(f1.canWrite());//檢測(cè)文件是否可寫(xiě)System.out.println(f1.isFile());//檢測(cè)此對(duì)象是否不是文件System.out.println(f1.isDirectory());//檢測(cè)此對(duì)象是否不是文件目錄System.out.println("============獲取常規(guī)文件信息=========");System.out.println(new Date(f1.lastModified()));//獲取文件最后修改時(shí)間System.out.println(f1.length());//獲取文件大小}@Testpublic void test2(){File f1 = new File("C:/Users/Cat God 007/Desktop/hello.txt"); File f2 = new File("C:/Users/Cat God 007/Desktop/test/tes1-test9");System.out.println(f1.delete());//刪除文件if(!f1.exists()){boolean b1 = f1.createNewFile();//創(chuàng)建文件System.out.println(b1);}if(!f2.exists()){boolean b2 = f2.mkdir();//mkdirs()可以遞歸創(chuàng)建文件夾,mkdir只創(chuàng)建最后的文件目錄,若它上層沒(méi)有創(chuàng)建,則它也不會(huì)創(chuàng)建System.out.println(b2);}File f3 = new File("C:\\Users\\Cat God 007\\Desktop\\javacode\\day13");String[] list = f3.list();for(int i = 0;i < list.length;i++){System.out.println(list[i]);//以String方式讀取出f3下的文件}File[] files = f3.listFiles();for(int i = 0;i < files.length;i++){System.out.println(files[i].getName());//以文件方式讀取出f3下的文件}}
}

2.IO流原理及其分類

  • IO流用來(lái)處理設(shè)備之間的數(shù)據(jù)傳輸
  • 按數(shù)據(jù)單位可分為:字節(jié)流(8bit)字符流(16bit)
  • 按流的流向可分為:輸入流輸出流
  • 按流的角色可分為:節(jié)點(diǎn)流(直接作為于文件),處理流
抽象基類字節(jié)流字符流
輸入流InputStreamReader
輸出流OutputStreamWriter
	IO流設(shè)計(jì)40多個(gè)類,都是從以上4個(gè)抽象基類派生由這四個(gè)類派生出的子類名稱都是以其父類名作為子類名后綴

IO流體系

在這里插入圖片描述

  • 訪問(wèn)文件的類也被稱為節(jié)點(diǎn)流,文件流,其他類被稱為處理流

3.文件流

FileInputStream

例:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.junit.Test;public class test11{@Test
//讀取硬盤文件內(nèi)容到程序中,使用FileInputStream(讀取的文件一定要存在,否則會(huì)報(bào)文件找不到的異常)
public void test1()throws Exception{//1.創(chuàng)建Filen類型的對(duì)象File file = new File("hello.txt");//2.創(chuàng)建FileInputStream類型的對(duì)象FileInputStream files = new FileInputStream(file);      //3.調(diào)用FileInputStream的方法,實(shí)現(xiàn)file文件的讀取//read():讀取文件的一個(gè)字節(jié),當(dāng)執(zhí)行到文件結(jié)尾時(shí),返回-1//方式一int b = files.read();while(b != -1){System.out.println((char)b);//int轉(zhuǎn)字符(否則打印出來(lái)的是對(duì)應(yīng)的Ascll碼)b = files.read();}//方式二int c;while((c = files.read()) != -1){System.out.println((char)c);}//4.關(guān)閉相應(yīng)的流files.close();
}@Test//測(cè)試test1的優(yōu)化,確保每次流都能被關(guān)閉public void test2(){FileInputStream files = null;try {File file = new File("hello.txt");files = new FileInputStream(file);int c;while((c = files.read()) != -1){System.out.println((char)c);}} catch (IOException e) {e.printStackTrace();}finally{if(files != null){try{files.close();}catch(IOException e){e.printStackTrace();}}
}
}@Testpublic void test3(){FileInputStream files = null;try{File file = new File("hello.txt");files = new FileInputStream(file);byte[] b = new byte[5];//將讀取到的數(shù)據(jù)寫(xiě)入數(shù)組中int len;//每次讀入到byte中的字節(jié)長(zhǎng)度while((len = files.read(b)) != -1){// 方式一:運(yùn)行for循環(huán)實(shí)現(xiàn)遍歷輸出// 成功案例:for(int i = 0;i < len;i++){System.out.print((char)b[i]);}//錯(cuò)誤案例:使用b.length時(shí),在讀取到最后(此時(shí)只讀取了1個(gè)元素),但依舊會(huì)傳入5個(gè)元素的數(shù)組{1個(gè)新元素+4個(gè)舊元素}// for(int i = 0;i < b.length;i++){//     System.out.print((char)b[i]);// }//方式二:運(yùn)行String構(gòu)造器實(shí)現(xiàn)遍歷輸出// String str = new String(b, 0, len);// System.out.println(str);}}catch(IOException e){e.printStackTrace();}finally{if(files != null){try{files.close();}catch(IOException e){e.printStackTrace();}}}}
}

FileOutputStream

簡(jiǎn)單編寫(xiě)

import java.io.File;
import java.io.FileOutputStream;
import org.junit.Test;public class test12{@Testpublic void testFileOutputSteam(){//1.創(chuàng)建要寫(xiě)入文件的文件路徑的File對(duì)象,此文件路徑可以不存在(會(huì)自動(dòng)創(chuàng)建),若存在,就會(huì)用新寫(xiě)入的數(shù)據(jù)覆蓋原來(lái)的數(shù)據(jù)File file = new File("hello1.txt");//2.創(chuàng)建FileOutputStream對(duì)象,將之前的File對(duì)象作形參傳入FileOutputStream的構(gòu)造器中FileOutputStream f = null;try{   f = new FileOutputStream(file);//3.寫(xiě)入數(shù)據(jù)f.write(new String("I Love China").getBytes());//這里用到了字符串轉(zhuǎn)字節(jié)數(shù)組}catch(Exception e){e.printStackTrace();}finally{//關(guān)閉輸出流if(f != null){try{f.close();}catch(Exception e){e.printStackTrace();}}}}
}

練習(xí):編寫(xiě)非文本文件復(fù)制的方法
主要實(shí)現(xiàn)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.Test;public class test12{@Test//從硬盤中讀入數(shù)據(jù),將此數(shù)據(jù)寫(xiě)入到另一位置(相當(dāng)文件復(fù)制)public void testFileInputOutputSteam(){//1.提供讀,寫(xiě)的文件路徑File file1 = new File("C:\\Users\\Cat God 007\\Desktop\\t1.jpg");File file2 = new File("C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\tt.jpg");//2.提供讀,寫(xiě)流FileOutputStream fos = null;FileInputStream fis = null;try{ fis = new FileInputStream(file1);fos = new FileOutputStream(file2);//3.實(shí)現(xiàn)文件復(fù)制byte [] b = new byte[20];int len;while ((len = fis.read(b)) != -1) {fos.write(b,0,len);                }}catch(Exception e){e.printStackTrace();}finally{//4.關(guān)閉讀,寫(xiě)流if(fos != null){try{fos.close();}catch(Exception e){e.printStackTrace();}}if(fis != null){try{fis.close();}catch(Exception e){e.printStackTrace();}}}}

最后包裝成方法,并進(jìn)行測(cè)試

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class test12{public static void main(String[] args) {  testFileInputOutputSteam("C:\\Users\\Cat God 007\\Desktop\\t1.jpg", "C:\\Users\\Cat God 007\\Desktop\\tt1.jpg");}public static void testFileInputOutputSteam(String src,String dest){File file1 = new File(src);File file2 = new File(dest);FileOutputStream fos = null;FileInputStream fis = null;try{fis = new FileInputStream(file1);fos = new FileOutputStream(file2);byte [] b = new byte[20];int len;while ((len = fis.read(b)) != -1) {fos.write(b,0,len);                }}catch(Exception e){e.printStackTrace();}finally{if(fos != null){try{fos.close();}catch(Exception e){e.printStackTrace();}}if(fis != null){try{fis.close();}catch(Exception e){e.printStackTrace();}}}}

FileReader,FileWriter(字符流)

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.junit.Test;public class test13{//使用FileReader,FileWriter可以實(shí)現(xiàn)文本文件的復(fù)制@Testpublic void testFileReaderWriter(){    FileReader fr = null;FileWriter fw = null; try{File src = new File("hello.txt");//讀File desc = new File("hello1.txt");//寫(xiě)fr = new FileReader(src);fw = new FileWriter(desc);char[] c = new char[20];int len;while ((len = fr.read(c)) != -1) {fw.write(c,0,len);                }}catch(Exception e){e.printStackTrace();}finally{if(fr != null){try{fr.close();}catch(Exception e){e.printStackTrace();}}if(fw != null){try{fw.close();}catch(Exception e){e.printStackTrace();}}}
}
}
  • 文本文件用字符流,非文本文件(視頻文件,音頻文件,圖片)用字節(jié)流,效率較高

4.緩沖流(主要使用)

  • 可以提高處理數(shù)據(jù)的效率

  • 每次處理完后,都需要刷新(flush())數(shù)組,方便下次元素不夠?qū)懭氲那闆r

使用 BufferedInputStream,BufferedOutputStream 實(shí)現(xiàn)非文本文件的復(fù)制

例:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.Test;public class test14{@Testpublic void testBufferedInputOutputStream(){BufferedInputStream bis = null;BufferedOutputStream  bos = null;try{            //1.提供讀寫(xiě)文件File file1 = new File("C:\\Users\\Cat God 007\\Desktop\\t1.jpg");File file2 = new File(".\\1.jpg");//2.創(chuàng)建相應(yīng)的節(jié)點(diǎn)流FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);//3.將創(chuàng)建的節(jié)點(diǎn)流的對(duì)象作為形參傳遞給緩沖流的構(gòu)造器中bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//4.實(shí)現(xiàn)文本文件byte[] b = new byte[1024];int len;while ((len = bis.read(b)) != -1) {bos.write(b, 0, len);bos.flush();//刷新一下}}catch(Exception e){e.printStackTrace();}finally{//5.關(guān)閉相應(yīng)的流if(bis != null){try{bis.close();}catch(Exception e){e.printStackTrace();}}if(bos != null){try{bos.close();}catch(Exception e){e.printStackTrace();}}}
}
}
}

使用BufferedReader,BufferedWriter實(shí)現(xiàn)文本文件的復(fù)制

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.Test;public class test{
// BufferedReader()的readLine()方法(一行一行讀)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.junit.Test;public class test14{@Testpublic void testBufferedReader(){BufferedReader br = null;BufferedWriter bw = null;try{        File file = new File("hello.txt");File file1 = new File("hell1o.txt");FileReader fr = new FileReader(file);FileWriter fw = new FileWriter(file1);br = new BufferedReader(fr);bw = new BufferedWriter(fw);// char[] c = new char[1024];// int len;// while ((len = br.read(c)) != -1) {//     String str = new String(c, 0, len);//     System.out.print(str);// }String str;while ((str = br.readLine()) != null) {//System.out.println(str);bw.write(str + "\n");//bw.newLine();//換行bw.flush();}}catch(Exception e){e.printStackTrace();}finally{if(bw != null){try{bw.close();}catch(Exception e){e.printStackTrace();}}if(br != null){try{br.close();}catch(Exception e){e.printStackTrace();}}}}
}

5.轉(zhuǎn)換流

  • 提供在字節(jié)流和字符流之間的轉(zhuǎn)換

  • 字節(jié)流中的數(shù)據(jù)都是字符時(shí),轉(zhuǎn)換成字符流操作更高效

  • 編碼(字符串====>字節(jié)數(shù)組),解碼(字節(jié)數(shù)組====>字符串)

  • 提供兩個(gè)轉(zhuǎn)換流InputStreamReader(解碼),OutputStreamWriter(編碼)

例:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;import org.junit.Test;public class test16{@Testpublic void test1(){BufferedReader br = null;BufferedWriter bw = null;try{ //解碼(字節(jié)數(shù)組====>字符串)File file = new File("hello.txt");FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis, "UTF-8");br = new BufferedReader(isr);//編碼(字符串====>字節(jié)數(shù)組)File file1 = new File("hello123.txt");FileOutputStream fos = new FileOutputStream(file1);OutputStreamWriter isw = new OutputStreamWriter(fos, "UTF-8");bw = new BufferedWriter(isw);String str;while ((str = br.readLine()) != null) {//System.out.println(str);isw.write(str + "\n");//bw.newLine();//換行isw.flush();}}catch(Exception e){e.printStackTrace();}finally{if(bw != null){try{bw.close();}catch(Exception e){e.printStackTrace();}}if(br != null){try{br.close();}catch(Exception e){e.printStackTrace();}}}}
}

6.標(biāo)準(zhǔn)的輸入輸出流

  • 標(biāo)準(zhǔn)的輸出流:System.out
  • 標(biāo)準(zhǔn)的輸入流:System.in

例:
在這里插入圖片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;import org.junit.Test;public class test16{@Testpublic void test(){BufferedReader br = null;try{   InputStream is = System.in;//接受傳入的字節(jié)流InputStreamReader isr = new InputStreamReader(is); //字節(jié)轉(zhuǎn)字符br = new BufferedReader(isr); //包裝成帶緩沖的字符流String str;while(true){System.out.println("請(qǐng)輸入字符串:");str = br.readLine();//忽略大小寫(xiě)if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){break;}String str1 = str.toUpperCase();//轉(zhuǎn)化成大寫(xiě)System.out.println(str1);}}catch(IOException e){e.printStackTrace();}finally{if(br != null){try{br.close();}catch(IOException e){e.printStackTrace();}}}}
}

練習(xí)

在一個(gè)目錄下創(chuàng)建test.txt文件,并寫(xiě)入以下內(nèi)容:

云計(jì)算是一個(gè)比較龐大的概念,入門云計(jì)算,首先要從掌握基本概念和基礎(chǔ)知識(shí)開(kāi)始,然后通過(guò)掌握完全面向云計(jì)算的特定供應(yīng)商的平臺(tái)或技術(shù)等重要領(lǐng)域來(lái)增強(qiáng)其專業(yè)知識(shí)水平,
這樣可以更快的學(xué)好云計(jì)算。你需要學(xué)習(xí)這些知識(shí):
計(jì)算機(jī)與網(wǎng)絡(luò)的基礎(chǔ)知識(shí)
安全基礎(chǔ)知識(shí)
編程語(yǔ)言基礎(chǔ)
腳本語(yǔ)言
linux基礎(chǔ)知識(shí)
分布式系統(tǒng)

讀取test.txt文件的內(nèi)容,并打印出來(lái)
復(fù)制test.txt文件為cloudcompute.txt文件

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import org.junit.Test;public class test17{@Test//字節(jié)流寫(xiě)入文本文件數(shù)據(jù)public void test1(){BufferedOutputStream bos = null;try{      File file = new File("C:\\Users\\Cat God 007\\Desktop\\test.txt");FileOutputStream fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);String str = "\u4E91\u8BA1\u7B97\u662F\u4E00\u4E2A\u6BD4\u8F83\u5E9E\u5927\u7684\u6982\u5FF5\uFF0C\u5165\u95E8\u4E91\u8BA1\u7B97\uFF0C\u9996\u5148\u8981\u4ECE\u638C\u63E1\u57FA\u672C\u6982\u5FF5\u548C\u57FA\u7840\u77E5\u8BC6\u5F00\u59CB\uFF0C\u7136\u540E\u901A\u8FC7\u638C\u63E1\u5B8C\u5168\u9762\u5411\u4E91\u8BA1\u7B97\u7684\u7279\u5B9A\u4F9B\u5E94\u5546\u7684\u5E73\u53F0\u6216\u6280\u672F\u7B49\u91CD\u8981\u9886\u57DF\u6765\u589E\u5F3A\u5176\u4E13\u4E1A\u77E5\u8BC6\u6C34\u5E73\uFF0C\r\n" + //"\u8FD9\u6837\u53EF\u4EE5\u66F4\u5FEB\u7684\u5B66\u597D\u4E91\u8BA1\u7B97\u3002\u4F60\u9700\u8981\u5B66\u4E60\u8FD9\u4E9B\u77E5\u8BC6\uFF1A\r\n" + //"\u8BA1\u7B97\u673A\u4E0E\u7F51\u7EDC\u7684\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u5B89\u5168\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u7F16\u7A0B\u8BED\u8A00\u57FA\u7840\r\n" + //"\u811A\u672C\u8BED\u8A00\r\n" + //"linux\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u5206\u5E03\u5F0F\u7CFB\u7EDF";bos.write(str.getBytes());}catch(IOException e){e.printStackTrace();}finally{if(bos != null){try{bos.flush();}catch(Exception e){e.printStackTrace();}}}}@Test//字符流寫(xiě)入文本文件數(shù)據(jù)public void test2(){BufferedWriter bw = null;try{bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\Cat God 007\\Desktop\\test.txt")));String str = "\u4E91\u8BA1\u7B97\u662F\u4E00\u4E2A\u6BD4\u8F83\u5E9E\u5927\u7684\u6982\u5FF5\uFF0C\u5165\u95E8\u4E91\u8BA1\u7B97\uFF0C\u9996\u5148\u8981\u4ECE\u638C\u63E1\u57FA\u672C\u6982\u5FF5\u548C\u57FA\u7840\u77E5\u8BC6\u5F00\u59CB\uFF0C\u7136\u540E\u901A\u8FC7\u638C\u63E1\u5B8C\u5168\u9762\u5411\u4E91\u8BA1\u7B97\u7684\u7279\u5B9A\u4F9B\u5E94\u5546\u7684\u5E73\u53F0\u6216\u6280\u672F\u7B49\u91CD\u8981\u9886\u57DF\u6765\u589E\u5F3A\u5176\u4E13\u4E1A\u77E5\u8BC6\u6C34\u5E73\uFF0C\r\n" + //"\u8FD9\u6837\u53EF\u4EE5\u66F4\u5FEB\u7684\u5B66\u597D\u4E91\u8BA1\u7B97\u3002\u4F60\u9700\u8981\u5B66\u4E60\u8FD9\u4E9B\u77E5\u8BC6\uFF1A\r\n" + //"\u8BA1\u7B97\u673A\u4E0E\u7F51\u7EDC\u7684\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u5B89\u5168\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u7F16\u7A0B\u8BED\u8A00\u57FA\u7840\r\n" + //"\u811A\u672C\u8BED\u8A00\r\n" + //"linux\u57FA\u7840\u77E5\u8BC6\r\n" + //"\u5206\u5E03\u5F0Fw\u7CFB\u7EDF";bw.write(str);}catch(IOException e){e.printStackTrace();}finally{if(bw != null){try{bw.flush();}catch(Exception e){e.printStackTrace();}}}}@Test//字符流輸出文本文件數(shù)據(jù)public void test3(){BufferedReader br = null;try{br = new BufferedReader(new FileReader("C:\\Users\\Cat God 007\\Desktop\\test.txt"));String str;while((str = br.readLine()) != null){System.out.println(str);}}catch(IOException e){e.printStackTrace();}finally{if(br != null){try{br.close();}catch(IOException e){e.printStackTrace();}}}}@Test//字符流實(shí)現(xiàn)復(fù)制public void test4(){BufferedWriter bw = null;BufferedReader br = null;try{br = new BufferedReader(new FileReader("C:\\Users\\Cat God 007\\Desktop\\test.txt"));bw = new BufferedWriter(new FileWriter("C:\\Users\\Cat God 007\\Desktop\\cloudcompute.txt"));String str;while((str = br.readLine()) != null){bw.write(str + "\n");}            }catch(IOException e){e.printStackTrace();}finally{if(bw != null){try{bw.close();}catch(IOException e){e.printStackTrace();}}if(br != null){try{br.close();}catch(IOException e){e.printStackTrace();}}}}}

7.打印流

  • PrintStream:字節(jié)流 printWriter:字符流

例:

public class test18{@Test//打印流public void printStreamTest(){FileOutputStream fos = null;try{    fos = new FileOutputStream("C:\\Users\\Cat God 007\\Desktop\\test.txt");}catch(IOException e){e.printStackTrace();}//創(chuàng)建打印輸出流,設(shè)置為自動(dòng)刷新模式(寫(xiě)入換行符就或字節(jié)'\n'時(shí)都會(huì)刷新輸出緩沖區(qū))PrintStream ps = new PrintStream(fos,true);if(ps != null){//把標(biāo)準(zhǔn)輸出流改成文件System.setOut(ps);//修改輸出打印的位置為ps}for(int i = 0;i < 255;i++){System.out.print((char)i);if(i % 50 == 0){//每50數(shù)據(jù)一行System.out.println();//換行}}ps.close();}
}

8.數(shù)據(jù)流

在這里插入圖片描述

  • 用來(lái)處理基本數(shù)據(jù)類型,String,字節(jié)數(shù)組的數(shù)據(jù)

例:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;import org.junit.Test;public class test19{@Test//數(shù)據(jù)流寫(xiě)入public void testData(){DataOutputStream dos = null;try {          FileOutputStream fos = new FileOutputStream("C:\\Users\\Cat God 007\\Desktop\\test.txt");dos = new DataOutputStream(fos);dos.writeUTF("你好");dos.writeInt(467876543);dos.writeDouble(1289.789);dos.writeBoolean(true);}catch(IOException e){e.printStackTrace();}finally{if(dos != null){try {dos.close();} catch (IOException e){e.printStackTrace();}}}}@Test//數(shù)據(jù)流讀取public void testData1(){DataInputStream dis = null;try {   dis = new DataInputStream(new FileInputStream("C:\\Users\\Cat God 007\\Desktop\\test.txt"));String str = dis.readUTF();System.out.println(str);int i = dis.readInt();System.out.println(i);double d = dis.readDouble();System.out.println(d);boolean b = dis.readBoolean();System.out.println(b);} catch (IOException e) {e.printStackTrace();}finally{if(dis != null){try {dis.close();} catch (IOException e){e.printStackTrace();}}}}
}

9.對(duì)象流

ObjectOutputStream 和 ObjectInputStream

  • 用于存儲(chǔ)和讀取對(duì)象的處理流,可以把Java中的對(duì)象寫(xiě)入到數(shù)據(jù)源中,也能把對(duì)象從數(shù)據(jù)源中還原回來(lái)

  • 序列化(Serialize):用 ObjectOutputStream 類將Java對(duì)象寫(xiě)入IO流中
    在這里插入圖片描述

  • 反序列化(Deserialize):用 ObjectInputStream 類從IO流中恢復(fù)該Java對(duì)象

  • ObjectOutputStream 和
    ObjectInputStream不能序列化static和transient(短暫的)修飾的成員變量

例:
i

mport java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.junit.Test;public class test20{@Test// 反序列化--ObjectInputStreampublic void testObjectInputStream(){ObjectInputStream ois = null;try {     ois = new ObjectInputStream(new FileInputStream("C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\test.txt"));Person p1 = (Person)ois.readObject();//讀取一個(gè)對(duì)象System.out.println(p1.toString());//打印一個(gè)對(duì)象Person p2 = (Person) ois.readObject();//讀取對(duì)象System.out.println(p2);//打印對(duì)象}catch (IOException e){e.printStackTrace();}finally{if(ois != null){try{ois.close();}catch (IOException e){e.printStackTrace();}}}}@Test//對(duì)象序列化---ObjectOutputStreampublic void testObjectOutputStream(){Person p1 = new Person("張三", 20,new Pet("張二"));Person p2 = new Person("李四", 21,new Pet("李三"));ObjectOutputStream oos = null;try{oos = new ObjectOutputStream(new FileOutputStream("C:\\\\Users\\\\Cat God 007\\\\Desktop\\\\test.txt"));oos.writeObject(p1);oos.flush();oos.writeObject(p2);oos.flush();       }catch(Exception e){e.printStackTrace();}finally{if(oos != null){try {oos.close();} catch (IOException e) {e.printStackTrace();}}}}
}
/** 實(shí)現(xiàn)序列化的類:* 1.此類可序列化,需要此類實(shí)現(xiàn)Serializable接口* 2.此類可序列化,需要類的屬性同樣實(shí)現(xiàn)Serializable接口* 3.版本號(hào):凡是實(shí)現(xiàn)Serializable接口的類都有一個(gè)表示序列化版本標(biāo)識(shí)符的靜態(tài)變量,表明類的不同版本間的兼容性,如果沒(méi)有定義,那在運(yùn)行時(shí)會(huì)自動(dòng)生成,如果源碼修改,版本號(hào)(自動(dòng)生成的)可能會(huì)變化* 如:private static final long serialVersionUID = 1234678876543L;   */
class Person implements Serializable{ private static final long serialVersionUID = 12343128876543L; String name;Integer age;Pet pet;public Person(String name, Integer age,Pet pet) { this.name = name;this.age = age; this.pet = pet;}@Overridepublic String toString() { return "Person{" +"name='" + name + '\'' +", age=" + age + '\'' +", pet=" + pet + '\'' +'}';}
}
class Pet implements Serializable{String name;public Pet(String name) {this.name = name;}
}

10.隨機(jī)存取文件流

  • RandomAccessFile類支持"隨機(jī)訪問(wèn)"的方式,程序可以直接跳到文件的任意地方來(lái)讀,寫(xiě)文件

    • 可以向已存在的文件后追加內(nèi)容

    • 支持只訪問(wèn)文件的部分內(nèi)容

  • RandomAccessFile對(duì)象包含一個(gè)記錄指針,用于標(biāo)示當(dāng)前讀寫(xiě)處的位置。RandomAccessFile類類對(duì)象可以自由移動(dòng)記錄指針

    • long getFilePointer():獲取文件記錄指針的當(dāng)前位置

    • void seek(long pos):將文件記錄指針定位到pos位置
      在這里插入圖片描述

例:

import java.io.RandomAccessFile;import org.junit.Test;public class test21{@Test//進(jìn)行文件的讀,寫(xiě)public void test(){RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try{    raf1 = new RandomAccessFile("hello.txt","r");raf2 = new RandomAccessFile("hello1.txt","rw");byte[] b = new byte[4];int len;while((len = raf1.read(b))!=-1){raf2.write(b,0,len);}}catch(Exception e){{e.printStackTrace();}}finally{if(raf2 != null){try{raf2.close();}catch(Exception e){e.printStackTrace();}}if(raf1 != null){try{raf1.close();}catch(Exception e){e.printStackTrace();}}}}@Test//在指定位置進(jìn)行文件的讀寫(xiě),實(shí)際上實(shí)現(xiàn)的是覆蓋的效果public void test1(){RandomAccessFile raf = null;try{raf = new RandomAccessFile("hello.txt","rw");raf.seek(3);//把指針調(diào)到第三個(gè)的位置raf.write("123456789".getBytes());}catch(Exception e) {e.printStackTrace();}finally{if(raf != null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}}@Test//實(shí)現(xiàn)在指定位置的插入效果public void test2(){RandomAccessFile raf = null;try{raf = new RandomAccessFile("hello.txt","rw");raf.seek(3);byte[] b = new byte[10];int len;StringBuffer sb = new StringBuffer();//可變的字符序列,相當(dāng)于Stringwhile((len = raf.read(b)) != -1){sb.append(new String(b,0,len));}raf.seek(3);//把指針調(diào)到第三個(gè)的位置raf.write("123456789".getBytes());//寫(xiě)入要插入的內(nèi)容raf.write(sb.toString().getBytes());//寫(xiě)入原來(lái)的后面字符}catch(Exception e) {e.printStackTrace();}finally{if(raf != null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}}@Test//實(shí)現(xiàn)在指定位置的插入效果(在test2的基礎(chǔ)上進(jìn)行優(yōu)化,使之更通用)public void test3(){RandomAccessFile raf = null;try{raf = new RandomAccessFile("hello.txt","rw");raf.seek(3);String str = raf.readLine();//讀取出要插入處之后的所有字符// long l = raf.getFilePointer();//獲取當(dāng)前指針的位置// System.out.println(l);//12raf.seek(3);//把指針調(diào)到第三個(gè)的位置raf.write("123456789".getBytes());//寫(xiě)入要插入的內(nèi)容raf.write(str.getBytes());//寫(xiě)入原來(lái)的后面字符}catch(Exception e) {e.printStackTrace();}finally{if(raf != null){try{raf.close();}catch(Exception e){e.printStackTrace();}}}}
}

IO流練習(xí)
在這里插入圖片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test1 {public static void main(String[] args) {   test1 t = new test1();System.out.println("請(qǐng)輸入一個(gè)字符串:");String str = t.nextString();System.out.println(str);int j = t.nextInt();System.out.println(j + 20);}public String nextString() {InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);String str = null;try {str = br.readLine();}catch(IOException e) {e.printStackTrace();}return str;}public int nextInt() {return Integer.parseInt(nextString());}public boolean nextBoolean() {return Boolean.parseBoolean(nextString());}}

感謝大家的支持,關(guān)注,評(píng)論,點(diǎn)贊!
參考資料:
尚硅谷宋紅康20天搞定Java基礎(chǔ)下部

http://www.risenshineclean.com/news/6827.html

相關(guān)文章:

  • 做網(wǎng)站什么什么鄭州網(wǎng)絡(luò)推廣平臺(tái)
  • 維度網(wǎng)絡(luò)做網(wǎng)站合肥網(wǎng)絡(luò)推廣外包
  • 男生十大好就業(yè)專業(yè)seo是什么意思知乎
  • 海爾建設(shè)此網(wǎng)站的目的企業(yè)文化建設(shè)方案
  • 手機(jī)屏幕網(wǎng)站重慶seo小z博客
  • 網(wǎng)站排名查詢工具有哪些八百客crm系統(tǒng)登錄入口
  • 自己做網(wǎng)站系統(tǒng)首選平臺(tái)網(wǎng)絡(luò)推廣外包業(yè)務(wù)銷售
  • 網(wǎng)站開(kāi)發(fā)工程師面試問(wèn)哪些問(wèn)題鄭州好的seo外包公司
  • 愛(ài)站查詢工具北京網(wǎng)站sem、seo
  • 新疆網(wǎng)站備案代辦谷歌play商店官網(wǎng)
  • 找人做網(wǎng)站沒(méi)有做好報(bào)案有用嗎電腦優(yōu)化大師
  • 做網(wǎng)站跑matlab程序百度一下網(wǎng)頁(yè)
  • 網(wǎng)站開(kāi)發(fā)計(jì)入無(wú)形資產(chǎn)嗎sem賬戶托管
  • 網(wǎng)站建設(shè)實(shí)力網(wǎng)址檢測(cè)
  • 彩票網(wǎng)站里的統(tǒng)計(jì)怎么做百度推廣引流
  • 網(wǎng)站數(shù)字證書(shū)怎么做百度快照推廣效果怎樣
  • 網(wǎng)站背景怎么做下載谷歌瀏覽器
  • 做游戲視頻網(wǎng)站要批證嗎牛奶推廣軟文文章
  • 最新新聞事件今天國(guó)內(nèi)大事2022自動(dòng)seo系統(tǒng)
  • 網(wǎng)站開(kāi)發(fā)項(xiàng)目計(jì)劃百度快照查詢?nèi)肟?/a>
  • 深圳公明做網(wǎng)站西安網(wǎng)絡(luò)優(yōu)化哪家好
  • 成人本科有學(xué)位證嗎關(guān)鍵詞優(yōu)化需要從哪些方面開(kāi)展
  • 南山公司網(wǎng)站建設(shè)成都優(yōu)化官網(wǎng)公司
  • 做域名后就得做網(wǎng)站嗎市場(chǎng)營(yíng)銷平臺(tái)
  • 重慶大渡口網(wǎng)站建設(shè)長(zhǎng)沙市云網(wǎng)站建設(shè)
  • 營(yíng)銷建設(shè)網(wǎng)站排名優(yōu)化工具
  • 用nat123做自己的網(wǎng)站一鍵seo提交收錄
  • 深圳本地做網(wǎng)站百度網(wǎng)站收錄入口
  • 南陽(yáng)網(wǎng)站seo報(bào)價(jià)cpa推廣平臺(tái)
  • wordpress mysql 應(yīng)用網(wǎng)站如何優(yōu)化一個(gè)關(guān)鍵詞