Java mit einem Stream eine .text Datei kopieren?
Ich möchte per Stream die Datei Hallo wie gehts.txt kopieren und eine neue Datei copy.txt erzeugen. Warum funktioniert dies nicht? FileNotFoundException?
Code ist unten angehängt:
package de.thws; // Von mir zum testen von Streams
import java.io.*;
public class PerStreamDateiEinlesen {
public static void main(String[] args) throws FileNotFoundException {
copyFileWithErrorHandling3Modularized();
}
public static void copyFileWithErrorHandling3Modularized()
{
// try-with-resource statement
try (InputStream fis = new FileInputStream("Hallo wie gehts.text");
OutputStream fos = new FileOutputStream("copy.txt");)
{
copyImproved(fis, fos);
}
catch (IOException e)
{
e.printStackTrace();
}
}
// Hier kopiere ich die Datei
static void copyImproved(InputStream is, OutputStream os) throws IOException
{
byte[] b = new byte[1024];
int n;
do
{
n = is.read(b); //kann maximal 1024 bytes lesen
if (n != -1) os.write(b, 0, n);
}
while (n != -1);
}
}
