1、随机存取文件流基本使用
package demo02; import org.junit.Test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; /** * @description: demo13 * @author: liuyang * @create: 2021-09-07 17:21 */ public class Demo13 { /** * RandomAccessFile: * 随机读取文件流,既可以做输入流也能做输出流。 * 构造器第二个参数mode: * r:只能读取 * rw:读取和写入 * rwd:读取和写入且实时同步文件内容的更新到磁盘 * rws:读取和写入且实时同步文件内容和元数据的更新到磁盘 */ @Test public void test1() { RandomAccessFile randomAccessFile1 = null; RandomAccessFile randomAccessFile2 = null; try { randomAccessFile1 = new RandomAccessFile("杨幂.jpg", "r"); randomAccessFile2 = new RandomAccessFile("杨幂3.jpg", "rw"); byte[] buff = new byte[1024]; int len = 0; while ((len = randomAccessFile1.read(buff)) != -1) { randomAccessFile2.write(buff, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (randomAccessFile1 != null) { randomAccessFile1.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (randomAccessFile2 != null) { randomAccessFile2.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 文件不存在时会自动创建, * 文件存在时会对文件的内容从头开始覆盖,而不是对文件进行覆盖 */ @Test public void test2() { RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile("text8.txt", "rw"); randomAccessFile.write("abcdefghijklmn".getBytes("UTF-8")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (randomAccessFile != null) { randomAccessFile.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 指定文件写入指针位置,位置从0开始, * 若该位置有内容则会对内容进行覆盖 */ @Test public void test3() { RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile("text8.txt", "rw"); randomAccessFile.seek(3); randomAccessFile.write("DEF".getBytes("UTF-8")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (randomAccessFile != null) { randomAccessFile.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 实现内容插入效果 */ @Test public void test4() { RandomAccessFile raf1 = null; RandomAccessFile raf2 = null; try { File file = new File("text8.txt"); raf1 = new RandomAccessFile(file, "r"); raf2 = new RandomAccessFile(file, "rw"); // 先将指针移动到3的位置,读取包含此位置后面的所有内容 raf1.seek(3); byte[] buff = new byte[1024]; int len = 0; // 暂存读取到的内容 StringBuilder sb = new StringBuilder(); while ((len = raf1.read(buff)) != -1) { sb.append(new String(buff, 0, len)); } // 将指针重新回到3的位置写入需插入的内容 raf2.seek(3); raf2.write("xyz".getBytes("UTF-8")); raf2.write(sb.toString().getBytes("UTF-8")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (raf1 != null) { raf1.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (raf2 != null) { raf2.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 实现内容插入效果 * 对test4方法进行改造只需要一个rw模式的RandomAccessFile实例即可 */ @Test public void test5() { RandomAccessFile raf1 = null; try { File file = new File("text8.txt"); raf1 = new RandomAccessFile(file, "rw"); // 先将指针移动到3的位置,读取包含此位置后面的所有内容 raf1.seek(3); byte[] buff = new byte[1024]; int len = 0; // 暂存读取到的内容 StringBuilder sb = new StringBuilder(); while ((len = raf1.read(buff)) != -1) { sb.append(new String(buff, 0, len)); } // 将指针重新回到3的位置写入需插入的内容 raf1.seek(3); raf1.write("xyz".getBytes("UTF-8")); raf1.write(sb.toString().getBytes("UTF-8")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (raf1 != null) { raf1.close(); } } catch (IOException e) { e.printStackTrace(); } } } }