修改文件行

package elevoc.fae.amazon.demo.file;

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

public class Tes {

static Listlist=new ArrayList<>();

public static void main(String[] args) {

list.add("111111");

String path="E:\\data\\amazon\\demo\\src\\main\\java";

modifyFile(path,"sdd");

byte[]  a=new byte[3];

byte[]  b=new byte[3];

boolean b1 =equleByte(a, b);

System.out.println(b1);

}

public static void modifyFile(String  path,String msg){

File file =new File(path);

//  File[] files = file.listFiles();

        modifyFile(file);

}

public static void modifyFile(File file){

File[] files = file.listFiles();

for (File f:files) {

if (!f.isDirectory()){

if (f.getName().equals("TestAb.java")){

modifyWR(f);

}

//System.out.println(f.getName());

            }else {

modifyFile(f);

}

}

}

public static int  isContailsBytes(byte[] a,byte[] b){

int c=a.length;

for (int i =0; i

if (a[i]==b[0]){

}

}

return 0;

}

public static boolean  equleByte(byte[] a,byte[] b){

if (a.length!=b.length)return false;

for (int i =0; i

if (a[i]!=b[i])return false;

}

return true;

}

public static void modifyWR(File file){

try {

RandomAccessFile fileWriter=new RandomAccessFile(file,"rw");

String  msg;

long up=0;

long cur=0;

while ((msg=fileWriter.readLine()) !=null){

long len=fileWriter.getChannel().position();

System.out.println(len);

//System.out.println(    fileWriter.getChannel().position());

          if (msg.contains(list.get(0))){

long len2=fileWriter.getChannel().position();

//    System.out.println(  "aa"+len2  );

              System.out.println(msg.length());

fileWriter.seek(fileWriter.getChannel().position()-list.get(0).getBytes().length-2);

fileWriter.write(getNewString(list.get(0)));

}

}

}catch (IOException e) {

e.printStackTrace();

}

}

public static byte[] getNewString (String msg){

int  a=msg.length();

StringBuilder  stringBuilder =new StringBuilder();

for (int i =0; i

stringBuilder.append("i");

}

//  stringBuilder.append("\n");

        return stringBuilder.toString().getBytes();

}

}

你可能感兴趣的:(修改文件行)