Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created February 27, 2016 20:31
Show Gist options
  • Save AnnaBoro/2468928b14888e93e3d8 to your computer and use it in GitHub Desktop.
Save AnnaBoro/2468928b14888e93e3d8 to your computer and use it in GitHub Desktop.
copy2
package lesson8.file;
import java.io.*;
public class DemoFile {
public static void main(String[] args) {
copyFile(new File("/Users/anna/Documents/directorytest/logika.pdf"));
}
public static void copyFile2(File file) {
String fileName = file.getName();
System.out.println(fileName);
FileReader fileReader;
BufferedReader reader = null;
FileWriter writer = null;
String line;
try {
fileReader = new FileReader(file);
reader = new BufferedReader(fileReader, 256);
writer = new FileWriter("copy" + fileName);
while ((line = reader.readLine()) != null) {
writer.write(line + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment