- /**
- * 輸入/輸出流
- * InputSream:
- * ObjectInputStream //對象流
- * PipedInputStream //管道流
- * FileInputStream //文件流
- * ByteArrayInputStream //字節數組流
- * FilterInputStream //過濾流
- * DataInputStream //數據輸入流
- * BufferedInputSteam //將數據寫入緩衝區
- *
- *
- * 字符流的層次結構
- * Reader:
- * BufferedReader //用字符流寫入緩衝區
- * InputStreamReader //用於將字節碼 與 字符碼 轉換
- * FileReader //用於字符文件的輸入
- *
- * 文件處理:
- * 創建文件對象:
- * 1、File(String path)
- * 2、File(String path, String name)
- * 3、File(File dir, String name)
- *
- * 創建文件 和 目錄
- * 1、public boolean exists() //文件、目錄 是否存在
- *
- * 2、public boolean isFile()
- * public boolean isDirectory() //判斷是文件還是目錄
- *
- * 3、public String getName()
- * public String getPaht() //獲取文件名 或 目錄
- *
- * 4、public long length() //長度
- *
- * 5、public boolean canRead() //文件是否 可讀
- * public boolean canWrite() //文件是否可寫
- *
- * 6、public String[] list() //列出目錄中文件
- *
- * 7、public boolean equals() //比較文件 或 目錄
- *
- * 文件目錄操作
- * 1、public boolean renameTo(File newFile()) //重命名
- *
- * 2、public void delete() //刪除
- *
- * 3、public boolean mkdir() //創建目錄
- *
- *
- *8.2.2文件流
- *
- * 1、字節文件流讀取文件
- *
- * FileIputStream
- * (1)FileInputStream(String filename) //指定的文件名,包括路徑
- * (2)FileInputStream(File file) //指定文件對象
- * (3) FileDescriptor( FileDescritor fdObj) //指定文件描述符
- *
- * 從文件輸入流中讀取字節
- * int read()
- * int rean(byte[] b) //讀到的數據直接寫入字節數組b中
- * int read(byte[] b, int off, int len)
- *
- *示例:
- * public static void main(String[] args){
- * byte[] buffer = new byte[2056];
- * String str;
- * try{
- *
- * }
- * }
- *
- */
- /***字節文件輸入流讀取文件***/
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import javax.swing.JOptionPane;
- class Example{
- public static void main(String[] args){
- byte[] buffer = new byte[2056];
- String str;
- try{
- File file = new File("d:/jtest/test.dat");
- FileInputStream fileInput = new FileInputStream(file);
- //將數據讀到 字節數組buffer中
- int bytes = fileInput.read(buffer, 0, 2056);
- //用字節數組buffer中的數據構建字符串
- str = new String(buffer, 0, bytes);
- }
- /***省略程序**/
- /***省略程序**/
- }
- }
- /***字節文件輸出流寫入文件***/
- /**
- * 把字節發送到文件輸出流
- * write(int b) //將指定字節寫入此文件輸出流
- * write(byte[] b) //將b.length個字節從指定字節數組寫入此文件輸出流中
- * write(byte[] b, int off, int len) //將指定字節數組從偏移量off開始的len個字節寫入
- * */
- 示例:
- import javax.swing.JOptionPane;
- import java.io.*;
- class FileRW{
- int bytes;
- byte buffer[] = new byte[65560];
- FileInputStream fileInput;
- FileOutputStream fileOutput;
- /**
- * 省略部分程序
- * */
- //將圖像文件讀取到字節數組中
- try{
- File file = new File("a.jpg");
- fileInput = new FileInputStream(file);
- //將文件a.jpg的數據讀取到字節數組buffer中,
- bytes = fileInput.read(buffer, 0 ,65560);
- }
- //將字節數組的數據寫入到文件b.jpg中
- try{
- fileOutput = new FileOutputStream("b.jpg");
- fileOutput.write(buffer, 0, bytes);
- }
- }
- /***字符文件流讀寫文本文件***/
- (1)FileReader和FileWriter
- FileReader(String filename)
- FileReader(File file)
- FileReader(FileDescriptor fdObj)
- 將字符輸入或輸出到緩衝區
- BufferedReader(Reader in)
- BufferedWriter(Reader out)
- (2)用字符流進行讀寫操作方法
- //從輸入流中按行讀取字符的方法
- String readLine()
- //向輸出流寫入多個字符的方法
- write(String s, int off, int len)
- //刷新緩衝區
- flush()
- //關閉流
- close()
- /****************/
- 示例:
- FileReader r_file;
- FileWriter w_file;
- BufferedReader buf_reader;
- BufferedWriter buf_writer;
- /**
- * 省略程序
- * */
- //讀取文件
- try{
- File f = new File("D:/jtext/", "a.txt");
- r_file = new FileReader(f);
- buf_reader = new BufferedReader(r_file);
- }
- catch(IOException ef){System.out.println(ef);}
- try{
- while((s=buf_reader.readLine()) != null){
- txt.append(s + '\n');
- }
- }
- catch(IOException ef){Syte........}
- //寫入文件
- try{
- w_file = new FileWriter("b.txt");
- buf_writer = new BufferedWriter(w_file);
- String str = txt.getText();
- buf_writer.write(str, 0, str.length());
- buf_writer.flush();
- }
- /****隨即存取文件流***/
- *前面學的都是順序訪問的
- *隨機存取可以訪問任意位置的的數據
- (1)RandomAccessFile(String filename, String mode)
- (2)RandomAccessFile(File file, String mode)
- 示例:
- try{
- RandomAccessFile f = new RandomAccessFile("a.txt", "rw");
- f.writeBytes("zhang si ming");
- f.close();
- }
- /***本地可執行文件**/
- Runtime 類對象有幾個常用方法:
- (1)exit(int status)
- //通過啓動虛擬機的關閉序列,終止當前正在運行的Java虛擬機
- (2)getRuntime()
- //返回與當前Java應用程序相關的運行時對象,使用該方法創建Runtime類對象
- Runtime rt = Runtime.getRuntime()
- (3)exec(String command)
- //調用該方法可以再單獨的進程中運行由字符串命令指定的本地機上的可執行文件
- (4)gc()
- //運行垃圾回收器
- 示例:
- /***調用windows系統自帶的計算器***/
- try{
- Runtime rt = Runtime.getRuntime();
- rt.exec("C:/windows/system32/calc.exe");
- }
- /***數據流與對象流****/
- 構造方法:
- public DataInputStream(InputStream in);
- public DataOutputStream(OutputStream out);
- 示例:
- int bytes, f_length;
- byte[] buffer;
- FileInputStream fileInput;
- FileOutputStream fileOutput;
- DataInputStream dataInput;
- DataOutputStream dataOutput;
- File file = new File("ABC.mp3");
- //獲取文件長度,設置數組容量
- f_length = (int)file.length();
- buffer = new byte[f_length];
- /***讀取聲音文件***/
- try{
- fileInput = new FileInputStream("ABC.mp3");
- dataInput = new DataInputStream(fileInput);
- bytes = dataInput.read(buffer);
- }
- /******寫入到新文件*******/
- try{
- fileOutput = new FileOutputStream("新文件.mp3");
- dataOutput = new DataOutputStream(fileOutput);
- dataOutput.write(buffer, 0, bytes);
- }
- /***********對象流****************/
- 構造方法:
- ObjectInputStream(InputStream in);
- ObjectOutputStream(OutputStream out);
- //創建數據輸入流
- FileInputStream file_in = new FileInputStream("sample.dat");
- ObjectInputStream object_in = new ObjectInputStream(file_in);
- 常用方法:
- readObject()
- writeObject()
- 對象序列化
- Serializable接口