src.txt放在工程目錄下,dest.txt可創建,也可不創建。一旦運行程序,如果dest.txt不存在,將自行創建這個文本文檔,再將src.txt中的內容復制到dest.txt
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 9 public class IOTest04 { 10 11 public static void main(String[] args) { 12 copy("src.txt", "dest.txt"); 13 } 14 15 public static void copy(String srcPath, String destPath) { 16 File src = new File(srcPath); 17 File dest = new File(destPath); 18 InputStream is = null; 19 OutputStream os = null; 20 21 try { 22 is = new FileInputStream(src); 23 os = new FileOutputStream(dest, false); 24 25 byte[] buffer = new byte[1024 * 1]; // 1k bytes 26 int length = -1; 27 while ((length = is.read(buffer)) != -1) { 28 os.write(buffer, 0, length); 29 os.flush(); 30 } 31 } catch (FileNotFoundException e) { 32 e.printStackTrace(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } finally { 36 try { 37 if (os != null) { 38 os.close(); 39 System.out.println("OutputStream Closed."); 40 } 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 try { 45 if (is != null) { 46 is.close(); 47 System.out.println("InputStream Closed."); 48 } 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 }
---- ---- ---- ---- ---- ---- ---- ----
讀取文本文檔A中的內容,先進行“加密”,將加密的內容寫入到另一個文本文檔B;完成加密和寫入之后,再將文本文檔B中(已加密)的內容復制、並覆蓋文本文檔A中原本的內容,刪除文本文檔B。
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 9 public class Encrypt { 10 11 public static void main(String[] args) { 12 encrypt("src.txt", 1); 13 } 14 15 public static void encrypt(String srcPath, int password) { 16 File src = new File(srcPath); 17 File dest = new File(src.getParent(), ("temp" + src.getName())); 18 InputStream is = null; 19 OutputStream os = null; 20 21 try { 22 is = new FileInputStream(src); 23 os = new FileOutputStream(dest); 24 25 byte[] buffer = new byte[1024 * 1]; // 1k bytes 26 int length = -1; 27 while ((length = is.read(buffer)) != -1) { 28 for (int i = 0; i < length; ++i) { 29 buffer[i] = (byte) (buffer[i] + password); 30 } 31 os.write(buffer, 0, length); 32 os.flush(); 33 } 34 } catch (FileNotFoundException e) { 35 e.printStackTrace(); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } finally { 39 try { 40 if (os != null) { 41 os.close(); 42 } 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46 try { 47 if (is != null) { 48 is.close(); 49 } 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } 53 } 54 55 try { 56 is = new FileInputStream(dest); 57 os = new FileOutputStream(src); 58 59 byte[] buffer = new byte[1024 * 1]; // 1k bytes 60 int length = -1; 61 while ((length = is.read(buffer)) != -1) { 62 os.write(buffer, 0, length); 63 os.flush(); 64 } 65 } catch (FileNotFoundException e) { 66 e.printStackTrace(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } finally { 70 try { 71 if (os != null) { 72 os.close(); 73 } 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 try { 78 if (is != null) { 79 is.close(); 80 } 81 } catch (IOException e) { 82 e.printStackTrace(); 83 } 84 } 85 86 dest.delete(); 87 } 88 }
文本文檔A中的內容:
對每一個字符執行+1(實際就是將字符所對應的編碼進行+1)的操作,之后變成:
---- ---- ---- ---- ---- ---- ---- ----
分解一
16 File src = new File(srcPath); 17 File dest = new File(src.getParent(), ("temp" + src.getName()));
getParent() - 返回此抽象路徑名父目錄的路徑名字符串;如果此路徑名沒有指定父目錄,則返回 null。
getName() - 返回由此抽象路徑名表示的文件或目錄的名稱。
絕對路徑名的定義與系統有關。在 UNIX 系統上,如果路徑名的前綴是 "/",那么該路徑名是絕對路徑名。在 Microsoft Windows 系統上,如果路徑名的前綴是后跟 "\\" 的盤符,或者是 "\\\\",那么該路徑名是絕對路徑名。
1 import java.io.File; 2 3 public class IO_Test01 { 4 5 public static void main(String[] args) { 6 File src = null; 7 File dest = null; 8 9 // Relative path 10 src = new File("src.txt"); 11 dest = new File(src.getParent(), ("temp" + src.getName())); 12 13 System.out.println("Is absolute path: " + src.isAbsolute()); 14 System.out.println("src's parent: " + src.getParent()); 15 System.out.println("src's name: " + src.getName()); 16 17 System.out.println("dest's parent: " + dest.getParent()); 18 System.out.println("dest's name: " + dest.getName()); 19 System.out.println("--------"); 20 21 // Absolute path 22 src = new File("E:/Java/workspace/IO_Study02/src.txt"); 23 dest = new File(src.getParent(), ("temp" + src.getName())); 24 25 System.out.println("Is absolute path: " + src.isAbsolute()); 26 System.out.println("src's parent: " + src.getParent()); 27 System.out.println("src's name: " + src.getName()); 28 29 System.out.println("dest's parent: " + dest.getParent()); 30 System.out.println("dest's name: " + dest.getName()); 31 } 32 }
輸出結果:
Is absolute path: false src's parent: null src's name: src.txt dest's parent: null dest's name: tempsrc.txt -------- Is absolute path: true src's parent: E:\Java\workspace\IO_Study02 src's name: src.txt dest's parent: E:\Java\workspace\IO_Study02 dest's name: tempsrc.txt
---- ---- ---- ---- ---- ---- ---- ----
分解二
25 byte[] buffer = new byte[1024 * 1]; // 1k bytes 26 int length = -1; 27 while ((length = is.read(buffer)) != -1) { 28 for (int i = 0; i < length; ++i) { 29 buffer[i] = (byte) (buffer[i] + password); 30 } 31 os.write(buffer, 0, length); 32 os.flush(); 33 }
其中的28~30行很好理解,這里不解釋。
關鍵在於方法read(byte[] b)、write(byte[] b, int off, int len)。
public int read(byte[] b) - 從此輸入流中將最多 b.length 個字節的數據讀入一個 byte 數組中。在某些輸入可用之前,此方法將阻塞。返回:讀入緩沖區的字節總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 public class IO_Test01 { 8 9 public static void main(String[] args) { 10 File src = new File("E:/Java/workspace/IO_Study02/src.txt"); 11 InputStream is = null; 12 13 try { 14 is = new FileInputStream(src); 15 16 byte[] buffer = new byte[1024 * 1]; // 1k bytes 17 int length = -1; 18 int times = 0; 19 while ((length = is.read(buffer)) != -1) { 20 ++times; 21 System.out.println("length: " + length); 22 System.out.println("times: " + times); 23 } 24 System.out.println("length: " + length); 25 } catch (FileNotFoundException e) { 26 e.printStackTrace(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } finally { 30 try { 31 if (is != null) { 32 is.close(); 33 } 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 } 39 }
輸出結果:
length: 47 times: 1 length: -1
length = 47,代表從src.txt中讀取到的字節數,也就是有read()返回的。
times = 1,表示while()循環只執行了一次。
length = -1,是while()執行第二次判斷,因為src.txt中的內容已經讀取完畢了,所以read()返回-1。
public void write(byte[] b, int off, int len) - 將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此文件輸出流。參數:b - 數據。 off - 數據中的起始偏移量。 len - 要寫入的字節數。
---- ---- ---- ---- ---- ---- ---- ----
分解三
功力不夠深厚,理解不了,寫不出!!!
---- ---- ---- ---- ---- ---- ---- ----
分解X
86 dest.delete();
public boolean delete() - 刪除此抽象路徑名表示的文件或目錄。如果此路徑名表示一個目錄,則該目錄必須為空才能刪除。返回: 當且僅當成功刪除文件或目錄時,返回 true;否則返回 false
---- ---- ---- ---- ---- ---- ---- ----
總結:待完善、糾錯。