#javaDay37

Опубликовано: 23 Июнь 2026
на канале: Geeks With Geeks
21
0

Java provides several classes and methods for file handling through the `java.io` package and `java.nio.file` package. Here’s an overview of how you can perform various file operations in Java:

Basic File Operations Using `java.io.File`

The `java.io.File` class is used for basic file operations like creating, deleting, and checking the existence of files or directories.

#### Creating a File
```java
import java.io.File;
import java.io.IOException;

public class FileHandling {
public static void main(String[] args) {
File file = new File("example.txt");
try {
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

#### Writing to a File
You can use `FileWriter` to write to a file.
```java
import java.io.FileWriter;
import java.io.IOException;

public class FileHandling {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, World!");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

#### Reading from a File
You can use `Scanner` to read from a file.
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileHandling {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

Advanced File Operations Using `java.nio.file`

The `java.nio.file` package provides more advanced and efficient file operations.

#### Creating a File
```java
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;

public class FileHandling {
public static void main(String[] args) {
try {
Files.createFile(Paths.get("example.txt"));
System.out.println("File created.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

#### Writing to a File
```java
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class FileHandling {
public static void main(String[] args) {
List String lines = List.of("Hello, World!");
try {
Files.write(Paths.get("example.txt"), lines, StandardCharsets.UTF_8);
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

#### Reading from a File
```java
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;

public class FileHandling {
public static void main(String[] args) {
try {
List String lines = Files.readAllLines(Paths.get("example.txt"));
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```

Additional Operations

#### Deleting a File
```java
import java.io.File;

public class FileHandling {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.delete()) {
System.out.println("Deleted the file: " + file.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}
```

#### Checking File Existence
```java
import java.io.File;

public class FileHandling {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.exists()) {
System.out.println("File exists.");
} else {
System.out.println("File does not exist.");
}
}
}
```

By using these classes and methods, you can handle files effectively in Java. The `java.nio.file` package, introduced in Java 7, is generally preferred for its better performance and ease of use compared to the older `java.io` package.