📜  珀尔 |文件处理介绍

📅  最后修改于: 2022-05-13 01:54:20.696000             🧑  作者: Mango

珀尔 |文件处理介绍

在 Perl 中,FileHandle 将名称与外部文件相关联,该文件可以在程序结束或 FileHandle 关闭之前使用。简而言之,FileHandle 就像一个连接,可用于修改外部文件的内容,并为连接(FileHandle)命名,以便更快地访问和简化。 Perl 中的三个基本 FileHandle 是 STDIN、STDOUT 和 STDERR,它们分别代表标准输入、标准输出和标准错误设备。

文件处理通常是通过 open函数完成的。

此外,Mode 和 FileName 可以组合在一起形成一个打开的表达式。

FileHandle 使用 close函数关闭。

使用 FileHandle 读取和写入文件


从 FileHandle 读取可以通过 print函数完成。

也可以通过 print函数写入文件。

文件处理的不同模式

ModeExplanation
“<“Read Only Mode
“>”Creates file (if necessary), Clears the contents of the File and Writes to it
“>>”Creates file (if necessary), Appends to the File
“+<“Reads and Writes but does NOT Create
“+>”Creates file (if necessary), Clears, Reads and Writes
“+>>”Creates file (if necessary), Reads and Appends

例子:
考虑一个包含字符串“Welcome to GeeksForGeeks!!!”的文件 Hello.txt最初。

  1. 模式 = “<”
    这是只读模式。此模式用于从文件中逐行读取内容。
    #!/usr/bin/perl
      
    # Opening a File in Read-only mode
    open(r, "<", "Hello.txt");
      
    # Printing content of the File
    print();
      
    # Closing the File
    close(r);
    

    输出:

  2. 模式=“>”
    这是只写模式。在此模式下打开文件后,文件的原始内容将被清除。如果找不到,它会创建一个同名的新文件。
    #!/usr/bin/perl
      
    # Opening File Hello.txt in Read mode
    open(r, "<", "Hello.txt"); 
      
    # Printing the existing content of the file
    print("Existing Content of Hello.txt: " . ); 
      
    # Opening File in Write mode
    open(w, ">", "Hello.txt"); 
      
    # Set r to the beginning of Hello.txt
    seek r, 0, 0; 
      
    print "\nWriting to File...";
      
    # Writing to Hello.txt using print
    print w "Content of this file is changed";
      
    # Closing the FileHandle
    close(w); 
      
    # Set r to the beginning of Hello.txt
    seek r, 0, 0; 
      
    # Print the current contents of Hello.txt
    print("\nUpdated Content of Hello.txt: ".); 
      
    # Close the FileHandle
    close(r); 
    

    输出:

  3. 模式=”>>”
    这是附加模式。在此模式下打开文件时,文件的原始内容不会被清除。此模式不能用于覆盖,因为字符串始终附加在末尾。如果找不到,它会创建一个同名的新文件。
    #!/usr/bin/perl
      
    # Opening File Hello.txt in Read mode
    open(r, "<", "Hello.txt"); 
      
    # Printing the existing content of the file
    print("Existing Content of Hello.txt: " . ); 
      
    # Opening the File in Append mode
    open(A, ">>", "Hello.txt"); 
      
    # Set r to the beginning of Hello.txt
    seek r, 0, 0; 
      
    print "\nAppending to File...";
      
    # Appending to Hello.txt using print
    print A " Hello Geeks!!!"; 
      
    # close the FileHandle
    close(A); 
      
    # Set r to the beginning of Hello.txt
    seek r, 0, 0; 
      
    # Print the current contents of Hello.txt
    print("\nUpdated Content of Hello.txt: ".);
      
    # Close the FileHandle
    close(r); 
    

    输出:

  4. 模式 = “+<”
    这是读写模式。这可用于覆盖 File 中的现有字符串。它无法创建新文件。
    #!/usr/bin/perl
      
    # Open Hello.txt in Read-Write Mode
    open(rw, "+<", "Hello.txt"); 
      
    # Print original contents of the File. 
    # rw is set to the end.
    print("Existing Content of Hello.txt: ".); 
      
    # The string is attached at the end 
    # of the original contents of the file.
    print rw "Added using Read-Write Mode."; 
      
    # Set rw to the beginning of the File for reading.
    seek rw, 0, 0; 
      
    # Printing the Updated content of the File
    print("\nUpdated contents of Hello.txt: ".);
      
    # Close the FileHandle
    close(rw); 
    

    输出:

  5. 模式 = “+>”
    这是读写模式。 “+<”和“+>”的区别在于“+>”可以创建一个新文件,如果没有找到同名的文件,但“+<”不能。
    #!/usr/bin/perl
      
    # Opening File Hello.txt in Read mode
    open(r, "<", "Hello.txt"); 
      
    # Printing the existing content of the file
    print("Existing Content of Hello.txt: " . ); 
      
    # Closing the File
    close(r);
      
    # Open Hello.txt in Read-Write Mode
    open(rw, "+>", "Hello.txt"); 
      
    # Original contents of the File
    # are cleared when the File is opened
    print("\nContents of Hello.txt gets cleared..."); 
      
    # The string is written to the File
    print rw "Hello!!! This is updated file."; 
      
    # Set rw to the beginning of the File for reading.
    seek rw, 0, 0; 
      
    print("\nUpdated Content of Hello.txt: " .);
      
    # Closing the File
    close(rw);
    

    输出:

  6. 模式 = “+>>”
    这是读取附加模式。这可用于从文件中读取以及追加到文件中。如果未找到,则会创建一个具有相同名称的新文件。
    # Open Hello.txt in Read-Append Mode
    open(ra, "+>>", "Hello.txt"); 
      
    # Set ra to the beginning of the File for reading.
    seek ra, 0, 0; 
      
    # Original content of the File 
    # is NOT cleared when the File is opened
    print("Existing Content of the File: " . );
      
    print "\nAppending to the File....";
      
    # The string is appended to the File
    print ra "Added using Read-Append Mode"; 
      
    # Set ra to the beginning of the File for reading.
    seek ra, 0, 0; 
      
    # Printing the updated content
    print("\nUpdated content of the File: " . );
      
    # Closing the File
    close(rw);
    

    输出:

    重定向输出


    可以使用 select函数将输出从控制台重定向到文件中。

    脚步:

    • 打开一个 FileHandle 来写即“>”、“>>”、“+<”、“+>”或“+>>”。
    • 使用选择函数选择 FileHandle。

    现在,使用 print函数打印的任何内容都被重定向到文件。例子:

    # Open a FileHandle in Write Mode.
    open(File, ">", "Hello.txt"); 
      
    # This sets File as the default FileHandle
    select File; 
       
    # Writes to File
    print("This goes to the File."); 
      
    # Writes to File
    print File "\nThis goes to the File too."; 
      
    # This sets STDOUT as default FileHandle
    select STDOUT; 
    print("This goes to the console.");
      
    # Close the FileHandle.
    close(File); 
    

    控制台中的输出:
    Hello.txt 的内容:
    原始文件:

    更新后的文件: