文件操作: 文本文件中含有 abc 字符串。 怎样做到:打开这个文件, 找到abc, 然后删除abc, 然后关闭。(100分)

  • 主题发起人 主题发起人 whbell
  • 开始时间 开始时间
W

whbell

Unregistered / Unconfirmed
GUEST, unregistred user!
用java.
用delphi, 给50分
 
public class IO_file {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("d:/ip地址.txt"));
String s, s2 = new String();
while ((s = in.readLine()) != null)
s2 += s + "/n";
System.out.println(s2);
s2=s2.replaceAll("abc", "");

//把文件内容输出
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(new FileWriter("d:/ip地址.txt")));
int lineCount = 1;
while ((s = in4.readLine()) != null)
out1.println(s);
out1.close();
in4.close();
in.close();

}
}
 
import java.io.*;
public class IO_file {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("d://ip地址.txt"));
String s, s2 = new String();
while ((s = in.readLine()) != null)
s2 += s + "/n";
System.out.println(s2);
s2=s2.replaceAll("abc", "");

//把文件内容输出
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(new FileWriter("d://ip地址.txt")));
int lineCount = 1;
while ((s = in4.readLine()) != null)
out1.println(s);
out1.close();
in4.close();
in.close();

}
}
 
var
textpoint:textfile;
i:integer;
tempstr:string;
tempindex:integer;
begin
memo1.Lines.LoadFromFile('c:/123.txt');
for i:=0 to memo1.lines.count-1do
begin
tempstr:=memo1.Lines.Strings;
tempindex:=pos('abc',tempstr);
Delete(tempstr,tempindex,3);
memo1.Lines.Strings:=tempstr;
end;
assignfile(textpoint,'c:/.txt');
rewrite(textpoint);
for i:=0 to memo1.lines.count-1do
begin
writeln(textpoint,memo1.items.Strings);
end;
closefile(textpoint);
end;
 
还是楼上的得半分实际点,哈哈
 
这一段怎么样?
procedure TForm1.Button1Click(Sender: TObject);
var
od:TOpenDialog;
Strs:TStringList;
fn:String;
i,j:Integer;
begin
od:=TOpenDialog.Create(nil);
strs:=TStringList.Create;
try
if od.Execute then
fn:=od.FileName;
strs.LoadFromFile(fn);
//这样的好处是每次循环时不必计算strs.count,节省时间
j:=strs.Count-1;
for i:=0 to jdo
begin
//把文件中与Edit1.Text相同的字符去掉
//忽略大小写,全部替换
strs:=StringReplace(strs,Edit1.Text,'',[rfReplaceAll, rfIgnoreCase]);
//忽略大小写,只替换找到的第一个
// strs:=StringReplace(strs,Edit1.Text,'',[rfIgnoreCase]);
//不忽略大小写,全部替换
// strs:=StringReplace(strs,Edit1.Text,'',[rfReplaceAll]);
end;
strs.SaveToFile(od.filename);
finally
strs.Free;
od.Free;
end;
end;
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部