IO流——檔案流(節點流——管子)
阿新 • • 發佈:2018-11-17
FileInputStream讀:檔案——記憶體
兩個方法:
read();
read(byte[] buf);
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class IOStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\data\\logs\\homestay-error.log.2018-05-02");
FileInputStream fis = new FileInputStream(file);
int asc = -1;
while ((asc = fis.read()) != -1) {// read()獲取的是位元組對應的ASC碼,一個位元組一個位元組的讀
System.out.print((char) asc);
}
fis.close();// 關閉流
}
}
一次性讀多個位元組,byte[]作為緩衝區暫存
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class Filestream {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("E:\\data\\logs\\udd-config-admin-error.log");
FileInputStream fis = null;
int lenth = -1;
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024];// 儲存讀取資料的緩衝區。
lenth = fis.read(buf);// read(byte[])返回讀入緩衝區的位元組總數,如果因為已經到達檔案末尾而沒有更多的資料,則返回 -1。
while (lenth != -1) {// 讀到的資料(位元組)存在buf(緩衝區)裡了
System.out.println(new String(buf, 0, lenth));// 讀字串string
lenth = fis.read(buf);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// finally塊中多放資源釋放相關程式碼
try {// 關閉流,釋放記憶體
if (fis != null) {
fis.close();
fis = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileOutputStream寫:記憶體——檔案
write(byte[] b)//清空重寫
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* @author cuijiao
*
*/
public class Filestream {
public static void main(String[] args) {
File file = new File("D:\\data\\test.log");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
System.out.println("請輸入要寫入檔案的內容");
String str = sc.nextLine();
byte[] b = str.getBytes();
try {
fos.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (sc != null) {
sc.close();// 關閉鍵盤輸入
sc = null;
}
if (fos != null) {
fos.close();// 關閉輸出流
fos = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileInputStream、FileOutputStream結合使用實現檔案(圖片)的拷貝
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class Filestream {
public static void main(String[] args) throws IOException {
File src = new File("D:\\data\\test.jpg");
File target = new File("E:\\copy.jpg");
copyFile(src, target);
}
/**
* 拷貝檔案:將src複製到target
*
* @param src
* @param target
* @throws IOException
*/
private static void copyFile(File src, File target) throws IOException {
if (!src.exists()) {
return;
}
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis = null;
FileOutputStream fos = null;
fis = new FileInputStream(src);
fos = new FileOutputStream(target);
byte[] buf = new byte[1024];
int lenth = -1;
lenth = fis.read(buf);// 讀
while (lenth != -1) {
fos.write(buf, 0, lenth);// 寫
lenth = fis.read(buf);// 讀
}
fis.close();
fos.close();
}
}