`
dengkehai
  • 浏览: 81198 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

文件操作

    博客分类:
  • java
OS 
阅读更多
Determining If a File or Directory Exists

boolean exists = (new File("filename")).exists();

    if (exists) {

        // File or directory exists

    } else {

        // File or directory does not exist

    }

Creating a File

try {

        File file = new File("filename");

   

        // Create file if it does not exist

        boolean success = file.createNewFile();

        if (success) {

            // File did not exist and was created

        } else {

            // File already exists

        }

    } catch (IOException e) {

    }

Copying One File to Another

This example uses file streams to copy the contents of one file to another file.

    // Copies src file to dst file.

    // If the dst file does not exist, it is created

    void copy(File src, File dst) throws IOException {

        InputStream in = new FileInputStream(src);

        OutputStream out = new FileOutputStream(dst);

   

        // Transfer bytes from in to out

        byte[] buf = new byte[1024];

        int len;

        while ((len = in.read(buf)) > 0) {

            out.write(buf, 0, len);

        }

        in.close();

        out.close();

    }
Getting the Size of a File

     File file = new File("infilename");

   

    // Get the number of bytes in the file

    long length = file.length();

Deleting a File

     boolean success = (new File("filename")).delete();

    if (!success) {

        // Deletion failed

    }

Creating a Temporary File

try {

        // Create temp file.

        File temp = File.createTempFile("pattern", ".suffix");

   

        // Delete temp file when program exits.

        temp.deleteOnExit();

   

        // Write to temp file

        BufferedWriter out = new BufferedWriter(new FileWriter(temp));

        out.write("aString");

        out.close();

    } catch (IOException e) {

    }

Renaming a File or Directory

    // File (or directory) with old name

    File file = new File("oldname");

   

    // File (or directory) with new name

    File file2 = new File("newname");

   

    // Rename file (or directory)

    boolean success = file.renameTo(file2);

    if (!success) {

        // File was not successfully renamed

    }
Moving a File or Directory to Another Directory

// File (or directory) to be moved

    File file = new File("filename");

   

    // Destination directory

    File dir = new File("directoryname");

   

    // Move file to new directory

    boolean success = file.renameTo(new File(dir, file.getName()));

    if (!success) {

        // File was not successfully moved

    }

Getting and Setting the Modification Time of a File or Directory

This example gets the last modified time of a file or directory and then sets it to the current time.

    File file = new File("filename");

   

    // Get the last modified time

    long modifiedTime = file.lastModified();

    // 0L is returned if the file does not exist

   

    // Set the last modified time

    long newModifiedTime = System.currentTimeMillis();

    boolean success = file.setLastModified(newModifiedTime);

    if (!success) {

        // operation failed.

    }
Forcing Updates to a File to the Disk

In some applications, such as transaction processing, it is necessary to ensure that an update has been made to the disk. FileDescriptor.sync() blocks until all changes to a file are written to disk.

    try {

        // Open or create the output file

        FileOutputStream os = new FileOutputStream("outfilename");

        FileDescriptor fd = os.getFD();

   

        // Write some data to the stream

        byte[] data = new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE};

        os.write(data);

   

        // Flush the data from the streams and writers into system buffers.

        // The data may or may not be written to disk.

        os.flush();

   

        // Block until the system buffers have been written to disk.

        // After this method returns, the data is guaranteed to have

        // been written to disk.

        fd.sync();

    } catch (IOException e) {

    }
分享到:
评论

相关推荐

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作

    ### 知识点详解 #### 一、二级目录结构及其...通过以上分析可以看出,本实习通过模拟实现采用了二级目录结构的磁盘文件系统中的文件操作,不仅加深了对文件系统原理的理解,还锻炼了数据结构设计和算法实现的能力。

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作。

    模拟实现采用二级目录结构的磁盘文件系统中的文件操作。 文件系统是操作系统中管理和存取信息的机构,它具有“按名存取”的功能,不仅方便用户,而且能提高系统效率且安全可靠。 在用户程序中可使用文件系统提供的...

    电子科技大学linux环境编程作业2——李林——编写带缓存的文件操作类

    编写带缓存的文件操作类 从执行体程序库中的CLLogger类可知,通过缓存要写入文件中的数据,能够提高读写磁盘的性能 请编写一个文件操作的封装类,其要求如下: 需要提供open/read/write/lseek/close等函数的封装函数...

    CANoe /CAPL 文件操作脚本

    CANoe/CAPL 文件操作脚本是用于自动化处理CANoe环境中的配置、数据记录和分析的编程工具。CANoe是一款广泛应用于汽车电子系统的诊断、测试和测量的软件,而CAPL(CANoe Application Programming Language)是CANoe内...

    C语言文件操作及函数大全

    C语言文件操作及函数大全 2.文件操作函数: (1)文件打开函数fopen fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen("文件名","使用文件方式"); 其中,“文件指针名”必须是被说明为FILE 类型的...

    java文件操作类

    java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...

    操作系统实验磁盘文件操作

    大学本科操作系统实验 《磁盘文件操作模拟C语言》,花了两天的时间调试。

    Unity中Android的文件操作

    原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。

    LabVIEW的打开和关闭文件操作

    打开操作是文件操作的个流程,包括打开创建/替换;关闭操作是文件操作的一个流程,保证文件操作的完整性。  1.打开创建I/O替换文件  “打开/创建/替换文件”位于函数选板的“编程→文件I/O打开创建/替换...

    c文件操作函数详解

    关于c的文件操作函数,详细的介绍,有利于查阅和学习....

    noip文件操作精讲

    Noip 文件操作精讲 Noip 文件操作是编程语言中最基本也是最重要的一部分,涉及到文件的输入输出操作。无论是 C 语言还是 C++ 语言,文件操作都是必不可少的。下面将对 Noip 文件操作进行详细的讲解。 文件操作的...

    C#编程 文件操作 FileCopyPlan(源码)(源码)

    C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)...

    使用内存映射文件加快文件操作速度

    ### 使用内存映射文件加快文件操作速度 在计算机科学领域,提高文件操作效率一直是系统优化的重要环节之一。本文将深入探讨如何通过使用内存映射文件(Memory-Mapped Files)来加速文件读写过程,特别是在使用VB...

    js实现读写文件操作

    js实现的读写文件,文件放在的c:\12.txt里

    JSP文件操作

    JSP文件操作

    C#编程 文件操作 ClearRecycle(源码)(源码)

    C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)...

    C#编程 文件操作 MultiFormatTxt(源码)(源码)

    C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt...

    C#编程 文件操作 ManageFileByType(源码)(源码)

    C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ManageFileByType(源码)(源码)C#编程 文件操作 ...

    C#编程 文件操作 SymmetricalEncrypt(源码)(源码)

    C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 ...

    C#编程 文件操作 DeleteDirByDG(源码)(源码)

    C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)...

Global site tag (gtag.js) - Google Analytics