Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. public class Main {
  2.    
  3.     public File createFileInDirectory(String directoryPath){
  4.        
  5.         File file = new File(directoryPath);
  6.         if(file.getParentFile().mkdirs()){
  7.             try {
  8.                 file.createNewFile();
  9.             } catch (IOException e) {
  10.                
  11.                 e.printStackTrace();
  12.                 return null;
  13.             }
  14.             return file;
  15.         }
  16.        
  17.         return null;
  18.     }
  19.    
  20.     public void readAndPrintFromFile(File file){
  21.         try {
  22.             Scanner sc=new Scanner(file);
  23.             while(sc.hasNextLine()){
  24.                 System.out.println(sc.nextLine());
  25.             }
  26.         } catch (FileNotFoundException e) {
  27.            
  28.             e.printStackTrace();
  29.         }
  30.        
  31.        
  32.     }
  33.     public static void main(String[] args) {
  34.         Main main = new Main();
  35.         main.readAndPrintFromFile(main.createFileInDirectory("C:"+File.separator+"Windows"));
  36.     }
  37.    
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement