當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.FileDescriptor用法及代碼示例

io.FileDescriptor in Java

java.io.FileDescriptor 用於打開具有特定名稱的文件。如果該文件中存在任何內容,它將首先刪除所有內容並將 “Beginning of Process” 作為第一行。文件說明符類的實例充當底層 machine-specific 結構的不透明句柄,表示打開的文件、打開的套接字或另一個字節源或接收器。

  • 文件說明符的主要實際用途是創建 FileInputStream 或 FileOutputStream 來包含它。
  • 應用程序不應創建自己的文件說明符。

領域

  • err: 標準錯誤流的句柄。
  • in:標準輸入流的句柄。
  • out: 標準輸出流的句柄。

聲明:

public final class FileDescriptor
   extends Object

構造函數:

  • FileDescriptor():構造一個FileDescriptor對象

方法:

  • sync(): java.io.File.sync()將所有緩衝區與底層設備同步。當FileDescriptor的所有修改數據都已寫入底層設備時,該方法返回。
    Syntax :
public void sync()
Parameters :
-----------
Return : 
void
Exception : 
-> SyncFailedException : This is exception is thrown if there is no guarantee of    
                         synchronization of buffers with the device. 

Java


// Java program explaining the working of sync() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Initializing a FileDescriptor
        FileDescriptor geek_descriptor = null;
        FileOutputStream geek_out = null;
        // HERE I'm writing "GEEKS" in my file
        byte[] buffer = {71,69,69,75,83};
        try{
            geek_out = new FileOutputStream("FILE.txt");
            // This getFD() method is called before closing the output stream
            geek_descriptor = geek_out.getFD();
            // writes byte to file output stream
            geek_out.write(buffer);
            // USe of sync() : to sync data to the source file
            geek_descriptor.sync();
            System.out.print("\nUse of Sync Successful ");
        }
        catch(Exception except)
        {
            // if in case IO error occurs
            except.printStackTrace();
        }
        finally
        {
            // releases system resources
            if(geek_out!=null)
                geek_out.close();
        }
    }
}

筆記:
您無法看到此代碼對代碼中提到的“FILE.txt” 文件所做的更改,因為 Online IDE 上不存在此類文件。因此,您需要將代碼複製到係統編譯器並觀察文件的更改
無論文件中存在什麽內容,它都會將您的文件同步到設備並覆蓋數據。現在文件“FILE.txt”的內容將是

GEEKS

即使不存在這樣的文件,它也會自行創建該文件,同步該文件,並寫入您提到的內容。
輸出:

Use of Sync Successful :) 
  • valid(): java.io.File.valid()檢查FileDescriptor對象是否有效。
    Syntax :
public boolean valid()
Parameters :
-----------
Return : 
true : if the FileDescriptor object is valid else, false
Exception : 
-----------

Java


// Java program explaining the working of valid() method
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // Initializing a FileDescriptor
        FileDescriptor geek_descriptor = null;
        FileInputStream geek_in = null;
        try
        {
            geek_in = new FileInputStream("FILE.txt");
            // get file descriptor
            geek_descriptor = geek_in.getFD();
            boolean check = false;
            // Use of valid() : checking the validity of FileDescriptor
            check = geek_descriptor.valid();
            System.out.print("FileDescriptor is valid : "+check);
        }
        catch(Exception except)
        {
            // if in case IO error occurs
            except.printStackTrace();
        }
        finally
        {
            // releases system resources
            if(geek_in!=null)
                geek_in.close();
        }
    }
}

筆記:
您無法看到此代碼對代碼中提到的“FILE.txt” 文件所做的更改,因為 Online IDE 上不存在此類文件。因此,您需要將代碼複製到係統編譯器並觀察文件的更改。該方法將檢查 FileDescriptor 的有效性。
自從。代碼中的 FileDescriptor 有效,因此返回 true

輸出:

FileDescriptor is valid : true

概括:

  • java.io.File.sync():將所有緩衝區與底層設備同步。
  • java.io.File.valid()檢查FileDescriptor對象是否有效。


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.FileDescriptor in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。