版權聲明:本文為部落客原創文章,轉載請注明出處。 https://blog.csdn.net/twilight_karl/article/details/53402523
File
路徑分隔符–File.pathSeparator
檔案分隔符–File.seprarator
路徑表示形式
path = “E://xp//test/2.jpg”
path =”E:”+File.separator+”xp”+File.separator….課跨平台的路徑
path = “E:\xp\test\2.jpg”–>path = “E:/xp/test/2.jpg”(推薦的方式)
常用的方法:
getName()
getParent()傳回上一級目錄,沒有上一級傳回空
exists()檔案是否存在
canRead()檔案是否可讀
isFile()
isDirectory() 判斷
length()長度,位元組數/檔案夾無法讀出長度
creatNewFile()建立檔案(傳回boolean類型)(若檔案已經存在傳回false)
delete()删除檔案傳回(boolean)
creatTempFile() 建立臨時檔案
deleteOnExit() 退出時自動删除
mkdir()建立目錄,必須確定父目錄存在
makirs()建立目錄,遞歸建立目錄。
String [] list()傳回檔案名稱
File [] listFiles() 傳回目錄中的檔案
listRoots() 根路徑
FilenameFilter 過濾器
1 位元組流:
輸入流: InputStream ,read(byte[]),read(byte[],int off,int len),close()
FileInputStream()
輸出流: OutputStream,write(byte[]),write(char [] ,int off,int len),flush(),close()
FileOutputStream()
2 字元流:
輸入流: Reader read(char []) ,read(char [] ,int off,int len),close()
FileReader()
輸出流: Writer write(char []),writer(char [] ,int off,int len),flush(),close()
FileWriter()
位元組流
- 建立聯系 File
- 選擇流 檔案輸入流
- 操作
- 關閉
// 讀取檔案
public static void main(String [] args){
File f = new File("D:/桌面/a.txt");
byte [] file = new byte[1024];
InputStream in = null;
try {
in = new FileInputStream(f);
int len = 0;
while(-1 != (len = in.read(file))){
String s = new String(file, 0, len);
System.out.print(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (in!=null)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 選擇流 檔案輸出流 OutputStream FileOutputStream
- 操作 write() flush()
OutputStream 将資料寫入到檔案中,FileOutputStream将資料寫入到流中
InputStream 将資料從檔案中讀取,FileInputStream将從六中讀取資料
FileOutputStream(File , true)将資料追加到檔案中,預設為false
**一定一定一定要記得關閉流***
//寫入到檔案
public static void main(String [] args){
File file = new File("D:/桌面/b.txt");
OutputStream out = null;
try {
out = new FileOutputStream(file,false);
String s = "寫入檔案操作\r\n";
byte [] s2 = s.getBytes();
out.write(s2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//複制檔案
public static void main (String [] args){
File file1 = new File("D:/桌面/tree.jpg");
File file2 = new File("D:/桌面/trees.jpg");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file1);
output = new FileOutputStream(file2);
byte [] data = new byte [1024*4];
int len = 0;
while(-1 != (len = input.read(data))){
output.write(data,0, len);
System.out.println(data);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("沒有找到檔案");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("寫入成功");
}
}
字元流:隻能處理純文字.txt .html
Writer FileWriter
Reader FileReader
1 建立聯系
2 選擇流 Writer FileWriter
3 讀取char[] dlush
4 關閉
FileWriter(File ,true) 追加
append()追加
//FileReader 讀取檔案
public class testFileReader {
public static void main (String [] args){
File file = new File("D:/桌面/switch.txt");
File file2 = new File("D:/桌面/switch2.txt");
Reader input = null;
Writer output = null;
try {
input = new FileReader(file);
char [] data = new char [1024];
int len = 0;
while (-1 != (len = input.read(data))){
String s = new String(data, 0, len);
System.out.println(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("檔案讀取失敗");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("關閉失敗");
}
}
}
}
//FileWriter複制檔案
public class testFileReader {
public static void main (String [] args){
File file = new File("D:/桌面/switch.txt");
File file2 = new File("D:/桌面/switch2.txt");
Reader input = null;
Writer output = null;
try {
input = new FileReader(file);
output = new FileWriter(file2);
char [] data = new char [1024];
int len = 0;
while (-1 != (len = input.read(data))){
String s = new String(data, 0, len);
output.write(s);
// System.out.println(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("檔案讀取失敗");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("關閉失敗");
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("關閉輸出流失敗");
}
}
}
}
緩沖流
1,位元組緩沖流
BufferedInputStream
BufferedOutputStream
2,字元緩沖流
BufferedReader ReadLine()
BufferedWriter newLine()換行
位元組流檔案拷貝+緩沖流,提高性能
緩沖流(節點流)
//使用新增方法不能發生多态
//用BufferedReader包裝
BufferedReader input = null;
BufferedWriter output = null;
try {
input = new BufferedReader(new FileReader(file));
output = new BufferedWriter(new FileWriter(file2));
String temp ;
while(null != (temp = input.readLine())){
System.out.println(temp);
}
。。。。。。
//輸出方式同理,writer(String s )
轉換流 : 位元組流轉字元流 處理亂碼(編碼集、解碼集)
String(byte [] bytes,Charset charset) 使用charset編碼來編碼byte數組
String(byte [] bytes,int offset, int length,Charset charset) 編碼byte數組的子數組
getbytes()使用預設的編碼方式傳回位元組數組
getBytes(Charset charset)使用指定的編碼方式傳回位元組數組
//處理檔案編碼
public static void main (String [] args) throws UnsupportedEncodingException{
String str1 = "字元串";
byte [] data = str1.getBytes("utf-8");
String str2 = new String(data, "utf-8");
System.out.println(str2);
}
位元組轉為字元:(轉換流)
InputStreamReader()
OutputStreamWriter()
//InputStreamReader(new FileInputStream(new File()))
//位元組流轉字元流
public static void main (String [] args) throws IOException{
File file1 = new File("D:/桌面/1.txt");
File file2 = new File("D:/桌面/2.txt");
try {
BufferedReader input = new BufferedReader(
new InputStreamReader(
new FileInputStream(file1),"utf-8")
);
String len ;
while (null != (len = input.readLine())){
System.out.println(len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}