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) {
}
分享到:
相关推荐
### 知识点详解 #### 一、二级目录结构及其...通过以上分析可以看出,本实习通过模拟实现采用了二级目录结构的磁盘文件系统中的文件操作,不仅加深了对文件系统原理的理解,还锻炼了数据结构设计和算法实现的能力。
模拟实现采用二级目录结构的磁盘文件系统中的文件操作。 文件系统是操作系统中管理和存取信息的机构,它具有“按名存取”的功能,不仅方便用户,而且能提高系统效率且安全可靠。 在用户程序中可使用文件系统提供的...
编写带缓存的文件操作类 从执行体程序库中的CLLogger类可知,通过缓存要写入文件中的数据,能够提高读写磁盘的性能 请编写一个文件操作的封装类,其要求如下: 需要提供open/read/write/lseek/close等函数的封装函数...
CANoe/CAPL 文件操作脚本是用于自动化处理CANoe环境中的配置、数据记录和分析的编程工具。CANoe是一款广泛应用于汽车电子系统的诊断、测试和测量的软件,而CAPL(CANoe Application Programming Language)是CANoe内...
大学本科操作系统实验 《磁盘文件操作模拟C语言》,花了两天的时间调试。
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
C++文件操作 C++文件的读写fstream // 文件流输入文件流输出文件流 创建一个文本文件并写入信息同向屏幕上输出信息一样将信息输出至文件
C#文件操作类
c#文件操作类,读取,写入;根据传入的虚拟路径或物理路径获取文件、目录;
本项目“C++使用hookapi监控文件操作程序”正是基于这一技术,用于实现对文件系统事件的实时监控。下面将详细介绍相关的知识点。 首先,`hookapi`是指Windows API中的钩子(Hook)机制。钩子是一种让程序能够监视...
js实现的读写文件,文件放在的c:\12.txt里
JSP文件操作
C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt...
C#编程 文件操作 CamouflageFolder(源码)(源码)C#编程 文件操作 CamouflageFolder(源码)(源码)C#编程 文件操作 CamouflageFolder(源码)(源码)C#编程 文件操作 CamouflageFolder(源码)(源码)C#编程 文件操作 ...
C#编程 文件操作 FileBatchChangeName(源码)(源码)C#编程 文件操作 FileBatchChangeName(源码)(源码)C#编程 文件操作 FileBatchChangeName(源码)(源码)C#编程 文件操作 FileBatchChangeName(源码)(源码)C#编程 文件...
C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 ...
C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 SymmetricalEncrypt(源码)(源码)C#编程 文件操作 ...
C#编程 文件操作 BatchDecompression(源码)(源码)C#编程 文件操作 BatchDecompression(源码)(源码)C#编程 文件操作 BatchDecompression(源码)(源码)C#编程 文件操作 BatchDecompression(源码)(源码)C#编程 文件操作 ...
C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)...
操作系统实验四的核心目标是设计和实现一个简单的...通过这样的实验,学生能够深入理解文件系统如何管理磁盘空间,跟踪文件元数据,以及如何执行基本的文件操作,这对理解和设计更复杂的操作系统有着至关重要的作用。