String類和IO流
1. String類
1.1 比較方式要求
1.2 獲取方法
int length();![字符串比較要求](img/字符串比較要求.png)
獲取字符串長度 ""
char charAt(int index);
獲取String字符串中指定下標位置的char類型字符,如果index超出有效範圍
StringIndexOutOfBoundsException
int indexOf(char ch);
int indexOf(String str);
int indexOf(char ch, int fromIndex);
int indexOf(String str, int fromIndex);
這些方法都是獲取指定元素所在的下標位置,元素可以是char類型字符,也可以是字符串。這裏找出的是指定元素在字符串中第一次出現的位置,當然可以通過一定的約束,從哪個位置開始找fromIndex
int lastIndexOf(char ch);
int lastIndexOf(String str);
int lastIndexOf(char ch, int fromIndex);
int lastIndexOf(String str, int fromIndex);
這些方法都是獲取指定元素所在的下標位置,元素可以是char類型字符,也可以是字符串。這裏找出的是指定元素在字符串中最後一次出現的位置,當然可以通過一定的約束,從哪個位置開始找fromIndex
tips: 最後兩個方法有坑!!!
1.3 判斷方法
boolean endsWith(String str);
判斷當前字符串是不是以指定字符串結尾
boolean isEmpty();
判斷字符串是否爲空 ""空串 JDK1.6之後 null不能讀取,不能寫入,不能調用方法
boolean equals(Object obj);
繼承重寫Object類內的方法,完成字符串要求的比較方式
boolean equalsIgnoreCase(String str);
不區分大小寫比較
boolean contains(String str);
判斷指定字符串是否存在
1.4 轉換方法
String(char[] arr);
使用字符數組中內容創建一個字符串對象
String(char[] arr, int offset, int length);
String(char[] arr, int off, int len);
String(char[] arr, int off, int cou);
String(char[] arg0, int arg1, int arg2);
使用字符數組中內容創建一個字符串對象,offset是從char類型數組中指定下標位置開始獲取數據,獲取的數據長度是length
static String valueOf(char[] arr);
通過類名調用的靜態方法,實際執行的是String(char[] arr);
static String valueOf(char[] arr, int offset, int length);
通過類名調用的靜態方法,實際執行的是String(char[] arr, int offset, int length);
char[] toCharArray();
返回當前字符串對應的字符數組
1.5 其他方法
String replace(char oldChar, char newChar)
替換,替換不會修改原始的字符串,會創建一個新字符串返回,並且替換效果是所有的對應 的oldChar全部替換成newChar ???
String[] split(String regex)
按照指定的字符串切割當前字符串
[00:00:15]XXXXXXX
String substring(int beginIndex)
從指定位置開始,截取子字符串,到字符串末尾
String substring(int beginIndex, int endIndex)
從指定位置開始beginIndex,到endIndex結束,要頭不要尾
String toUpperCase() 轉大寫
字符串小寫轉大寫
String toLowerCase() 轉小寫
字符串大寫轉小寫
String trim() 去除空格
去除字符串兩邊的無用空格
2. IO流
2.1 什麼是IO流
I
input 輸入流
read 讀取數據
O
output 輸出流
write 寫入數據
一般情況下都是按照當前程序使用的內存爲參照物來考慮數據的走向問題。
文件操作爲例
從內存中保存數據到硬盤 output
從硬盤中讀取數據到內存 input
文件操作爲例
1GB完整的文件拷貝過程,要遠遠高於1GB散文件的拷貝過程。
1. 打開文件,關閉文件消耗資源較少
2. 1GB散文件就需要不斷的打開,關閉資源
操作時間的消耗和打開文件,關閉文件或者說打開硬盤,關閉硬盤有直接關係
看視頻,緩衝
使用緩衝可以讓用戶體驗提高,相對來說較爲平和的觀看體驗。
網頁第一次訪問時,加載時間較慢,第二次打開,速度很快
IO流基類
InputStream
輸入流基類
read
OutputStream
輸出流基類
write
2.2 IO流分類
流向分類
輸入輸出
文件操作處理單元分類
字節流和字符流
FileInputStream
文件操作輸入字節流
FileOutputStream
文件操作輸出字節流
FileReader
文件操作輸入字符流
FileWriter
文件操作輸出字符流
2.3 文件操作字節流
2.3.1 文件操作輸入字節流
FileInputStream 文件操作輸入字節流
Constructor 構造方法
FileInputStream(File file);
這裏是根據提供的File類對象創建對應的文件操作輸入字節流。
FileInputStream(String pathName);
這裏是根據提供的String類型文件路徑,創建對應的文件操作輸入字節流。
都會拋出異常:
FileNotFoundException 文件未找到異常。
Method 成員方法
int read();
從文件中讀取一個字節數據返回到方法外。
雖然返回值是一個int類型,但是在整個int類型當中存儲的數據是一個byte類型,有
且只有低8位數據有效
int read(byte[] buf);
讀取文件的內容是存儲在byte類型數組中,返回值是讀取到的字節個數
int read(byte[] buf, int offset, int count);
讀取文件的內容是存儲在byte類型數組中,要求從byte數組offset位置開始,到
count長度結束,返回值是讀取到的字節個數
以上三個方法如果讀取到文件末尾,返回值都是 -1 EOF End Of File
而且以上方法都要異常拋出
IOException IO異常
2.3.2 使用演示
package com. qfedu. c_io;
import java. io. File;
import java. io. FileInputStream;
import java. io. IOException;
public class Demo1 {
public static void main ( String[ ] args) {
long start = System. currentTimeMillis ( ) ;
readTest1 ( ) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "Time : " + ( end - start) ) ;
}
public static void readTest2 ( ) {
File file = new File ( "C:\\aaa\\1.txt" ) ;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream ( file) ;
byte [ ] buf = new byte [ 1024 * 8 ] ;
int length = - 1 ;
while ( ( length = fileInputStream. read ( buf) ) != - 1 ) {
System. out. println ( new String ( buf, 0 , length) ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileInputStream != null) {
try {
fileInputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public static void readTest1 ( ) {
File file = new File ( "C:\\aaa\\1.txt" ) ;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream ( file) ;
int content = - 1 ;
while ( ( content = fileInputStream. read ( ) ) != - 1 ) {
System. out. println ( ( char ) content) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileInputStream != null) {
try {
fileInputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
2.3.3 文件讀取使用緩衝和非緩衝差距
內存的運作速度看做是火箭
硬盤就是一個自行車
以上代碼中,使用緩衝之後,從硬盤中一口氣讀取8KB數據存儲在內存中,供程序使用。
8KB
固態硬盤,4KB對齊。固態硬盤中每一個扇區都是4KB。緩衝這裏是要求CPU讀取兩個4KB數據,對於CPU而言沒有太多壓力。
如果是一個字節一個字節讀取,CPU取出4KB數據,結果有4095無效。
2.3.4 文件操作輸出字節流
FileOutputStream 文件操作輸出字節流
Constructor 構造方法
FileOutputStream(File file);
根據File類對象創建對應的文件輸出字節流對象
FileOutputStream(String pathName);
根據String類型文件路徑創建對應的文件輸出字節流對象
以上兩個構造方法,創建的FileOutputStream是刪除寫/清空寫操作,會將原文件中的內容全部刪除之後,寫入數據。
FileOutputStream(File file, boolean append);
根據File類對象創建對應的文件輸出字節流對象。創建對象時給予append參數爲
true,表示追加寫。
FileOutputStream(String pathName, boolean append);
根據String類型文件路徑創建對應的文件輸出字節流對象,創建對象時給予append參
數爲true,表示追加寫。
FileOutputStream構造方法是擁有創建文件的內容,如果文件存在,不創建,文件不存在且路徑正確,創建對應文件。否則拋出異常FileNotFoundException
Method 成員方法
void write(int b);
寫入一個字節數據到當前文件中,參數是int類型,但是有且只會操作對應的低八位數
據
void write(byte[] buf);
寫入字節數組中的內容到文件中
void write(byte[] buf, int offset, int length);
寫入字節數組中的內容到文件中,從指定的offset開始,到指定長度length
以上方法會拋出異常:IOException
2.3.5 使用演示
package com. qfedu. c_io;
import java. io. File;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
public class Demo2 {
public static void main ( String[ ] args) {
writeTest2 ( ) ;
}
public static void writeTest2 ( ) {
File file = new File ( "C:/aaa/中國加油.txt" ) ;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream ( file) ;
byte [ ] bytes = "武漢加油!中國加油!" . getBytes ( ) ;
byte [ ] arr = { 65 , 66 , 67 , 68 , 69 , 70 , 71 } ;
fileOutputStream. write ( bytes) ;
fileOutputStream. write ( arr, 2 , 3 ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileOutputStream != null) {
try {
fileOutputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public static void wrietTest1 ( ) {
File file = new File ( "C:/aaa/武漢加油.txt" ) ;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream ( file, true ) ;
fileOutputStream. write ( 'D' ) ;
fileOutputStream. write ( 'D' ) ;
fileOutputStream. write ( 'D' ) ;
fileOutputStream. write ( 'D' ) ;
fileOutputStream. write ( 'D' ) ;
fileOutputStream. write ( 'D' ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileOutputStream != null) {
try {
fileOutputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
2.3.6 文件拷貝對比
2.4 文件操作字符流
2.4.1 字符流特徵
字符流 = 字節流 + 解碼過程
字節組合操作 ==> 對應當前環境編碼集的一個字符
如果字符找不到,該數據無效,需要被刪除。
如果是字符內容操作,效率還可以!!!
如果是非字符操作,凶多吉少!!!
字符流操作文件
個人建議,該文件可以使用notepad 記事本打開無亂碼,可以使用字符流操作。
視頻文件,圖片文件,特定格式的文件,都無法使用字符操作。
2.4.2 文件操作輸入字符流
FileReader 文件操作輸入字符流
Constructor 構造方法
FileReader(File file)
根據File類對象創建對應的FileReader字符流輸入對象
FileReader(String pathName)
根據String類型文件路徑創建對應的FileReader字符流輸入對象
如果文件不存在,拋出異常FileNotFoundException
Method 成員方法
int read();
讀取文件中的一個字符數據,通過返回值返回,返回值類型是int類型,但是在int類
型中有且只有低16位數據有效
int read(char[] arr);
讀取文件中的數據保存到字符數組中,返回值類型是讀取到的字符個數
int read(char[] arr, int off, int len);
讀取文件中的數據保存到字符數組中,要求從數組中下標offset開始,到len結束,返
回值類型是讀取到的字符個數
以上方法,如果讀取到文件默認,返回值爲-1 EOF End Of File
如果讀取操作工作中,出現問題,拋出異常IOException
2.4.3 使用演示
package com. qfedu. c_io;
import java. io. File;
import java. io. FileNotFoundException;
import java. io. FileReader;
import java. io. IOException;
public class Demo4 {
public static void main ( String[ ] args) {
long start = System. currentTimeMillis ( ) ;
readTest2 ( ) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "Time : " + ( end - start) ) ;
}
public static void readTest2 ( ) {
FileReader fileReader = null;
try {
fileReader = new FileReader ( new File ( "C:/aaa/3.txt" ) ) ;
char [ ] buf = new char [ 1024 * 4 ] ;
int length = - 1 ;
while ( ( length = fileReader. read ( buf) ) != - 1 ) {
System. out. println ( new String ( buf, 0 , length) ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileReader != null) {
try {
fileReader. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public static void readTest1 ( ) {
FileReader fileReader = null;
try {
fileReader = new FileReader ( new File ( "C:/aaa/3.txt" ) ) ;
int content = - 1 ;
while ( ( content = fileReader. read ( ) ) != - 1 ) {
System. out. println ( ( char ) content) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileReader != null) {
try {
fileReader. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}