JAVA exception異常處理+I/O操作讀寫檔案 筆記
JAVA 異常處理
JAVA的異常5個關鍵字try...catch...finally..throw..throws
捕獲異常 | 宣告異常 | 丟擲異常 | |
try | 執行可能產生異常的程式碼 | throws | throw |
catch | 捕獲異常 | 宣告方法可能要丟擲的的各種異常 | 手動丟擲異常 |
finally | 無論是否發生異常程式碼總能執行 |
所有異常物件都包含了如下幾個常用方法:
getMessage():返回該異常的詳細描述字串。
printStackTrace():將該異常的跟蹤棧資訊輸出到標準錯誤輸出。
printStackTrace(PrintStream s):將該異常的跟蹤棧資訊輸出到指定輸出流。
getStackTrace():返回該異常的跟蹤棧資訊。
try
{
需要檢測的程式碼;
}
catch(異常類 變數)
{
異常處理程式碼;
}
finally
{
一定會執行的程式碼;
}
Finally程式碼塊只有一種情況不會被執行。就是在之前執行了System.exit(0)。
異常處理的巢狀
異常處理流程程式碼可以放在任何能放可執行性程式碼的地方,因此完整的異常處理流程既可放在try塊裡,也可放在catch塊裡,也可放在finally塊裡。
異常處理巢狀的深度沒有很明確的限制,但通常沒有必要使用超過兩層的巢狀異常處理,層次太深的巢狀異常處理沒有太大不要,而且導致程式可讀性降低。
I/O操作讀寫檔案
import java.io.File;
……
File file = new File(args[0]);
System.out.println("檔案或目錄是否存在:" + file.exists());
System.out.println("是檔案嗎:" + file.isFile());
System.out.println("是目錄嗎:" + file.isDirectory());
System.out.println("名稱:" + file .getName());
System.out.println("路徑: " + file.getPath());
System.out.println("絕對路徑: " + file.getAbsolutePath());
System.out.println("最後修改時間:" + file.lastModified());
System.out.println(“檔案大小:” + file.length()+ “ 位元組”);
……
Java的IO流是實現輸入/輸出的基礎,它可以方便地實現資料的輸入/輸出操作,Java中把不同的輸入/輸出源(鍵盤、檔案、網路連線等)抽象表述為“流”(stream),通過流的方式允許Java程式使用相同的方式來訪問不同的輸入/輸出源。stream是從起源(source)到接收(sink)的有序資料。
Java把所有傳統的個流型別(類或抽象類)都放在java.io包中,用以實現輸入/輸出功能。
按照流的流向來分:可以分為輸入流和輸出流。
輸入流:只能從中讀取資料,而不能向其寫出資料。
輸出流:只能向其寫出資料,而不能從中讀取資料。
位元組流和字元流
按照流的角色分,可以分為節點流和處理流。
文字檔案的讀寫
用FileInputStream讀檔案
用FileOutputStream寫檔案
用BufferedReader讀文字檔案
用BufferedWriter寫文字檔案
二進位制檔案的讀寫
DataOutputStream
DataInputStream
IO流的四個基類
Java把所有裝置裡的有序資料抽象成流模型簡化了輸入/輸出的處理。
Java的IO流共涉及到40多個類,這些類看上去蕪雜而凌亂,但實際上是非常規則、而且彼此之間存在非常緊密的聯絡。
Java的IO流的40多個類都是從4個抽象基類派生出來的:
InputStream/Reader:所有輸入流的基類,前者是輸入位元組流,後者是輸入字元流。
OutputStream/Writer:所有輸出流的基類,前者是輸出位元組流,後者是輸出字元流。
輸入流
InputStream和Reader是所有輸入流的基類,它們都是兩個抽象類,本身並不能建立例項來執行輸入,但它們將所謂所有輸入流的模板,所以它們的方法是所有輸入流都可使用的方法。它們包含如下三個方法:
int read():從輸入流中讀取單個位元組(相當於從圖15.5所示水管中取出一滴水),返回所讀取的位元組資料(位元組資料可直接轉換為int型別)。
int read(byte[]/char[] b):從輸入流中讀取最多b.length個位元組的資料,並將其儲存在位元組陣列b中,返回實際讀取的位元組數。
int read(byte[]/char[] b, int off, int len):從輸入流中讀取最多len位元組的資料,並將其儲存在陣列 b 中,放入b陣列中時,並不是從陣列起點開始,而是從off位置開始,返回實際讀取的位元組數。
輸出流
OutputStream和Writer也非常相似,它們採用如圖15.6所示的模型來執行輸出,兩個流都提供瞭如下三個方法:
void write(int c):將指定的位元組/字元輸出到輸出流中,其中c既可以代表位元組,也可以代表字元。
void write(byte[]/char[] buf):將位元組陣列/字元陣列中的資料輸出到指定輸出流中。
void write(char[] cbuf, int off, int len):將位元組陣列/字元陣列中從off位置開始,長度為len的位元組/字元輸出到輸出流中。
package ch07;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystemException;
public class FileInputStreamTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis=null;
try {
fis=new FileInputStream("E:\\新建資料夾\\12.txt");
StringBuffer sbff=new StringBuffer();
int next=0;
while ((next=fis.read())!=-1){
sbff.append((char)next);
}
System.out.println(sbff);
} catch (FileSystemException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package ch07;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis=null;
try {
fis=new FileInputStream("E:\\新建資料夾\\12.txt");
byte[] b=new byte[30];
StringBuffer sbff=new StringBuffer();
while(fis.read(b)!=-1){
sbff.append(new String(b));
}
System.out.println(sbff);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
讀某資料夾裡內容
在檔案裡寫內容
package ch07;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FilOutputStreamTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream fos = null;
String name = "E:\\新建資料夾\\1234.txt";
try {
fos = new FileOutputStream(name); //清空後加(name)//追加(name,true)
String str = "dhahashdsd中國";
fos.write(str.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
複製文字
package ch07;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class InputOuput {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis=null;
FileOutputStream fos = null;
String inputname = "E:\\新建資料夾\\1234.txt";
String ouputname = "E:\\新建資料夾\\123455.txt";
try {
fis=new FileInputStream(inputname);
fos = new FileOutputStream(ouputname);
byte[] b=new byte[3000];
StringBuffer sbff=new StringBuffer();
while(fis.read(b)!=-1){
sbff.append(new String(b));
}
String str = sbff.toString();
fos.write(str.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{try {
fis.close();
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
複製圖片
package ch07;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class inputouputtupian {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis=null;
FileOutputStream fos = null;
String inputname = "E:\\新建資料夾\\123.jpg";
String ouputname = "E:\\新建資料夾\\123455.jpg";
try {
fis=new FileInputStream(inputname);
fos = new FileOutputStream(ouputname);
//byte[] b=new byte[3000000];
//StringBuffer sbff=new StringBuffer();
int next=0;
while((next=fis.read())!=-1){
fos.write(next);
}
// String str = sbff.toString();
// fos.write(str.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{try {
fis.close();
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
作者:沖天之峰 20160603