`
dengkehai
  • 浏览: 80838 次
  • 性别: 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) {

    }
分享到:
评论

相关推荐

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

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

    QT实现HEX文件操作

    HEX文件操作涉及到对这类文件的读取、写入和解析等基本功能。下面将详细讨论QT如何实现HEX文件操作,并结合提供的资源,如《HEX文件格式解析.pdf》和源码工程,探讨相关知识点。 首先,了解HEX文件格式至关重要。...

    电子科技大学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 类型的...

    21个文件操作VC 源码实例.rar

    收集了21个文件操作VC 源码实例,基础级别的VC 文件操作实例,获得INI文件指定段的全部键名和键值、文件对话框、临时文件创建、目录创建、获得INI文件的全部段名、查找文件、复制文件、获得或设置进程的当前目录、...

    Android JNI调用-文件操作

    在本教程中,我们将重点讨论如何通过JNI在Android应用中进行文件操作。 首先,要使用JNI,我们需要在Java类中声明native方法。例如,我们可以声明一个名为`readFileFromNative`的方法: ```java public class ...

    java文件操作类

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

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

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

    Unity中Android的文件操作

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

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

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

    js实现读写文件操作

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

    JSP文件操作

    JSP文件操作

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

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

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

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

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

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

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

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

    操作系统实验4_文件系统

    操作系统实验四的核心目标是设计和实现一个简单的...通过这样的实验,学生能够深入理解文件系统如何管理磁盘空间,跟踪文件元数据,以及如何执行基本的文件操作,这对理解和设计更复杂的操作系统有着至关重要的作用。

    C#文件操作大全.pdf

    根据提供的信息,我们可以总结出以下关于C#文件操作的关键知识点: ### 1. 创建文件夹 在C#中,可以通过`System.IO`命名空间中的`Directory.CreateDirectory`方法来创建一个新的文件夹。 ```csharp using System....

    使用C语言文件操作模拟实现简易记事本

    使用C语言文件操作模拟实现简易记事本,可以达到创建文件,打开文件,读取文件等操作,主要在于熟悉使用C语言的文件操作相关函数,以及对于一些cmd命令同C语言的结合的使用,并且可以通过自定义函数,开关分支语句等...

Global site tag (gtag.js) - Google Analytics