txt文件的格式如下RefereeView1/192.168.0.102
RefereeView2/192.168.0.104
ArtScore1/192.168.0.107
ArtScore2/192.168.0.114
ArtScore3/192.168.0.112
CompletionScore2/192.168.0.108
CompletionScore1/192.168.0.101
ArtScore4/192.168.0.100
DiffcultyScore1/192.168.0.109
RefereeEdit/192.168.0.105
RefereeView3/192.168.0.106比如要删除:CompletionScore1/192.168.0.101,请问如何实现?
RefereeView2/192.168.0.104
ArtScore1/192.168.0.107
ArtScore2/192.168.0.114
ArtScore3/192.168.0.112
CompletionScore2/192.168.0.108
CompletionScore1/192.168.0.101
ArtScore4/192.168.0.100
DiffcultyScore1/192.168.0.109
RefereeEdit/192.168.0.105
RefereeView3/192.168.0.106比如要删除:CompletionScore1/192.168.0.101,请问如何实现?
楼主可以参考这篇帖子可以找到你要的答案
使用Java中支持这些结构的API来处理每行数据,处理完毕后,
再讲存储结构中的内容写回文件中。
public static void main(String[] args) { JavaSeTest test = new JavaSeTest();
List<String> list;
try {
list = test.readFile("E:\\test.txt","CompletionScore1/192.168.0.101");
test.writeFile("E:\\test.txt", list);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} private List<String> readFile(String filePath, String str)throws IOException {
File file = new File(filePath);
BufferedReader reader = null;
List<String> list = new ArrayList<String>();
reader = new BufferedReader(new FileReader(file));
String text = reader.readLine();
while (text != null) {
if (!text.trim().equals(str)) {
list.add(text + "\r\n");
}
text = reader.readLine();
}
reader.close();
return list;
} private void writeFile(String filePath, List<String> list)
throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(file);
for (String s : list) {
outputStream.write(s.getBytes());
}
outputStream.close();
}
}