解决方案 »
- JAVA到底能做什么
- 最后六分了,我晕,来看看,关于hashMap的,帮帮忙,实在要分的,到我的其他帖子上拿分,今天用的分太多了,或者明天来
- char[] ch=null;ch的输出
- 请各位推荐有关数据结构和算法的书
- 网络游戏是用什么语言开发的?
- Java新手的QQ群,欢迎大家加入
- 关于链接数据库,请牛人指教下
- **送给总爱问java能做什么的一些人..........
- 请问java编制的程序主要运行在服务器端还是客户端?听说它的速度比较慢,怎么可以运行在服务器端呢?谢谢!
- 换成jdk1.8以后报错
- Java中Thread的终止问题,stop(),interrupt()
- 这个报错有人知道为什么吗?哭死啦。。。。。5555
我查了下网上说把文件内容存放到String中,然后用正则表达式检索、替换。
现在我在查怎么把txt文件存到string对象里。
现在解决了txt转为string,在继续尝试ing……
无论是位置还是关键字,都推荐它。
都读到内存中是有文件大小要求的。
使用RandomAccessFile的seek
void seek(long offset),移动当前读写位置距文件开始offset位置。
如果按位置的话,还要去计算在txt文件中字符的字节数,我觉得太麻烦了。
不知道你的原意是什么?我不知道怎么用seek方法会更好。
可以的话说下思路吧,谢谢!
UP 另外你也可以学习下
java.io.RandomAccessFile如果你记得你那个字符串的位置你可以直接跳跃到那里进行操作的 大概吧
但我也承认也不至于太复杂。
我只是查了下randomaccessfile的几个方法,觉得不适用我的需求而已。
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小小鸟!
他是一头牛!
你是什么?
我也没实际用过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);
}
}
}
我也是半瓢水啊 帮不上你啥忙咯