package com.oldboy.hbase; import java.io.*; public class Edit { public static void main(String[] args) throws IOException { File file = new File("G:\\MyVirtual\\CentOS-7-x86_64-Minimal-1611-111\\CentOS-7-x86_64-Minimal-1611-110-cl1-000003.vmdk"); // modifyFileContent(file,"parentCID=6cb404c7","parentCID=9f767ebd"); showText(); } private static boolean modifyFileContent(File file, String oldstr, String newStr) { RandomAccessFile raf = null; try { raf = new RandomAccessFile(file, "rw"); String line = null; long lastPoint = 0; //记住上一次的偏移量 while ((line = raf.readLine()) != null) { final long ponit = raf.getFilePointer(); if(line.contains(oldstr)){ String str=line.replace(oldstr, newStr); raf.seek(lastPoint); raf.writeBytes(str); } lastPoint = ponit; } } catch (Exception e) { e.printStackTrace(); } finally { try { raf.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } public static void showText() throws IOException { File file = new File("G:\\MyVirtual\\CentOS-7-x86_64-Minimal-1611-111\\CentOS-7-x86_64-Minimal-1611-110-cl1-000003.vmdk"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String str ; while ((str = br.readLine())!=null){ if (str.contains("parentCID")){ System.out.println(str); } } } }