Created
February 27, 2016 20:31
-
-
Save AnnaBoro/2468928b14888e93e3d8 to your computer and use it in GitHub Desktop.
copy2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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