Perl读取文件的两种常用方式
整体读入,逐行处理
open(FILE,"<","/home/chenmi/.bashrc")||die"cannot open the file: $!\n";
@linelist=<FILE>;
foreach $eachline(@linelist){
print $eachline;
}
close FILE;
逐行读入,边读边处理
open(FILE,"<","/home/chenmi/.bashrc")||die"cannot open the file: $!\n";
while (<FILE>){
print;
}
close FILE;
第一种方法适合于较小的文件,一次全部读入到array之后可以更加灵活的处理;
open(FILE,"<","/home/chenmi/.bashrc")||die"cannot open the file: $!\n";
@linelist=<FILE>;
foreach $eachline(@linelist){
}
close FILE;
逐行读入,边读边处理
open(FILE,"<","/home/chenmi/.bashrc")||die"cannot open the file: $!\n";
while (<FILE>){
}
close FILE;
第一种方法适合于较小的文件,一次全部读入到array之后可以更加灵活的处理;
第二种方法则适合于大型文件,一次读入一行,可以减少内存占用。
本文介绍了Perl中两种常用的文件读取方法:整体读入并逐行处理,适用于小文件;逐行读入并处理,适合大型文件以节省内存。
1260

被折叠的 条评论
为什么被折叠?



