回顧:
Collection
ArrayList
Map
HashMap
Properties
LinkedList - 鏈表,靈活性
Collections - 工具類
sort
java.io
File -> 內存對象,表示文件/目錄,和本地有沒有這個文件/目錄沒關係
listFiles()
listFiles(FileFilter-accept)
getName()
....
isFile()
isDirectory()
IO -> 輸入/輸出。讀/寫
對文件進行[隨機]讀寫的類。不算IO,IO全都是單向的
RandomAccessFile - 可讀可寫 -使用場景:文件複製/文件加密
實例化 -> new
new(File/fileName,“rw”)
API - 文件以字節爲單位
寫
write*3//重要
write(b) - write(int b) - 寫入指定int值的低八位 xxx(x) 寫入的是最後一個x
write(bs) - write(byte[] b) -
write(bs,offset,len) - write(byte[] b,int off,int len) - byte數組從下標off開始,截取長度len。
write 8種基本數據類型
注意:寫入到文件中的是字節,看到文件內容是字符
字節 -> 字符 需要編碼
讀
read * 3 -> 返回 -1 表示讀到文件末尾
int read -> 讀一個字節,並存入到int類型的低八位
返回讀取出來的字節書
int read (bs)-> 讀bs.lenth個字節,並且存入到bs中
int read -> 讀len個字節,並且在bs中off位置開始存。
返回讀取出來的字節個數
read 8種基本數據類型
隨機:指定指針位置 pointer -- 讀寫操作指針都會移動
1.獲得當前指針位置 getFilePointer()
2.跳過對應字節數 -> 順序 skipBytes(int)
3.跳轉到指定指針位置 seek(int)
從而實現 讀寫同時操作
-----------------------------------------------------------------------
隨機讀寫類RandomAccessFile
package java_08;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import org.junit.Test;
public class RandomAccessFileDemo {--------------------------------隨機讀寫類文件拷貝
@Test
public void init() throws FileNotFoundException {
//new RandomAccessFile(“文件名”,“模式”)
//簡化成直接使用文件名
new RandomAccessFile(“a.txt”,“rw”);
new RandomAccessFile(new File(“a.txt”),“rw”);
}
@Test
public void write() throws IOException {--------------------------------------隨機讀寫類文件寫入
RandomAccessFile rf = new RandomAccessFile(“c.txt”, “rw”);
System.out.println(rf);//toString沒有重寫
//文件不存在時,創建新的文件
//文件存在時,在原文件上覆蓋
//寫單個字節
/*
* rf.write(1);rf.write(10000);
*/
//97
rf.write('a');
// 00000000 00000000 00000000 00000001
rf.write(1);
// 00000000 00000000 00000000 10000000
rf.write(128);
// 00000000 00000000 00000001 00000000(讀出的是低8位的,所以讀的是0)
rf.write(256);
// ->10000000 00000000 00000000 00000001 - 原碼
// ->11111111 11111111 11111111 11111110 - 反碼
// ->11111111 11111111 11111111 11111111 - 補碼
rf.write(-1);//11111111 -> 255
// -> 10000000 00000000 00000000 00000010 - 原碼
// -> 11111111 11111111 11111111 11111101 - 反碼
// -> 11111111 11111111 11111111 11111110 - 補碼
rf.write(-2);//11111110
}
@Test
public void read() throws IOException {------------------------------------隨機讀寫類 文件讀
RandomAccessFile rf = new RandomAccessFile("c.txt", "rw");
// 寫入 10000000 -> byte
// 讀 單個字節 8位, 並且放入到int類型的低八位。
// 00000000 00000000 00000000 10000000
int b = rf.read();//a
System.out.println(b);
b = rf.read();//1
System.out.println(b);
b = rf.read();//128
System.out.println(b);
b = rf.read();//256
System.out.println(b);
b = rf.read();//-1
System.out.println(b);
b = rf.read();//-2
System.out.println(b);
}
@Test
public void write2() throws IOException {---------------------------------隨機讀寫類 字符寫入
RandomAccessFile rf = new RandomAccessFile("c.txt","rw");
String word = "天秀強哥";
//字符 -> 字節 解碼
byte[] bs = word.getBytes();
rf.write(bs);
}
@Test
public void read02() throws IOException {--------------------------------隨機讀寫類 通過字節數組讀文件
RandomAccessFile rf = new RandomAccessFile("c.txt","rw");
byte[] bs = new byte[12];
rf.read(bs);//方法結束,bs中已經存入了12個字節了
System.out.println(Arrays.toString(bs));
//字節 -> 字符 編碼
// new String()
}
@Test
public void write3() throws IOException {-------------------------------隨機讀寫類 rf.write(bs, 3, 6); 字符存入字節數組,覆蓋
RandomAccessFile rf = new RandomAccessFile("c.txt","rw");
String word = "天秀強哥";
//字符 -> 字節 解碼
byte[] bs = word.getBytes();
rf.write(bs, 3, 6);//秀強 覆蓋 天秀 在數組前面
}
@Test
public void read03() throws IOException {---------------------------------------隨機讀寫類 rf.read(bs,3,6) 讀取字符
RandomAccessFile rf = new RandomAccessFile("c.txt","rw");
//秀強強哥
byte[] bs = new byte[12];
// rf.read(bs);
rf.read(bs,3,6);//方法結束,bs中已經存入了12個字節了
System.out.println(Arrays.toString(bs));
String word = new String(bs);
System.out.println(word);
//字節 -> 字符 編碼
// new String()
}
@Test
public void read04() throws IOException {-----------------------------隨機讀寫類,寫入時指針下標
RandomAccessFile rf = new RandomAccessFile("a.txt","rw");
long index =rf.getFilePointer();//指針位置。
System.out.println(index);//0
rf.write(1);//寫一個字節
//rf.writeInt(1); (寫4個字節)
index =rf.getFilePointer();
System.out.println(index);//指針下標爲1
rf.write("哈哈".getBytes()); //寫六個字節
index =rf.getFilePointer();
System.out.println(index); //指針下標爲7
//跳轉到對應位置開始讀
rf.seek(3);
index =rf.getFilePointer();
System.out.println(index); //3,指針下標爲3
//跳過一個字節
rf.skipBytes(1);
int b = rf.read();
System.out.println("讀第一個個字節的結果爲"+ b);
b = rf.read();
System.out.println("讀第二個個字節的結果爲"+ b);
b = rf.read();
System.out.println("讀第三個字節的結果爲"+ b);
}
@Test
public void test01() {-----------------------------字節加密
String str = "早上,你好";
byte[] bs = str.getBytes();
//每一個字節都加密
//加減乘 都會存在越界問題 ,除法會存在不能夠整除問題
//& | ^
for(int i = 0;i<bs.length;i++) {
bs[i]^=12;
}
System.out.println(234234^12^12);
String newStr = new String(bs);
System.out.println(newStr);
}
@Test
public void test02() throws IOException { //-----------------------------隨機讀寫類文件 加密1?
RandomAccessFile raf1 = new RandomAccessFile("08jiamidemo.txt","rw");
//raf1 -> 要複製的文件,讀
//raf2 -> 複製後的文件,新文件,寫
int b = -1;
while((b=raf1.read())!= -1) {
b^=12;
raf1.seek(raf1.getFilePointer()-1);
raf1.write(b);
}
raf1.close();
}
@Test
public void test03() throws IOException { //-------------------------------------隨機讀寫類文件加密2?
RandomAccessFile raf1 = new RandomAccessFile("08jiamidemo.txt","rw");
int len = -1;
byte[] bs = new byte[1024];
while((len=raf1.read(bs))!= -1) {
//加密算法
for(int i=0;i<len;i++) {
bs[i]^=22;
}
raf1.seek(raf1.getFilePointer()-len);
raf1.write(bs,0,len); //將讀出來的字節直接寫入到新文件
//加密:子寫入之前先加密計算,在寫入
// 源文件加密: 讀n個字節,回到讀之前的指針位置
}
raf1.close();
}
}
文件拷貝
package java_08;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
public class FileCopyDemo {
@Test
public void copy1() throws IOException { ----------------------------------------------------------- 隨機讀寫類文件拷貝1
RandomAccessFile raf1 = new RandomAccessFile(“1.jpg”,“rw”);
RandomAccessFile raf2 = new RandomAccessFile(“jerry.jpg”,“rw”);
//raf1 -> 要複製的文件,讀
//raf2 -> 複製後的文件,新文件,寫
int b = -1;
while((b=raf1.read())!= -1) {
raf2.write(b);
}
}
@Test
public void copy2() throws IOException { //--------------------------------------------------------------------------隨機讀寫類 文件拷貝2
RandomAccessFile raf1 = new RandomAccessFile("1.jpg","rw");
RandomAccessFile raf2 = new RandomAccessFile("jerry1.jpg","rw");
//raf1 -> 要複製的文件,讀
//raf2 -> 複製後的文件,新文件,寫
int len = -1;
byte[] bs = new byte[1024];
while((len=raf1.read())!= -1) {
raf2.write(bs,0,len); //將讀出來的字節直接寫入到新文件
//加密:子寫入之前先加密計算,在寫入
// 源文件加密: 讀n個字節,回到讀之前的指針位置
}
}
}
--------------——————————————————————————————————————————————————
文件流,字節流,字符流
package java_08;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.Test;
public class StreamDemo {
@Test
public void outputTest01() throws Exception {--------------------------------文件流寫入
//如果沒有文件,創建新文件
//如果有文件,清空原有文件內容,再寫入 -默認false
//如果有文件,在原有內容後面追加 - true
OutputStream fis = new FileOutputStream(“a.txt”,true);
fis.write(1);
fis.write("哈哈".getBytes());
fis.write("兒童節快樂".getBytes(),3,9);
fis.close();
}
@Test
public void inputTest() throws Exception {----------------------------------------------------文件流讀
InputStream fis = new FileInputStream("a.txt");
int a = fis.read();//1
byte[] bs = new byte[100];
fis.read(bs);
System.out.println(new String(bs));
fis.close();
}
// 文件複製 -> InputStream / OutputStream
@Test
public void copyFile1() throws Exception {-----------------------------------------------------文件流 拷貝
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("c1.txt");
byte[] bs = new byte[1024];
int len = fis.read(bs);
while(len != -1) {
fos.write(bs, 0, len);
len = fis.read(bs);
}
fis.close();
fos.close();
}
@Test
public void copyFile2()throws Exception {//-------------------------------------------------------文件流拷貝2
InputStream fis = new FileInputStream("a.txt");
OutputStream fos = new FileOutputStream("a1.txt");
int len = -1;
byte[] bs = new byte[1024];
while((len=fis.read(bs))!=-1) {
fos.write(bs, 0, len);
}
fis.close();
fos.close();
}
}