假设要建立的这个.txt文件的内容是:stuid=1001 stuname=Tom engscore=65 mathscore=70
我想将engscore加5然后把修改后的内容再写回去。
具体要用到哪几个类的哪几个方法,请大家帮忙想一下,我不知该如何下手,谢谢了!!
----------------解决方案--------------------------------------------------------
import java.io.*;
public class Modify{
private BufferedWriter bw;//用于读
private BufferedReader br;//用于写
private StringBuffer content;//把内容读进这个字符串里
public Modify(){
content=new StringBuffer();//初始化StringBuffer
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream("xxx.txt")));
//把xxx.txt里面的内容读到content里面
String temp=new String();//中间过度的String
while((temp=br.readLine())!=null){
content.append(temp);
}
System.out.println(temp);
br.close();
//先关闭输入流,再构造输出流
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxx.txt")) );
int i=content.indexOf("engscore");//找到engscore的位置
String sub=content.substring(i+9,i+11);//取子字符串,65
int value=Integer.parseInt(sub);
value+=5;
sub=Integer.toString(value);
content=content.replace(i+9,i+11,sub);//放回去
sub=content.toString();
bw.write(sub);//写入文件
bw.close();//关闭输出流
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String args[]){
new Modify();
}
}
我是在构造函数中就完成了此操作,这样不太好。
如果你常用的话,最好是写两个方法,用于处理这类问题,
----------------解决方案--------------------------------------------------------
我觉得应该给他们一个思路而不是给他们一个程序……
所谓授人以鱼不如授人以渔……
----------------解决方案--------------------------------------------------------
```所谓授人以鱼不如授人以渔```
----------------解决方案--------------------------------------------------------
这两天电脑出问题了,上不了网,所以现在才看到你们的回复。
我自己用RandomAccessFile写了一个,也没运行,不知道写的对不对,回头PO上来让大家帮忙看一下,谢谢各位!!
----------------解决方案--------------------------------------------------------
import java.io.*;
public class Modify{
private BufferedWriter bw;//用于读
private BufferedReader br;//用于写
private StringBuffer content;//把内容读进这个字符串里
public Modify(){
content=new StringBuffer();//初始化StringBuffer
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream("xxx.txt")));
//把xxx.txt里面的内容读到content里面
String temp=new String();//中间过度的String
while((temp=br.readLine())!=null){
content.append(temp);
}
System.out.println(temp);
br.close();
//先关闭输入流,再构造输出流
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxx.txt")) );
int i=content.indexOf("engscore");//找到engscore的位置
String sub=content.substring(i+9,i+11);//取子字符串,65
int value=Integer.parseInt(sub);
value+=5;
sub=Integer.toString(value);
content=content.replace(i+9,i+11,sub);//放回去
sub=content.toString();
bw.write(sub);//写入文件
bw.close();//关闭输出流
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String args[]){
new Modify();
}
}
我是在构造函数中就完成了此操作,这样不太好。
如果你常用的话,最好是写两个方法,用于处理这类问题,
----------------解决方案--------------------------------------------------------
我觉得应该给他们一个思路而不是给他们一个程序……
所谓授人以鱼不如授人以渔……
呵呵,你说的很对。
----------------解决方案--------------------------------------------------------
我用RandomAccessFile写了一个,但是出现了异常:
import java.io.*;
public class UpdateFile
{
public static void main(String args[])throws Exception
{
RandomAccessFile ra=new RandomAccessFile("c:\\studata.txt","rw");
int num;
int len=3;
ra.skipBytes(33);
num=ra.readInt();
num=num+5;
ra.close();
RandomAccessFile wa=new RandomAccessFile("c:\\studata.txt","rw");
wa.skipBytes(33);
wa.writeInt(num);
wa.close();
}
}
出现异常:
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:713)
at UpdateFile.main(UpdateFile.java:10)
Press any key to continue...
studata.txt的内容:stuid=1001 stuname=tom engscore=65。
我把ra.skipBytes(33);和wa.skipBytes(33);改为ra.skipBytes(30);和wa.skipBytes(30);结果文件内容变成这样了:
stuid=1001 stuname=tom engscore=6:.
怎么解决这个问题呢,就是能够直接对engscore=后的整个数字加5?
谢谢各位!!!
----------------解决方案--------------------------------------------------------
import java.io.*;
public class Modify{
private BufferedWriter bw;//用于读
private BufferedReader br;//用于写
private StringBuffer content;//把内容读进这个字符串里
public Modify(){
content=new StringBuffer();//初始化StringBuffer
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream("xxx.txt")));
//把xxx.txt里面的内容读到content里面
String temp=new String();//中间过度的String
while((temp=br.readLine())!=null){
content.append(temp);
}
System.out.println(temp);
br.close();
//先关闭输入流,再构造输出流
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxx.txt")) );
int i=content.indexOf("engscore");//找到engscore的位置
String sub=content.substring(i+9,i+11);//取子字符串,65
int value=Integer.parseInt(sub);
value+=5;
sub=Integer.toString(value);
content=content.replace(i+9,i+11,sub);//放回去
sub=content.toString();
bw.write(sub);//写入文件
bw.close();//关闭输出流
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String args[]){
new Modify();
}
}
我是在构造函数中就完成了此操作,这样不太好。
如果你常用的话,最好是写两个方法,用于处理这类问题,
我自己编译运行了几次,虽然编译通过了,但是文件没有变化,我还得再研究研究。
----------------解决方案--------------------------------------------------------
以上文件我这里运行正常
你在先运行此文件之前须有一个文件叫“xxxx.txt”,
它里面的内容也要写好,写成你第一楼的内容
----------------解决方案--------------------------------------------------------