File
The File
class is used to represent files and directories. It provides methods for reading, writing, and manipulating files and directories.
Usage
The File
class can be used to perform a variety of tasks, including:
- Creating new files and directories:
File.createFile(String path)
: Creates a new file at the specified path.File.createDirectory(String path)
: Creates a new directory at the specified path.
- Deleting files and directories:
File.deleteFile(String path)
: Deletes a file at the specified path.File.deleteDirectory(String path)
: Deletes a directory and all its contents at the specified path.
- Reading and writing files:
File.read(String path)
: Reads the contents of a file at the specified path.File.write(String path, String content)
: Writes the specified content to a file at the specified path.
- Getting information about files and directories:
File.exists(String path)
: Checks if a file or directory exists at the specified path.File.isDirectory(String path)
: Checks if the specified path is a directory.File.isFile(String path)
: Checks if the specified path is a file.File.lastModified(String path)
: Returns the last modified time of a file or directory at the specified path.File.size(String path)
: Returns the size of a file at the specified path.
- Listing files and directories:
File.list(String path)
: Returns a list of files and directories in the specified directory.
Examples
// Create a new file
File.createFile("myfile.txt");
// Write to a file
File.write("myfile.txt", "Hello, world!");
// Read from a file
String content = File.read("myfile.txt");
System.out.println(content);
// Delete a file
File.deleteFile("myfile.txt");
// Create a new directory
File.createDirectory("mydir");
// List files and directories in a directory
List<String> files = File.list("mydir");
for (String file : files) {
System.out.println(file);
}
Source Code
The File
class is located in the io.github.stevedunn.switchvsversion.util
package.
package io.github.stevedunn.switchvsversion.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class File {
public static void createFile(String path) throws IOException {
Path filePath = Paths.get(path);
Files.createFile(filePath);
}
public static void createDirectory(String path) throws IOException {
Path dirPath = Paths.get(path);
Files.createDirectories(dirPath);
}
public static void deleteFile(String path) throws IOException {
Path filePath = Paths.get(path);
Files.delete(filePath);
}
public static void deleteDirectory(String path) throws IOException {
Path dirPath = Paths.get(path);
Files.walk(dirPath)
.sorted((p1, p2) -> -p1.compareTo(p2))
.forEach(path1 -> {
try {
Files.delete(path1);
} catch (IOException e) {
System.err.println("Error deleting path: " + path1);
e.printStackTrace();
}
});
}
public static String read(String path) throws IOException {
Path filePath = Paths.get(path);
return new String(Files.readAllBytes(filePath));
}
public static void write(String path, String content) throws IOException {
Path filePath = Paths.get(path);
Files.write(filePath, content.getBytes());
}
public static boolean exists(String path) {
Path filePath = Paths.get(path);
return Files.exists(filePath);
}
public static boolean isDirectory(String path) {
Path filePath = Paths.get(path);
return Files.isDirectory(filePath);
}
public static boolean isFile(String path) {
Path filePath = Paths.get(path);
return Files.isRegularFile(filePath);
}
public static long lastModified(String path) throws IOException {
Path filePath = Paths.get(path);
return Files.getLastModifiedTime(filePath).toMillis();
}
public static long size(String path) throws IOException {
Path filePath = Paths.get(path);
return Files.size(filePath);
}
public static List<String> list(String path) throws IOException {
Path dirPath = Paths.get(path);
List<String> files = new ArrayList<>();
Files.list(dirPath).forEach(file -> files.add(file.getFileName().toString()));
return files;
}
}