perl读写文件代码实例
2015-05-08 11:21
706 查看
#mode operand create truncate
#read <
#write > yes yes
#append >> yes
Case 1: Throw an exception if you cannot open the file:
use strict;
use warnings;
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' with the error $!";
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}
close($fh);
Case 2: Give a warning if you cannot open the file, but keep running:
use strict;
use warnings;
my $filename = 'data.txt';
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}
close($fh);
} else {
warn "Could not open file '$filename' $!";
}
Case 3: Read one file into array
use strict;
use warnings;
my $filename = 'data.txt';
open (FILEIN, "<", $filename)
or die "Could not open file '$filename' with the error $!";
my @FileContents = <FILEIN>;
for my $l (@FileContents){
print "$l\n";
}
close FILEIN;
end
相关文章推荐
- js读写json文件实例代码
- Perl 文本文件的读写操作、文件的重命名和删除、多个文本文件的合并实现代码
- Android文件读写实例代码
- java使用RandomAccessFile类基于指针读写文件实例代码
- js读写json文件实例代码
- Android文件读写实例代码
- java向文件中追加内容与读写文件内容源码实例代码
- perl比较两个文件字符串的实例代码
- Android文件读写实例代码
- Android文件读写实例代码
- C#读写txt文件多种方法实例代码
- C语言以数据块的形式读写文件实例代码
- php curl 上传文件代码实例
- JSP上传文件到指定位置实例代码
- java IO流文件的读写具体实例
- Python之文件目录遍历实例代码
- vb.net读写文件代码
- VB.NET拷贝整个目录下所有子目录及文件的实例代码
- Java中获取文件大小的详解及实例代码
- java怎么创建目录(删除/修改/复制目录及文件)代码实例