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等函数的封装函数...
在Visual Studio(VS)中,XML文件操作是一个重要的开发任务,尤其对于处理数据存储、配置文件、序列化或数据交换等场景。XML(eXtensible Markup Language)是一种标记语言,它提供了结构化的数据表示方式,易于...
大学本科操作系统实验 《磁盘文件操作模拟C语言》,花了两天的时间调试。
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
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#编程 文件操作 ...
8. **实验流程图**:实验报告中可能会有流程图展示文件操作的步骤,如文件的创建过程、打开文件的过程、读写文件的过程等,帮助理解操作系统如何执行这些操作。 9. **源代码分析**:实验报告可能包含实现特定文件...
C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)...
汇编语言中,对文件的相关操作,包括建文件,读写文件等等
- **错误处理**:在进行文件操作时,应添加适当的错误处理代码,如`Try...Catch`结构,以应对可能出现的异常情况,如文件不存在、权限问题等。 - **定位**:可以使用`Seek`方法改变读写位置,`Tell`方法获取当前...
MFC程序设计详细实例 第14章 MFC文件操作MFC程序设计详细实例 第14章 MFC文件操作MFC程序设计详细实例 第14章 MFC文件操作MFC程序设计详细实例 第14章 MFC文件操作MFC程序设计详细实例 第14章 MFC文件操作MFC程序...