10.IO流(代碼解析)

10.IO流(代碼解析)

(適於電腦觀看)

File

/*
* java.io.File類
* 1.凡是與輸入、輸出相關的類、接口等都定義在java.io包下
* 2.File是一個類,可以有構造器創建其對象。此對象對應著一個文件(.txt .avi .doc .ppt .mp3 .jpg)或文件目錄
* 3.File類對象是與平臺無關的。
* 4.File中的方法,僅涉及到如何創建、刪除、重命名等等。只要涉及文件內容的,File是無能為力的,必須由io流來完成。
* 5.File類的對象常作為io流的具體類的構造器的形參。
*/
public class TestFile {
/*
* createNewFile() delete() mkDir():創建一個文件目錄。只有在上層文件目錄存在的情況下,才能返回true
* mkDirs():創建一個文件目錄。若上層文件目錄不存在,一併創建 list() listFiles()
*/
@Test
public void test3() throws IOException {
File file1 = new File("d:/io/helloworld.txt");
System.out.println(file1.delete());
if (!file1.exists()) {
boolean b = file1.createNewFile();
System.out.println(b);
}
File file2 = new File("d:\\io1\\io2");
if (!file2.exists()) {

boolean b = file2.mkdirs();
System.out.println(b);
}
File file3 = new File("d:\\teach");
String[] strs = file3.list();
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
System.out.println();
File[] files = file3.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getName());
}
}
/*
* exists() canWrite() canRead() isFile() isDirectory() lastModified() length()
*
*/
@Test
public void test2() {
File file1 = new File("d:/io/helloworld.txt");
File file2 = new File("d:\\io\\io1");
System.out.println(file1.exists());
System.out.println(file1.canWrite());
System.out.println(file1.canRead());
System.out.println(file1.isFile());
System.out.println(file1.isDirectory());
System.out.println(new Date(file1.lastModified()));
System.out.println(file1.length());
System.out.println();
System.out.println(file2.exists());
System.out.println(file2.canWrite());
System.out.println(file2.canRead());
System.out.println(file2.isFile());
System.out.println(file2.isDirectory());
System.out.println(new Date(file2.lastModified()));
System.out.println(file2.length());
}
/*
* 路徑: 絕對路徑:包括盤符在內的完整的文件路徑 相對路徑:在當前文件目錄下的文件的路徑
*
* getName() getPath() getAbsoluteFile() getAbsolutePath() getParent()
* renameTo(File newName)
*
*/
@Test

public void test1() {
File file1 = new File("d:/io/helloworld.txt");
File file2 = new File("hello1.txt");
File file3 = new File("d:\\io\\io1");
File file4 = new File("d:\\io2");
System.out.println(file1.getName());
System.out.println(file1.getPath());
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getParent());
System.out.println(file1.getAbsolutePath());
System.out.println();
System.out.println(file3.getName());
System.out.println(file3.getPath());
System.out.println(file3.getAbsoluteFile());
System.out.println(file3.getParent());
System.out.println(file3.getAbsolutePath());
// renameTo(File newName):重命名
// file1.renameTo(file2):file1重命名為file2.要求:file1文件一定存在,file2一定不存在
boolean b = file1.renameTo(file2);
System.out.println(b);
boolean b1 = file4.renameTo(file3);
System.out.println(b1);
}
}

FileInputOutputStream

/*
* 1.流的分類:
* 按照數據流向的不同:輸入流 輸出流
* 按照處理數據的單位的不同:字節流 字符流(處理的文本文件)
* 按照角色的不同:節點流(直接作用於文件的) 處理流
*
* 2.IO的體系
* 抽象基類 節點流(文件流) 緩衝流(處理流的一種)

* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream
* Reader FileReader BufferedReader
* Writer FileWriter BufferedWriter
*/
public class TestFileInputOutputStream {
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
// String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
// String dest = "C:\\Users\\shkstart\\Desktop\\2.avi";
String src = "dbcp.txt";
String dest = "dbcp2.txt";
copyFile(src,dest);
long end = System.currentTimeMillis();
System.out.println("花費的時間為:" + (end - start));//3198
}

// 實現文件複製的方法
public void copyFile(String src, String dest) {
// 1.提供讀入、寫出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相應的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.實現文件的複製
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 從硬盤讀取一個文件,並寫入到另一個位置。(相當於文件的複製)
@Test
public void testFileInputOutputStream() {
// 1.提供讀入、寫出的文件
File file1 = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
File file2 = new File("C:\\Users\\shkstart\\Desktop\\2.jpg");
// 2.提供相應的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.實現文件的複製
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
}
// FileOutputStream
@Test
public void testFileOutputStream() {
// 1.創建一個File對象,表明要寫入的文件位置。
// 輸出的物理文件可以不存在,當執行過程中,若不存在,會自動的創建。若存在,會將原有的文件覆蓋
File file = new File("hello2.txt");
// 2.創建一個FileOutputStream的對象,將file的對象作為形參傳遞給FileOutputStream的構造器中
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
// 3.寫入的操作
fos.write(new String("I love China!").getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.關閉輸出流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputStream3() { // abcdefgcde
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];// 讀取到的數據要寫入的數組。
int len;// 每次讀入到byte中的字節的長度
while ((len = fis.read(b)) != -1) {

// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 使用try-catch的方式處理如下的異常更合理:保證流的關閉操作一定可以執行
@Test
public void testFileInputStream2() {
// 2.創建一個FileInputStream類的對象
FileInputStream fis = null;
try {
// 1.創建一個File類的對象。
File file = new File("hello.txt");
fis = new FileInputStream(file);
// 3.調用FileInputStream的方法,實現file文件的讀取。
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.關閉相應的流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}
// 從硬盤存在的一個文件中,讀取其內容到程序中。使用FileInputStream
// 要讀取的文件一定要存在。否則拋FileNotFoundException
@Test
public void testFileInputStream1() throws Exception {
// 1.創建一個File類的對象。
File file = new File("hello.txt");
// 2.創建一個FileInputStream類的對象
FileInputStream fis = new FileInputStream(file);
// 3.調用FileInputStream的方法,實現file文件的讀取。
/*
* read():讀取文件的一個字節。當執行到文件結尾時,返回-1
*/
// int b = fis.read();
// while(b != -1){
// System.out.print((char)b);
// b = fis.read();
// }
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
// 4.關閉相應的流
fis.close();
}
}

FileReaderWriter

/*
* 使用FileReader、FileWriter 可以實現文本文件的複製。
* 對於非文本文件(視頻文件、音頻文件、圖片),只能使用字節流!

*/
public class TestFileReaderWriter {
@Test
public void testFileReaderWriter(){
//1.輸入流對應的文件src一定要存在,否則拋異常。輸出流對應的文件dest可以不存在,執行過程中會自動創建
FileReader fr = null;
FileWriter fw = null;
try{
//不能實現非文本文件的複製
// File src = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
// File dest = new File("C:\\Users\\shkstart\\Desktop\\3.jpg");
File src = new File("dbcp.txt");
File dest = new File("dbcp1.txt");
//2.
fr = new FileReader(src);
fw = new FileWriter(dest);
//3.
char[] c = new char[24];
int len;
while((len = fr.read(c)) != -1){
fw.write(c, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test

public void testFileReader(){
FileReader fr = null;
try {
File file = new File("dbcp.txt");
fr = new FileReader(file);
char[] c = new char[24];
int len;
while((len = fr.read(c)) != -1){
String str = new String(c, 0, len);
System.out.print(str);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fr != null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}
Buffered
/*
* 抽象基類 節點流(文件流) 緩衝流(處理流的一種,可以提升文件操作的效率)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream (flush())
* Reader FileReader BufferedReader (readLine())
* Writer FileWriter BufferedWriter (flush())
*/
public class TestBuffered {
@Test
public void testBufferedReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("dbcp.txt");
File file1 = new File("dbcp3.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 (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}

@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
// String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
// String dest = "C:\\Users\\shkstart\\Desktop\\3.avi";
String src = "C:\\Users\\shkstart\\Desktop\\實驗.doc";
String dest = "C:\\Users\\shkstart\\Desktop\\實驗1.doc";
copyFile(src,dest);
long end = System.currentTimeMillis();

System.out.println("花費的時間為:" + (end - start));//746
}

//使用緩衝流實現文件的複製的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供讀入、寫出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想創建相應的節點流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.將創建的節點流的對象作為形參傳遞給緩衝流的構造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具體的實現文件複製的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.關閉相應的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if(bis != null){
try {
bis.close();
} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}


//使用BufferedInputStream和BufferedOutputStream實現非文本文件的複製
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供讀入、寫出的文件
File file1 = new File("1.jpg");
File file2 = new File("2.jpg");
//2.想創建相應的節點流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.將創建的節點流的對象作為形參傳遞給緩衝流的構造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具體的實現文件複製的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.關閉相應的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}

}

OtherStream

public class TestOtherStream {
/*
* 標準的輸入輸出流:
* 標準的輸出流:System.out
* 標準的輸入流:System.in
*
* 題目:
* 從鍵盤輸入字符串,要求將讀取到的整行字符串轉成大寫輸出。然後繼續進行輸入操作,
* 直至當輸入“e”或者“exit”時,退出程序。
*/
@Test
public void test2(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while(true){

System.out.println("請輸入字符串:");
str = br.readLine();
if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
break;
}
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

/*
* 如何實現字節流與字符流之間的轉換:
* 轉換流:InputStreamReader OutputStreamWriter
* 編碼:字符串 --->字節數組
* 解碼:字節數組--->字符串
*/
@Test
public void test1(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//解碼
File file = new File("dbcp.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);
//編碼
File file1 = new File("dbcp4.txt");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
String str;

while((str = br.readLine()) != null){
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}
}


分享到:


相關文章: