Java筆記--I/O操作
Java筆記–輸入、輸出
1,File類
File能新建,刪除,重新命名檔案和目錄,但是不能訪問檔案內容,訪問檔案本身內容需要使用輸入、輸出流
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) throws IOException{
//以當前路徑建立一個File物件
File file = new File(".");
//輸出絕對路徑
System.out.println(file.getAbsolutePath());
//在當前路徑建立一個臨時檔案
File tmpFile = File.createTempFile("aaa", ".txt",file);
//退出時刪除臨時檔案
tmpFile.deleteOnExit();
//使用當前路徑建立一個新的檔案
File newFile = new File("bbb.txt");
newFile.createNewFile();
//列出當前路徑所有檔案和路徑
String[] fileList = file.list();
for(String filename : fileList) {
System.out.println(filename);
}
}
}
2,IO流基礎概念
IO流按照不同的分類方式,可以將流分為三種不同的型別
1,輸入流和輸出流
輸入輸出根據程式執行所在記憶體的角度劃分
輸入流:InputStream,Reader
輸出流:OutputStream,Writer
2,位元組流和字元流
位元組流:操作的資料單元是8位位元組 InputStream OutputStream
字元流:操作的資料單元是16位位元組 Reader Writer
3,字點流和處理流
字點流(低階流):可以從、向一個特定的IO裝置讀、寫資料的流
處理流(高階流,包裝流):對一個已存在的流進行連線或封裝,通過封裝後的流來實現資料讀/寫功能
使用處理流進行輸入/輸出是,程式不會直接連線到實際的資料來源,沒有和實際的I/O節點連線,只要使用相同的處理流,程式就可以採用完全相同的I/O程式碼來訪問不同的資料來源
什麼是流
將輸入輸出裝置抽象成管道,那麼在管道中傳輸的就是流,管道中傳輸的最小介質可分為位元組流和字元流,可以類比為水管和水管中的水,一個水滴可以是位元組流也可以是字元流
3,位元組流和字元流
字元流和位元組流其實差不多,區別是操作的資料單元不一樣
3.1,InputStream和Reader
int read(byte[] b):從輸入流中最多讀取b.length個位元組的資料,並將其儲存在位元組陣列b中,返回實際讀取的位元組數
InputStream的使用:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) throws IOException{
//建立位元組輸入流
FileInputStream fis = new FileInputStream("FileInputStreamTest.java");
//快取陣列
//1024這個數值與檔案編碼有關,防止出現亂碼
byte[] bbuf = new byte[1024];
//實際讀取的位元組數
int hasRead = 0;
while((hasRead = fis.read(bbuf))>0) {
//取出快取陣列中的位元組,將位元組轉換成字串輸入
System.out.println(new String(bbuf,0,hasRead));
}
fis.close();
}
}
結果:控制檯以字串形式顯示FileInputStreamTest.java檔案中的內容
Reader的使用:I/O的正確操作–結束時關閉輸入輸出流
import java.io.FileReader;
public class FileReaderTest {
public static void main(String[] args) {
FileReader fr=null;
try {
//建立字元輸入流
fr = new FileReader("FileReaderTest.java");
//建立長度為32的快取陣列
char[]cbuf = new char[32];
int hasRead=0;
while((hasRead = fr.read(cbuf))>0) {
System.out.print(new String(cbuf,0,hasRead));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
結果:控制檯以字串形式顯示FileReaderTest.java檔案中的內容
3.1,OutputStream和Writer
void write(byte/char[] buf):將位元組/字元陣列中的資料傳輸到指定的輸出流中
OutputStream的使用:
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileOutputStreamTest {
public static void main(String[] args) {
try ( //建立位元組輸入流
FileInputStream fis = new FileInputStream("FileInputStreamTest.java");
//建立位元組輸出流
FileOutputStream fos = new FileOutputStream("FileOutputTest.txt");){
byte[] bbuf = new byte[32];
int hasRead = 0;
while((hasRead = fis.read(bbuf))>0) {
fos.write(bbuf,0,hasRead);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
結果:先將FileInputStreamTest.java檔案的內容讀入快取陣列中,再寫入FileOutputTest.txt檔案中,每讀一次就寫入檔案輸出流
Writer的使用:
import java.io.FileWriter;
public class FileWriteTest {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("FileWriter.txt");){
fw.write("11111\r\n");
fw.write("22222222\r\n");
fw.write("3333333333333\r\n");
fw.write("44444444444444444\r\n");
fw.write("5555555555555555555555\r\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
結果生成FileWriter.txt檔案:
11111
22222222
3333333333333
44444444444444444
5555555555555555555555
4,輸入輸出體系
如果進行輸入/輸出的內容是文字內容,考慮使用字元流;如果進行輸入/輸出的內容是二進位制內容,考慮使用位元組流
4.1,以字串為物理節點的字元I/O操作
String src = "123\n"+"456\n"+"789\n";
StringReader sr = new StringReader(src);
4.2,轉換流
InputStreamReader:將位元組輸入流轉換成字元輸入流
OutputStreamWriter:將位元組輸出流轉換成字元輸出流
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class KeyinTest {
public static void main(String[] args) {
try (
//將System.in物件轉換成Reader物件
InputStreamReader reader = new InputStreamReader(System.in);
//將普通的Reader包裝成BufferedReader
BufferedReader br = new BufferedReader(reader);){
String line = null;
while((line=br.readLine())!=null) {
if(line.equals("exit")) {
System.exit(1);
}
System.out.println("輸出內容為:"+line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java使用System.in代表標準輸入,即鍵盤輸入,但是這個標準輸入流是InputStream類的例項,使用不方便,而且鍵盤輸入的內容你那個是文字內容,所以可以使用InputStreamReader將其轉換為字元輸入流,普通的Reader讀取輸入內容依然不方便,可以將普通的Reader再次包裝BufferedReader
4.2,重定向標準輸入輸出
static void setErr(PrintStream err):重定向標準錯誤輸出流
static void setIn(InputStream in):重定向標準輸入流
static void setOut(PrintStream out):重定向標準輸出流
4.3,實現圖片的複製
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FinalTest {
static FileInputStream fis=null;
static FileOutputStream fos=null;
public static void main(String[] args) {
//圖片地址
String filePath1 = "E:\\Java\\eclipse\\work space"
+ "\\SummerTest\\copytest1\\picture.png";
//圖片複製目的地
String filePath2 = "E:\\Java\\eclipse\\work space"
+ "\\SummerTest\\copytest2\\picture.png";
try {
fis = new FileInputStream(filePath1);
fos= new FileOutputStream(filePath2);
byte[] bbuf = new byte[32];
int hasRead = 0;
while((hasRead=fis.read(bbuf))>0) {
fos.write(bbuf,0,hasRead);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();
fis.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
done! 後面再補充