11.5
1、常用的文件操作
方式一:根據路徑構建一個 File 對象:
//方式 1 @Test public void create1(){ String filePath = "D:\\file1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("創建文件 1 成功"); } catch (IOException e) { e.printStackTrace(); } }
方式二:根據父目錄文件+子路徑構建:
//方式 2 @Test public void create2(){ File parentFile = new File("D:\\"); String fileNane = "file2.txt"; File file = new File(parentFile, fileNane); try { file.createNewFile(); System.out.println("文件 2 創建成功"); } catch (IOException e) { throw new RuntimeException(e); } }
方式三:根據父目錄+子路徑構建:
//方式三 @Test public void create3(){ String parentPath = "d:\\"; String filePath = "file3.txt"; File file = new File(parentPath, filePath); try { file.createNewFile(); System.out.println("文件 3 創建成功"); } catch (IOException e) { throw new RuntimeException(e); } }
2、獲取文件的相關信息

Serializable是java中實現對象序列化與反序列化的方式之一,使用方式非常簡單,只要在在類聲明時實現Serializable接口就可以。
Comparable是個排序接口,若一個類實現了該接口,那么該類的數組和列表就可以通過Collections.sort或Arrays.sort進行自動排序。
gbk編碼: 中文占用兩個字節,英文占用一個字節
utf-8編碼:中文占用三個字節,英文占用一個字節
常見的 File 相關方法: getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory
//獲取文件信息 @Test public void Info(){ //先創建文件對象 File file = new File("D:\\file1.txt"); //調用相應方法,得到對應信息 System.out.println("文件名稱:"+file.getName()); System.out.println("文件絕對路徑:"+file.getAbsolutePath()); System.out.println("文件父目錄:"+file.getParent()); System.out.println("文件大小(字節):"+file.length()); System.out.println("文件是否存在:"+file.exists()); System.out.println("是否是文件:"+file.isFile()); System.out.println("是否是目錄:"+file.isDirectory()); }
3、目錄的操作
創建一級目錄:mkdir,創建多級目錄:mkdirs ,delete 刪除空目錄或者文件:
@Test //刪除文件 public void fileDelete() { String filePath = "D:\\file1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "刪除成功"); } else { System.out.println(filePath + "刪除失敗"); } ; } else { System.out.println("文件不存在"); } } //刪除目錄 @Test public void fileDeleteD() { String filePath = "D:\\file1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "刪除成功"); } else { System.out.println(filePath + "刪除失敗"); } ; } else { System.out.println("目錄不存在"); } } //判斷目錄是否存在,不存在就創建 @Test public void fileDeleteD1() { String dirPath = "D:\\test\\dir1.txt"; File file = new File(dirPath); if (file.exists()) { System.out.println(dirPath + "該目錄已經存在"); } else { if (file.mkdirs()) { System.out.println("創建成功"); } else { System.out.println("創建失敗"); } ; } }
4、IO 流原理及流的分類
1、I=input,O=output,用于處理數據傳輸,讀寫文件、網絡通信
2、Java 程序中,輸入輸出以流的形式進行
3、java.io 包下
流的分類:
按操作數據單位不同可以分為:字節流(8bit)、字符流(字符,對應字節按照編碼確定)
效率:字符流更好,字節流的存在是為了更好的處理二進制文件,音視頻
按流向不同可以分為:輸入流、輸出流
按流的角色不同分為:節點流、處理流
在java中InputStream是字節輸入流,用來將文件中的數據讀取到java程序中。InputStream是所有字節輸入流的頂層父類,是一個抽象類。如果要用,需要使用子類。最常用的子類:FileInputStream。
public abstract class InputStream implements Closeable { private static final int MAX_SKIP_BUFFER_SIZE = 2048; public InputStream() { } public abstract int read() throws IOException; public int read(byte[] var1) throws IOException { return this.read(var1, 0, var1.length); }
下面是idea生成的類圖:
OutputStream抽象類,此抽象類是表示輸出字節流的所有類的超類。輸出流接受輸出字節并將這些字節發送到某個接收器。
public abstract class OutputStream implements Closeable, Flushable { public OutputStream() { } }
void close()
關閉此輸出流并釋放與此流有關的所有系統資源。void flush()
刷新此輸出流并強制寫出所有緩沖的輸出字節。void write(byte[] b)
將 b.length 個字節從指定的 byte 數組寫入此輸出流。void write(byte[] b, int off, int len)
將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此輸出流。abstract void write(int b)
將指定的字節寫入此輸出流。
Java.io.Writer類是用于寫入字符流的抽象類。而且Writer類是Java IO中所有Writer的基類。它的子類如:BufferedWriter和PrintWriter等
public abstract class Writer implements Appendable, Closeable, Flushable{ }

Reader是Java的IO庫提供的另一個輸入流接口。和InputStream的區別是,InputStream是一個字節流,即以byte為單位讀取,而Reader是一個字符流,即以char為單位讀取。
public abstract class Reader implements Readable, Closeable { }
前面四個都是抽象類,想要使用必須實現其具體方法
5、Scanner 與 Println
1、基本鍵盤輸入:
import org.testng.annotations.Test; import java.util.Scanner; public class scanPrintTest { public static void main(String[] args) { //創建 Scanner 對象,接受從控制臺輸入 Scanner input = new Scanner(System.in); //接受 String 類型 String str = input.next(); //輸出結果 System.out.println(str); System.out.println("hello world"); } }
2、常見鍵盤輸入類型:
import java.util.Scanner; public class scanTest { public static void main(String[] args) { Scanner input =new Scanner(System.in); //double 類型的數據 System.out.print("請輸入一個 double 類型的數:"); double d = input.nextDouble(); System.out.println(d); //int 類型的數據 System.out.print("請輸入一個 int 類型的數:"); int i = input.nextInt(); System.out.println(i); //字符串類型的數據 System.out.print("請輸入一個 string 類型的數:"); String s = input.next(); System.out.println(s); } }
6、InputStream
常見的子類:
FileInputStream:文件輸入流
BufferedStream:緩沖字節輸入流
ObjectedStream:對象字節輸入流
public class fileInputStream { public static void main(String[] args) { } @Test public void fileIn1() { String filePath = "D:\\testfile.txt"; int readData; FileInputStream fileInputStream = null; try { //創建 FileInputStream 對象,用于讀取文件 fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read()) != -1) { System.out.println((char) readData); } ; } catch (IOException e) { e.printStackTrace(); } finally { //關閉文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn2() { String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字節數組 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try { //創建 FileInputStream 對象,用于讀取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readlength)); } ; } catch (IOException e) { e.printStackTrace(); } finally { //關閉文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn3() { String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字節數組 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try { //創建 FileInputStream 對象,用于讀取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readlength)); } ; } catch (IOException e) { e.printStackTrace(); } finally { //關閉文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
7、OutputStream
1、FileOutputStream:
例子:使用 FileOutputStream 在文件夾中寫入“hello world”,如果文件不存在,會創建文件
public class outputStream { public static void main(String[] args) { } //使用 FileOutputStream 將數據寫到文件中 //如果文件不存在,則創建文件 @Test public void writeFile() { String filePath = "d:\\fileout.txt"; //創建對象 FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath); //1、寫入一個字節 //fileOutputStream.write('H'); //2、寫入一個字符串 //String str = "hello socket"; //str.getBytes()將字符串轉換成字節數組 //fileOutputStream.write(str.getBytes()); //3、寫入字符串 String str = "hello lst"; fileOutputStream.write(str.getBytes(), 0, str.length()); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
8、文件復制
public class copyPic { //file 讀和寫實現復制文件 public static void main(String[] args) throws Exception { //創建 file 對象 File f = new File("d:\\test.jpg"); //判斷文件是否存在 if (f.exists()) { System.out.println("test.jpg 存在,可以復制"); } else { f.createNewFile(); System.out.println("test.jpg 不存在,新建成功,可以復制"); } //創建 FileInputStream 對象 FileInputStream inp = new FileInputStream(f); //創建 FileOutputStream 對象 //判斷 demo 目錄是否存在 File f1 = new File("d:\\demo"); if (f1.isDirectory()) { FileOutputStream out = new FileOutputStream("e:\\demo\\" + f.getName()); byte bytes[] = new byte[1024]; int temp = 0; //邊讀邊寫 while ((temp = inp.read(bytes)) != -1) { //讀 out.write(bytes, 0, temp); //寫 } //結束 inp.close(); out.close(); System.out.println("文件拷貝成功!"); } else { //新建 demo 目錄 f1.mkdir(); System.out.println("demo 目錄不存在,已經新建成功,繼續復制"); FileOutputStream out = new FileOutputStream("d:\\demo\\" + f.getName()); byte bytes[] = new byte[1024]; int temp = 0; //邊讀邊寫 while ((temp = inp.read(bytes)) != -1) { //讀 out.write(bytes, 0, temp); //寫 } //結束 inp.close(); out.close(); System.out.println("文件拷貝成功!"); } } }

浙公網安備 33010602011771號