日期:2014-05-20  浏览次数:20702 次

java的文件问题
怎样将信息从文件中删除啊?
//删除学生信息
public static void deleteStudent(){
while(true){
System.out.println("请选择否要删除数据:");
System.out.println("1.删除");
System.out.println("0.退出");
System.out.print("请选择序号:");
int flag = new Scanner(System.in).nextInt();
while(flag == 1){
System.out.println("************************************************");
System.out.println("请输入要删除的学生的学号:");
String sno = new Scanner(System.in).nextLine();
if(sll.snoExit(sno) ){
sll.delete(sno);
System.out.println("数据删除成功!!!");
}else 
System.out.println("学生或课程不存在!!!");
break;

}
System.out.println("************************************************");
if(flag == 0)
break;
}
}

这是没有用文件的删除代码,文件中录入了
String str = new Scanner(System.in).nextLine();
String[] electiveTemp = str.split("[ ]");
String esno = electiveTemp[0];
String ecno = electiveTemp[1];
int egrade = Integer.parseInt(electiveTemp[2]);
这些信息,现在要指定删除学生的信息怎样删除啊?

求指教。。。。。。

------解决方案--------------------

package demo;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class CharacterDelete {
public static void main(String[] args) throws IOException {
FileReader in = null;
PrintWriter out = null;
try {
in = new FileReader(new File("D:/eclipse4.2/wlog/javatest/src/demo/test.txt"));
out = new PrintWriter("D:/eclipse4.2/wlog/javatest/src/demo/test.txt");
while (in.read() != -1) {
out.println();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}

}
}


要知道更详细的io问题,请点击这里
------解决方案--------------------
使用输入流,一行行将文件内容读取出来,符合删除条件的字符串就不保存,其它内容保存到String[]中