File und manuelle Streams
Streams, Buffer und Close-Logik werden manuell verwaltet.
Vollständiger Legacy-Modern-Vergleich aller Java-Klassen, Tests, Maven-POMs und relevanten Ressourcen dieses Moduls. Jede Datei ist genau einmal einer fachlich passenden Vergleichsgruppe zugeordnet.
Streams, Buffer und Close-Logik werden manuell verwaltet.
Files.copy übernimmt Kopieren und Ressourcenverwaltung; Path ist die zentrale Abstraktion.
Die Ähnlichkeit ist eine technische Orientierung auf normalisiertem Quelltext. Sie ersetzt keine fachliche Bewertung.
| Vergleichsgruppe | Legacy-Dateien | Modern-Dateien | Legacy-Zeilen | Modern-Zeilen | Ähnlichkeit |
|---|---|---|---|---|---|
| Build-Konfiguration | 1 | 1 | 34 | 34 | 100% |
| Dateiservice und Test | 2 | 2 | 44 | 46 | 13% |
Das Pair-POM bindet die Legacy- und Modern-Submodule zusammen und gehört deshalb ebenfalls zur vollständigen Codeabdeckung.
I/O-Testkonfiguration wird verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| pom.xml | pom.xml | Direktvergleich Semantische Ähnlichkeit: 100% |
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>17ionio-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>io-legacy</artifactId>
<packaging>jar</packaging>
<name>I/O Legacy</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>17ionio-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>nio-modern</artifactId>
<packaging>jar</packaging>
<name>NIO Modern</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Stream-basierte Kopie wird NIO-Path/Files-API gegenübergestellt.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| LegacyFileCopy.java LegacyFileCopyTest.java | PathFileService.java PathFileServiceTest.java | Direktvergleich Semantische Ähnlichkeit: 13% |
package at.aydin.lab.io.legacy;
// Design Pattern: Adapter – kapselt streambasiertes Kopieren hinter einer kleinen Datei-API.
import java.io.*;
public final class LegacyFileCopy {
public long copy(File source, File target) throws IOException {
InputStream in = null;
OutputStream out = null;
long bytes = 0;
try {
in = new FileInputStream(source);
out = new FileOutputStream(target);
byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
bytes += read;
}
return bytes;
} finally {
if (in != null) try {
in.close();
} catch (IOException ignored) {
}
if (out != null) out.close();
}
}
}package at.aydin.lab.io.legacy;
import org.junit.jupiter.api.Test;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class LegacyFileCopyTest {
@Test
void copiesFile() throws Exception {
Path a = Files.createTempFile("a", "txt"), b = Files.createTempFile("b", "txt");
Files.writeString(a, "abc");
assertEquals(3, new LegacyFileCopy().copy(a.toFile(), b.toFile()));
assertEquals("abc", Files.readString(b));
}
}package at.aydin.lab.io.modern;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.zip.*;
// Design Pattern: Adapter
// Zweck: Fachliche Dateioperationen werden hinter der modernen Path-/Files-API gebündelt.
public final class PathFileService {
public long copy(Path source, Path target) throws IOException {
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
return Files.size(target);
}
public List<Path> find(Path root, String suffix) throws IOException {
try (var paths = Files.walk(root)) {
return paths.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(suffix)).sorted()
.toList();
}
}
public void zip(Path source, Path zip) throws IOException {
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zip))) {
out.putNextEntry(new ZipEntry(source.getFileName().toString()));
Files.copy(source, out);
out.closeEntry();
}
}
}package at.aydin.lab.io.modern;
import org.junit.jupiter.api.Test;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PathFileServiceTest {
@Test
void copiesAndFinds() throws Exception {
Path dir = Files.createTempDirectory("nio"), a = dir.resolve("a.txt"), b = dir.resolve("b.txt");
Files.writeString(a, "abc");
var s = new PathFileService();
assertEquals(3, s.copy(a, b));
assertEquals(2, s.find(dir, ".txt").size());
}
}