Created
February 27, 2016 19:14
-
-
Save AnnaBoro/7d19d57f6e0d3a8e9751 to your computer and use it in GitHub Desktop.
+ copy file
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/testtest.txt")); | |
| // createFile(); | |
| // File fileTest = new File("/Users/anna/Documents/directorytest"); | |
| // getFolderTree(fileTest); | |
| } | |
| public static void copyFile(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); | |
| 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(); | |
| } | |
| } | |
| } | |
| public static void createFile() { | |
| File file = new File("file", "test.txt"); | |
| file.mkdirs(); | |
| System.out.println(file.getAbsolutePath()); | |
| } | |
| public static void getFolderTree(File file) { | |
| for (File f : file.listFiles()) { | |
| if(f.isFile()) | |
| System.out.print("\t"); | |
| System.out.println(f.getName()); | |
| if (f.isDirectory()) { | |
| System.out.print("\t"); | |
| getFolderTree(f); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment