FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 229   



I am sharing some most commonly used one liner code blocks in Java

Collections

  1. Creating an array in single line
    List<String> colors = Arrays.asList("Red", "Green", "Blue");
    
    Source: https://fastfoodcoding.com/questions/1503909213127/how-to-create-an-arraylist-with-values-in-one-line-in-java

Files

  1. Copy file from one location to another location

    Files.copy(Paths.get("/from/location/foo.txt"), Paths.get("/to/location/foo.txt"));
    

    Source: https://fastfoodcoding.com/questions/1503912305207/how-to-copy-file-from-one-location-to-another-location-in-java

  2. Read file's content into String

    String fileContent = new String(Files.readAllBytes(Paths.get("/path/to/file.txt")));
    

    Source: https://fastfoodcoding.com/questions/1503915579483/how-to-read-a-file-content-into-string-in-java

  3. Filter all files with a particular extension

         Arrays.asList(Paths.get("/path/to/folder").toFile().listFiles(file -> !file.isHidden() &&  file.isFile() && (file.getName().endsWith(".json")))).forEach(file -> {
             // deal with the file
         });
    

    Source: https://fastfoodcoding.com/questions/1504198300348/filter-all-files-with-a-particular-extension-in-java

Dates

  1. Converting Date String into Date Object
    Date dateObject = new SimpleDateFormat("yyyy-MM-dd").parse("2017-10-15");
    
    Source: https://fastfoodcoding.com/questions/1507367618536/how-to-convert-string-into-date-in-one-line-in-java



On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News