標籤:大量 清除 大寫 不能 path length fileinput tac txt
Java當中的
IO
IO的分類
第一種分:輸入資料流和輸出資料流
第二種分:位元組流和字元流
第三種分:節點流和處理流
節點流處理資料,處理流是節點流基礎上加工的。
IO中的重點:
InputStreamOutputStreamFileInputStreamFileOutputStream
InputStream
int read(byte[] b,int off,int len)
OutputStream
void write(byte[] b,int off,int len)
// 第一步,匯入類import java.io.*;class Demo { public static void main(String[] args){ // 聲明輸入資料流的引用 FileInputStream fis = null; // 聲明輸出資料流的引用 FileOutputStream fos = null; try{ // 產生輸入資料流的對象 fis = new FileInputStream("e:/from.txt"); // 產生代表輸出資料流的對象 fos = new FileOutputSteam("e:/to.txt"); // 產生一個位元組數組 byte[] buffer = new byte[100]; // 調用輸入資料流對象的read方法 int temp = fis.read(buffer,0,buffer.length); fos.write(buffer,0,temp); for(int i=0; i<buffer.length; i++){ System.out.println(buffer[i]); } }catch(Exception e){ System.out.println(e); } }}
IO是為了對資料進行讀寫操作,資料的流向是以Java程式為參照物的,注意read方法和write方法。
流:在Java中輸入和輸出是通過流的類來實現的,Java提供了一套豐富的流類,可匯入java.io,這樣就可以完成輸入和輸出了,也同樣可以實現網路操作等。
聽到流,是不是很形象了呢?流好比一個走向,當程式中需要讀取資料時,就會開啟一個通向資料來源的流,這個資料來源可以是檔案,記憶體,網路連接等,需要寫入資料時,也會開啟一個通向目的地的流。流好比串連起點到終點的路。
輸入資料流:源到程式,即讀,從資料來源的地方輸入到程式中,讀即 一個檔案裡的內容讀到程式中。
輸出資料流:程式中輸出到目的地,輸出資料流,即寫,一個檔案寫到另一個檔案,從這邊輸出到另一邊。
在Java中流:位元組流,字元流都要掌握
InputStreamOutputStreamReaderWriter
重點早期的Java內容:InputStream和OutputStream,這兩個都是基於位元組流。
字元流:Reader和Writer
Java語言中的流分:文字資料流(字元序列)和二進位流
輸入資料流和輸出資料流
InputStream類為基本的輸入資料流類,是抽象的類,在InputStream類中定義了read方法,用於讀取資料的,讀的。
OutputStream類為基本的輸出類,是抽象的類,在OutputStream類中定義了write方法,用於輸出資料,寫的。
OutputStream類
清除緩衝區:public void flush()
關閉輸出資料流:public void close()
其他重點:
在Java中提供了DataInputStream類與DataOutputStream類,資料輸入流和資料輸出資料流。在JDK1.1版本提供了InputStreamReader類與OutputStreamWriter類,它們是Reader、Writer類的子類,提供了位元組流到字元流的轉換。
字元流
BufferedReader類與BufferedWriter類分別是Reader類和Writer類的子類,輸入和輸出的緩衝區得以提供。
檔案類
FileInputStream類為輸入操作,是檔案輸入資料流類
import java.io.*;public class Demo{ public static void main(String[] args){ byte buffer[] = new byte[1024]; try{ // 建立FileInputStream類對象 FileInputStream fis = new FileInputStream("from.txt"); int temp = fis.read(buffer,0,buffer.length); String str=new String(buffer,0,temp); // 輸出字串內容 System.out.println(str); }catch(Exception e){ System.out.println(e); } }}
FileOutputStream類為輸出類,與FileInputStream同理即可。
FileInputStream類,FileOutputStream類
DataInputStream類,DataOutputStream類
InputStream類,OutputStream類
BufferedReader類,BufferedWriter類
說說IO
//第一種:輸入資料流輸出資料流//第二種:位元組流字元流//第三種:節點流處理流//FileInputStreamclass Test{ public static void main(String args[]){ FileInputStream fis = null; try{ fis = new FileInputStream("e:/read.txt"); byte[] buffer = new byte[100]; fis.read(buffer,0,buffer.length); for(int i = 0;i<buffer.length;i++){ System.out.println(buffer[i]); }} catch(Exception e){ System.out.println(e); } }}
class Test{ public static void main(String args[]){ FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream("e:/read.txt"); fos = new FileOutputStream("e:/write.txt"); byte[] buffer = new byte[100]; int temp = fis.read(buffer,0,buffer.length); fos.write(buffer,0,temp); } catch(Exception e){ System.out.println(e); } }}
class Test{ public static void main(String args[]){ FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream("e:/read.txt"); fos = new FileOutputStream("e:/write.txt"); byte[] buffer = new byte[1024]; while(true){ int temp = fis.read(buffer,o,buffer.length); if(temp = -1){ break; } fos.write(buffer,0,temp); } }catch(Exception e){ System.out.println(e); }finally{ try{ fis.close(); fos.close(); }catch(Excepiton e){ System.out.println(e); } }}}
//字元流public class TextChar public static void main(String args[]){ FileReader fr = null; FileWriter fw = null; try{ fr = new FileReader("e:/read.txt"); fw = new FileWriter("e:/write.txt"); char[] buffer = new char[100]; int temp = fr.read(buffer,0,buffer.length); fw.write(buffer,0,temp); } catch(Exception e){ System.out.println(e); }finally{ try{ fr.close(); fw.close(); } catch(Excepiton e){ System.out.println(e); } }}
//FileReader和BufferedReaderclass Test{ public static void main(String args[]){ FileReader fileReader = null; BufferedReader bufferedReader = null;try{ fileReader = new FileReader("e:/read.txt"); bufferedReader = new BufferedReader(fileReader); String line = null; while(true){ line = bufferedReader.readLine(); if(line == null){ break; } System.out.println(line); } }catch(Exception e){ System.out.println(e); } finally{ try{ bufferedReader.close(); fileReader.close(); } catch(Exception e){ System.out.println(e); } } }}
public class Test{ public static void main(String[] args) throws Exception{ //位元組流 FileInputStream in = new FileInputStream("c:/read.txt"); FileOutStream out = new FileOutputStream("c:/write.txt"); byte[] buffer = new byte[1024]; int len; while( (len = in.read(buffer)) != -1){ out.write(buffer,0,len); } in.close(); out.close(); //字元流 BufferedReader bf = new BufferedReader(new FileReader("c:/read.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt"); String str; while( (str=bf.readLine()) != null ){ bw.write(str); bw.newLine(); } bf.close(); bw.close(); }}
位元組流: InputStream位元組輸入資料流,OutputStream位元組輸出資料流
字元流 : Reader字元輸入資料流 ,Writer字元輸出資料流
資料流: DataInputStream 資料輸入流 ,DataOutputStream 資料輸出資料流
檔案讀寫
目的
1 掌握檔案讀寫的幾種方法
2 FileOutputStream和FileInputStream類的使用。
3 基礎資料型別 (Elementary Data Type)之間的轉換
實現檔案讀取後轉換為大寫後寫入到目標檔案中,其中src是指源檔案,des是目標檔案目錄。
public class FileDemo { //建立一個檔案夾 public static void createFolder(String path){ File folder=new File(path); if(folder.exists()){ System.out.println("檔案夾已存在!"); }else{ //不存在時去建立 folder.mkdir(); } } //建立一個檔案 public static void createFile(String path,String filename){ File file=new File(path,filename); //檔案判斷是否已存在 if(file.exists()){ System.out.println("檔案已存在!"); System.out.println(file.length()); }else{ try{ file.createNewFile(); }catch(IOException e){ System.out.println("檔案建立失敗!"); } } } //寫檔案 public static void write(String path,String filename){ try{ String str="0123456789/nac"; String Upstr = str.toUpperCase();// byte b[]=Upstr.getBytes();// FileOutputStream fos=new FileOutputStream(new File(path,filename)); fos.write(b); fos.close(); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ System.out.println("寫檔案失敗"); } } //讀檔案 public static void read(String path,String filename){ try{ int length=0; String str=""; byte buffer[]=new byte[10]; FileInputStream fis=new FileInputStream(new File(path,filename)); while((length=fis.read(buffer, 0, buffer.length)) !=-1){ str+=new String (buffer, 0, length); } System.out.println(str);// fis.close(); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ e.printStackTrace(); } }// public static void FileReaderCopy(String src,String des){ try{ FileReader fr=new FileReader(src); FileWriter fw=new FileWriter(des); char c[]=new char[1024]; int len=0; while((len=fr.read(c, 0, c.length)) != -1){ fw.write(c, 0, c.length); } fw.close(); fr.close(); } catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ System.out.println("讀寫失敗"); } } // public static void BufferedReaderCopy(String src,String des){ try{ BufferedReader br=new BufferedReader(new FileReader(src)); BufferedWriter bw=new BufferedWriter(new FileWriter(des)); String str=""; while((str=br.readLine()) != null){ String Upstr = str.toUpperCase();//加入大寫的變換 bw.write(Upstr);// bw.newLine(); } bw.close(); br.close(); } catch(FileNotFoundException e){ System.out.println("檔案存在"); }catch(IOException e){ System.out.println("讀寫失敗"); } } //複製 public static void copy(String src,String des){ try{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(des); int c; while((c=fis.read()) != -1){ fos.write(c); } fos.close(); fis.close(); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ System.out.println("讀寫失敗"); } } //複製檔案 public static void copy1(String src,String des){ try{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(des); int c; byte buff[]=new byte[1024]; while((c=fis.read(buff,0,buff.length)) != -1){ fos.write(buff,0,c); } fos.close(); fis.close(); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ System.out.println("讀寫失敗"); } } public static void main(String[] args) { // TODO Auto-generated method stub FileDemo.createFolder("c:/test"); FileDemo.createFile("c:/test", "1.txt"); FileDemo.write("c:/test", "1.txt"); FileDemo.read("c:/test", "1.txt"); FileDemo.read("c:/test", "FileDemo.java"); FileDemo.BufferedReaderCopy("c:/test/FileDemo.java", "c:/test/FileDemo2.java"); FileDemo.copy1("c:/test/1.mp3", "c:/test/2.mp3"); }}
讀檔案
//讀檔案public static void read(String path,String filename){ try{ int length = 0; String str = ""; byte buffer[]=new byte[10]; FileInputStream fis=new FileInputStream(new File(path,filename)); while((length=fis.read(buffer,0,buffer.length))!=-1){ str+=new String(buffer,0,length); } System.out.println(str); fis.close(); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ e.printStackTrace(); }}
檔案的建立
public class FileDemo{ public static void createFolder(String path){ File folder = new File(path); if(folder.exists()){ System.out.println("檔案已存在!"); }else{ folder.mkdir(); }} public static void createFile(String path,String filename){ File file = new File(path,filename); if(file.exists()){ System.out.println("檔案已存在!"); System.out.println(file.length()); }else{ try{ file.createNewFile(); }catch(IOException e){ System.out.println("檔案建立失敗"); } }}public static void main(String[] args){ FileDemo.createFolder("c:/test"); FileDemo.createFile("c:/test","1.txt");}}
寫檔案
public static void write(String path,String filename){ try{ String str = "234455"; byte b[] = str.getBytes(); FileOutputStream fos = new FileOutputStream(new File(path,filename)); fos.write(b); }catch(FileNotFoundException e){ System.out.println("檔案不存在"); }catch(IOException e){ System.out.println("寫檔案失敗"); }}
檔案的讀寫
重點:
檔案類主要功能:建立,讀屬性,寫屬性,刪除等
檔案讀寫操作
File類
File類的對象
用來擷取檔案本身的資訊,如檔案所在目錄、檔案長度、檔案讀寫權限等,不涉及檔案的讀寫操作。
建構函式
File(String filename)File(String directoryPath,String filename)File(File f,String filename)
擷取檔案的屬性
String getName()boolean canRead()boolean canWrite()long length()boolean isFile()等
目錄操作
boolean mkdir():建立目錄。String[] list():以字串的形式返回目錄下所有檔案。File[] listFiles():以File對象形式返回目錄下所有檔案。
檔案操作
boolean createNewFile():建立一個新的檔案。boolean delete():刪除一個檔案
流的概念
Java輸入輸出功能是藉助輸入輸出資料流類來實現的。
java.io包中包含大量用來完成輸入輸出資料流的類。
Java中流的分類:
流的運動方向,可分為輸入資料流和輸出資料流兩種。
流的資料類型,可以分為位元組流和字元流。
輸入資料流類都是抽象類別InputStream(位元組輸入資料流)或抽象類別Reader類(字元輸入資料流)的子類。
輸出資料流類都是抽象類別OutputStream(位元組輸出資料流)或抽象類別Writer類(字元輸出資料流)的子類。
輸入資料流
輸入資料流用於讀取資料,使用者可以從輸入資料流中讀取資料,但不能寫入資料。
輸入資料流讀取資料過程如下:
(1)開啟一個流。
如:FileInputStream inputFile=new FileInputStream("資料來源");
(2)從資訊源讀取資訊。
如:inputFile.read();
(3)關閉流。
如:inputFile.close();
輸出資料流
輸出資料流用於寫入資料。只能寫,不能讀。
寫資料到輸出資料流過程如下:
(1)開啟一個流。
如:FileOutputStream outFile=new FileOutputStream("資料來源");
(2)寫入資訊到目的地。
如:outFile.write(inputFile.read()):
(3)關閉流。如:
如:outFile.close();
IO
I/O操作的目標
IO的分類方法
- 讀取檔案和寫入檔案的方法
I/O操作的目標
目標是從資料來源中讀取資料,將資料寫入到資料目的地中。
從一個地方輸入到java程式中輸出到另外一個地方。
檔案與資料流
寫入和讀出資料檔案,在Java的輸入/輸出操作採用資料流的形式,資料流的兩種形式為16位字元或8位位元組
資料流的操作對象:
資料檔案的讀寫線程間的資料轉送網路間的資料傳播
讀/寫步驟:
匯入輸入/輸出包,匯入java.io.*包
建立檔案對象
FileReader/FileWriter類用於文字檔的讀寫操作
DataInputStream(用於檔案讀出)和DataOutputStream(用於檔案寫入)類檔案的讀寫
FileInputStream(用於檔案讀出)和FileOutputStream對象(用於檔案寫入)
關閉檔案
close函數關閉檔案
File對象的特性
建立檔案
boolean createNewFile();
建立子目錄
boolean mkdir();boolean mkdirs();
重新命名/移動檔案
boolean renameTo(File dest);
刪除檔案
boolean delete();
檢查檔案是否存在
boolean exists();
檢查是否為檔案
boolean isFile();
檢查是否為檔案夾
boolean isDirectory();
查詢檔案長度
long length();
往後餘生,唯專屬你
簡書達叔小生
90後帥氣小夥,良好的開發習慣;獨立思考的能力;主動並且善於溝通
簡書部落格: https://www.jianshu.com/u/c785ece603d1
結語
- 下面我將繼續對 其他知識 深入講解 ,有興趣可以繼續關注
- 小禮物走一走 or 點贊
第39節:Java當中的IO