java IO位元組流和字元流常見類總結
阿新 • • 發佈:2019-02-20
前述
程式在執行結束後,資料內容就會被記憶體回收掉,從而消失,為了使一些有用的資料可以被下一次呼叫該程式時候直接使用,所以需要引入IO的操作將資料從記憶體中傳輸到磁碟中,從而實現資料的持久化(或者使用資料庫也可以實現資料的持久化)。資料的傳輸都是通過兩種型別的流:輸入流和輸出流,這就是IO。
流的繼承關係圖
需要讀入資料使用輸入流,需要寫入資料使用輸出流;
按照操作的資料型別分類:位元組流和字元流
位元組流可以讀取和寫入任何資料,因為任何資料最終都能以位元組儲存;
字元流只能操作文字型別的檔案,按照字元進行讀取和寫入,方便對字元的操作
常用的一些位元組流子類:
檔案輸入輸出流:FileInputStream 、FileOutputStream
物件輸入輸出流:ObjectInputStream、ObjectOutputStream
常用的一些字元流子類:
檔案輸入輸出流:FileReader FileWriter
快取的檔案輸入輸出流:BufferedReader BufferedWriter
就個人整理以上所述的8中IO類
1、FileInputStream(檔案位元組輸入流)本例需要有原始的檔案,會將該檔案中的內容輸入到eclipse的控制檯中 txt檔案內容:package io; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class ByteInput { public static void main(String[] args) throws IOException { //1、定義要使用的檔案 File file = new File("F:" + File.separator + "byteInput.txt"); file.createNewFile(); //檔案存在的時候不會執行,不存在的時候會執行 //2、定義位元組輸入流指定為檔案輸入流 InputStream input = new FileInputStream(file); byte[] b = new byte[(int) file.length()]; // file.length()獲取檔案的長度返回long型別 int len = input.read(b); input.close(); //3、驗證輸入結果 System.out.println("檔案的內容長度為 : " + len); System.out.println("檔案的內容為: " + new String(b)); } }
控制檯輸出: 2、FileOutputStream(檔案位元組輸出流)實現對檔案內容的逐位元組處理
本例結果:package io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class ByteOutput { public static void main(String[] args) throws IOException{ //1、獲取要操作的檔案 File file=new File("F:"+File.separator+"byteOutput.txt"); file.createNewFile(); //2、寫入指定的內容 String str="I Like Java!"; OutputStream output=new FileOutputStream(file); output.write(str.getBytes(), 0, str.length()); //寫入字串 output.close(); } }
3、FileReader(檔案字元輸入流)實現對檔案內容的逐字元處理
package io;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class CharInput {
public static void main(String[] args) throws IOException {
//1、指定要操作的檔案
File file=new File("F:"+File.separator+"charInput.txt");
file.createNewFile();
//2、指定位元組輸入流
Reader reader=new FileReader(file);
char[] c=new char[(int)file.length()];
int len=reader.read(c);
reader.close();
//3、驗證
System.out.println("字元流讀取檔案的長度為: "+len);
System.out.println("字元流讀取檔案的內容: "+new String(c));
}
}
本例結果
原始檔中的內容:
控制檯結果:
4、FileWriter(檔案字元輸出流)實現對檔案內容的逐字元處理
package io;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class CharOutput {
public static void main(String[] args) throws IOException {
File file = new File("F:" + File.separator + "charOutput.txt");
file.createNewFile();
Writer writer = new FileWriter(file);
writer.write("I Love Basketball!", 0, 18);
writer.close();
}
}
本例結果
5、ObjectInputStream(物件輸入流)本例需要物件實現序列化介面,實現對檔案內容的逐個物件處理 先定義一個實現Serializable介面的pojo實體類
package io;
import java.io.Serializable;
public class StudentInfo implements Serializable{
private String stuno;
private String name;
private Integer age;
public StudentInfo() {
}
public StudentInfo(String stuno, String name, Integer age) {
super();
this.stuno = stuno;
this.name = name;
this.age = age;
}
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "StudentInfo [stuno=" + stuno + ", name=" + name + ", age=" + age + "]";
}
}
定義測試類
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectInput {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file=new File("F:"+File.separator+"object.txt");
file.createNewFile();
ObjectInputStream in=new ObjectInputStream(new FileInputStream(file));
StudentInfo stu=(StudentInfo)in.readObject();
in.close();
System.out.println(stu);
}
}
本例的結果
6、ObjectOutputStream(物件輸出流)本例需要物件實現序列化介面,實現對檔案內容的逐個物件處理
pojo物件同5例中的StudentInfo物件,測試類如下
package io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectOutput {
public static void main(String[] args) throws IOException {
File file=new File("F:"+File.separator+"object.txt");
file.createNewFile();
StudentInfo student=new StudentInfo("10001","zhangsan",20);
ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(file));
output.writeObject(student);
output.close();
}
}
本例結果
本例會在txt檔案中出現亂碼,是由於記事本和eclipse的編碼不同導致,但是當再次存取在eclipse中不會亂碼,所以不必在意亂碼結果。 7、BufferedReader(快取檔案輸入流)實現對檔案內容的逐行處理
package io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class BufferReaderDemo {
public static void main(String[] args) throws IOException {
//指定檔案
File file = new File("F:" + File.separator + "buffered.txt");
file.createNewFile();
//定義需要驗證的變數
int i = 1;
String str;
StringBuffer buffer = new StringBuffer();
//定義逐行讀入的流
BufferedReader br = new BufferedReader(new FileReader(file));
while ((str = br.readLine()) != null) { //逐行讀取並驗證
System.out.println("讀取的行數: " + (i));
buffer.append(str);
System.out.println("第" + (i++) + "行的內容為: " + str);
}
br.close();
//列印最終結果
System.out.println("\n檔案中的全部內容為: "+buffer.toString());
}
}
本例結果
8、BufferedWriter(快取檔案輸出流)實現對檔案內容的逐行處理
package io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException{
//指定檔案
File file=new File("F:"+File.separator+"buffered.txt");
file.createNewFile();
//指定
Writer bw=new BufferedWriter(new FileWriter(file,true));
bw.write("\r\n");
bw.write("XiaoHuangRen like banana!");
bw.write("\r\n");
bw.write("XiaoHuangRen like bana!");
bw.close();
}
}
本例結果
以上的8個例子中,由於5,6兩個例子和7,8兩個例子共用同一個檔案,所以可以先測試輸出流在測試輸入流看結果。
以上的例子均為個人理解,初學者可以參考一下,大神勿噴。。。