本帖最后由 zhaogehaolaopo 于 2010-06-28 14:51:25 编辑

解决方案 »

  1.   

    把类容放到一个数组中,用Arrays.binarySerrch()方法,检索你要替换的内容的索引,在替换就OK
      

  2.   

    我是要替换到特定位置的内容,请问怎么办?
    我查了下网上说把文件内容存放到String中,然后用正则表达式检索、替换。
    现在我在查怎么把txt文件存到string对象里。
      

  3.   

    java.io.RandomAccessFile 自己看Java DOC吧。
      

  4.   

    仔细思考了下,我还是用正则表达式比较好,用randomaccessfile来指定位置太麻烦了,位置不好计算。
    现在解决了txt转为string,在继续尝试ing……
      

  5.   


    无论是位置还是关键字,都推荐它。
    都读到内存中是有文件大小要求的。
    使用RandomAccessFile的seek
      

  6.   

    这个怎么用?
    void seek(long offset),移动当前读写位置距文件开始offset位置。
    如果按位置的话,还要去计算在txt文件中字符的字节数,我觉得太麻烦了。
    不知道你的原意是什么?我不知道怎么用seek方法会更好。
    可以的话说下思路吧,谢谢!
      

  7.   


    UP 另外你也可以学习下
    java.io.RandomAccessFile如果你记得你那个字符串的位置你可以直接跳跃到那里进行操作的 大概吧 
      

  8.   

    别大概啊,文件读写要么按字节,要么按字符,不像你凭空想象的那么简单。
    但我也承认也不至于太复杂。
    我只是查了下randomaccessfile的几个方法,觉得不适用我的需求而已。
      

  9.   

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;public class test {
    public test() {
    try {
    RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\1.txt", "rw");
    StringBuffer buff = new StringBuffer();
    int r = 0;
    byte[] b = new byte[1024 * 8];
    while ((r = randomAccessFile.read(b, 0, 1024 * 8)) > -1) {
    buff.append(new String(b, 0, r, "GBK"));
    }
    randomAccessFile.seek(0);
    randomAccessFile.setLength(0);
    randomAccessFile.write(buff.toString().replace("一只", "XX").getBytes());
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static void main(String[] lsg) {
    new test();
    }
    }
    1.txt的内容
    执行前:
    我是一只小小鸟!
    他是一头牛!
    你是什么?执行后:
    我是XX小小鸟!
    他是一头牛!
    你是什么?
      

  10.   


    我也没实际用过java.io.RandomAccessFile 寡人只是单纯的按照书上写了个小例子
    package io;import java.io.DataInput;
    import java.io.DataOutput;
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Date;
    import java.util.GregorianCalendar;import static java.util.Calendar.*;public class RandomFileTest {
    public static void main(String[] args) {
    EmployeeRandomFile[] staff = new EmployeeRandomFile[3];
    staff[0] = new EmployeeRandomFile("Carl Cracker", 75000, 1987, 12, 15);
    staff[1] = new EmployeeRandomFile("Harry Hacker", 50000, 1989, 10, 1);
    staff[2] = new EmployeeRandomFile("Tony Tester", 40000, 1990, 3, 15);
    try {
    DataOutputStream out = new DataOutputStream(new FileOutputStream("randomfileemployee.dat"));
    for (EmployeeRandomFile e : staff) {
    e.writeData(out);
    }
    out.close(); RandomAccessFile in = new RandomAccessFile("randomfileemployee.dat", "r");
    int n = (int) (in.length() / EmployeeRandomFile.RECORD_SIZE);
    EmployeeRandomFile[] newstaff = new EmployeeRandomFile[n];
    for (int i = n - 1; i >= 0; i--) {
    newstaff[i] = new EmployeeRandomFile();
    in.seek(i * EmployeeRandomFile.RECORD_SIZE);
    newstaff[i].readData(in);
    }
    in.close();
    for (EmployeeRandomFile e : newstaff) {
    System.out.println(e);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }class EmployeeRandomFile { public static final int NAME_SIZE = 40;
    public static final int RECORD_SIZE = 2 * NAME_SIZE + 8 + 4 + 4 + 4; private String name;
    private double salary;
    private Date hireDay; public EmployeeRandomFile() { } public EmployeeRandomFile(String n, double s, int year, int month, int day) {
    this.name = n;
    this.salary = s;
    this.hireDay = new GregorianCalendar(year, month - 1, day).getTime();
    } public void raiseSalary(double byParent) {
    double raise = salary * byParent / 100;
    salary += raise;
    } public void writeData(DataOutput out) throws IOException {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(hireDay); DataIO.writeFixedString(name, NAME_SIZE, out);
    out.writeDouble(salary); out.writeInt(calendar.get(YEAR));
    out.writeInt(calendar.get(MONTH));
    out.writeInt(calendar.get(DAY_OF_MONTH) + 1);
    } public void readData(DataInput in) throws IOException {
    name = DataIO.readFixedString(NAME_SIZE, in);
    salary = in.readDouble();
    int y = in.readInt();
    int m = in.readInt();
    int d = in.readInt();
    hireDay = new GregorianCalendar(y, m - 1, d).getTime();
    } public String toString() {
    return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public double getSalary() {
    return salary;
    } public void setSalary(double salary) {
    this.salary = salary;
    } public Date getHireDay() {
    return hireDay;
    } public void setHireDay(Date hireDay) {
    this.hireDay = hireDay;
    }}class DataIO {
    public static String readFixedString(int size, DataInput in) throws IOException {
    StringBuilder b = new StringBuilder(size);
    int i = 0;
    boolean more = true;
    while (more && i < size) {
    char ch = in.readChar();
    i++;
    if (ch == 0) {
    more = false;
    } else {
    b.append(ch);
    }
    }
    in.skipBytes(2 * (size - i));
    return b.toString();
    } public static void writeFixedString(String s, int size, DataOutput out) throws IOException {
    int i;
    for (i = 0; i < size; i++) {
    char ch = 0;
    if (i < s.length()) {
    ch = s.charAt(i);
    }
    out.writeChar(ch);
    }
    }
    }
    我也是半瓢水啊 帮不上你啥忙咯