Skip to main content

Posts

Showing posts with the label File

Gradle : How to list files inside a Jar, Zip Tar Files in Gradle Task

How to load and read files in Gradle Project. The Gradle Project Api has the following methods for using files. file() - to load and read a particular file files() - to load and read multiple files fileTree() - to load and read file hierarchy in a directory or folder zipTree() - to load and read a zip file (includes Jar , War and Ear files) tarTree() - to load and read a tar file How to list files inside a Zip file Jar File and Tar file Lets say we have a file structure as shown in image below. Following Gradle task will list the files inside a zip file, jar file and a tar file at the location "src/main/resources/archives/" task showFiles(){ doLast{ FileTree zipFiles = zipTree("src/main/resources/archives/resources.zip"); FileTree jarFiles = zipTree("src/main/resources/archives/resources.jar"); FileTree tarFiles = tarTree("src/main/resources/archives/resources.tar.gz"); println " \n#Contents of Zip ...

Gradle : Difference between File, FileCollection and FileTree in Gradle

Difference Between File, FileCollection and FileTree in Gradle File FileCollection FileTree File is from java.io package FileCollection is from org.gradle.api.file package FileTree is from org.gradle.api.file package File represents a single file. FileCollection represents a collection of files FileTree represents hierarchy of files in a directory or folder. Difference Between File, FileCollection and FileTree Suppose we have a directory structure as shown in image below. We can write a gradle task as shown below task loadFiles(){ doLast{ File file = file('src/main/resources/sample1.txt') println file.name println "\n" FileCollection collection = files('src/main/resources/sample1.txt','src/main/resources/sample2.txt') collection.each { println it.name } println "\n" FileTree tree = fileTree('src') tree.each { println it.name ...

Gradle : How to read all files in a directory using FileTree

Introduction to FileTree in Gradle Api FileTree is an interface that extends FileCollection and introduces methods like matching() and visit() . It class represents a hierarchy of files in a given directory. It includes files in same folder and all sub folders. How to get all files in a Directory To load all files in a given directory and its sub directories you can use FileTree . Syntax for FileTree is as shown below. FileTree tree = fileTree('path/to/dir') The FileTree can get files in following ways from relative path from We can write a gradle task to list all the files in the src folder as shown below. task loadFiles(){ doLast{ FileTree tree = fileTree('src') tree.each { println it.name } } } When we run the aobe task using command gradle -q loadFiles it will give the following output sample1.txt sample2txt file1.properties A.java

Gradle : What is a FileCollection in Gradle Api

Introduction to FileCollection FileCollection is an interface that extends Iterable<File> , AntBuilderAware and Buildable interfaces. How to load files in a FileCollection Suppose we have a file structure as shown in image below We can load multiple files for operation using the following syntax FileCollection collection = files('relative/path/file1','absolute/path/file2',new File('file3')) As shown in the example above we can load files in three ways From a relative path From a absolute path From a File object. We can write a gradle task to read all files in a folder or directory as follows task fileTree(){ doLast{ FileTree tree = fileTree('src') tree.each { println it.name } } } Now when we run the above gradle task with the command gradle -q fileTree , it will give the following output sample1.txt sample2txt file1.properties resources.tar.gz resources.jar resources.zip A.java Operations and methods ...

Gradle : How to load and read a file in gradle task

How to read a file in Gradle Task To load a file, you can take following three approaches Load file using relative path File file = file('relative/path/file') Load file using absolute path File file = file('absolute/path/file') Load a file using File object File file = file(new File('somePath')) Lets see how these three approaches work task loadFiles << { println file('src/main/resources/file1.properties') println file('/home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties') println file(new File('src/main/resources/file1.properties')) } When we run the above task using command gradle -q loadFiles it will give the following output /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties /home/ekiras/workspace/tutorials/gradle/src/main/resources/file1.properties How to read a File in gradle task Say, we have...

Java : How to write a File in Java using FileWriter

Write a File in Java using FileWriter You can write a file using FileWriter as shown in the code below. package com.ekiras.demo; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Date; class FileDemo { public static void main(String args[]) { readFile(); } public static void readFile() { System.out.println(" Start :: writing file"); try { File file = new File("/home/ekansh/myFile.txt"); FileWriter fileWriter = new FileWriter(file); fileWriter.write("hello, this file is created at :: " + new Date()); fileWriter.flush(); fileWriter.close(); System.out.println(" End :: writing file"); } catch (IOException e) { e.printStackTrace(); } } } You can write the data using the write() method and then call the flush() method to force the os to write to the file, finally close() method to close the file writer object, so that it can be garbage collected.

How to delete a Folder in Java

Points To Remember Before deleting the file you need to check if you have permission to delete file using method canWrite() file specified exists exists() Deleting the file in Java Test.java import java.io.*; class Test{ public static void main(String args[]){ File file = new File("/home/ekansh/test"); if(file.isDirectory()){ if(file.delete()) System.out.println("Success ! Folder deleted."); else System.out.println("Failure ! Folder not deleted."); }else System.out.println("FOLDER DOES NOT EXIST"); } } Success ! Folder deleted.

How to create Nested folders in Java

Points To Remember You need to check the following things fro creating the nested folders If it is a directory by method isDirectory() If it exists by method exists() You might need to check if you have write permissions to create folder by method  canWrite() How to create Nested Folders/Directories in Java This is how yo can create the nested folders in java. CreateNestedFolders.java import java.io.*; class CreateNestedFolders{ public static void main(String args[]){ File file = new File("/home/ekansh/test/1/2/3/abc"); if(!file.canWrite()){ // check if user have write permissions if(!(file.exists() && file.isDirectory())){ if(file.mkdirs()) System.out.println("Success ! Folders created."); else System.out.println("Failure ! Folders not created."); } }else{ System.out.println("PERMISSION DENIED"); } } } Success ! Folders created.

How to Upload and Read a Csv in Grails

Points To Remember Add the plugin Csv Grails plugin . You can do it manually without the plugin if the you have a uniform structure of the csv file. Upload a Csv  You need to add the dependency for the csv file reader in BUildConfig.groovy as shown below BuildConfig.groovy compile ":csv:0.3.1" And you have a csv file like the following. upload.jsp <g:uploadForm action="upload"> <input type="file" name="file"> <g:submitButton name="file" value="Upload"/> </g:uploadForm> When the above form is submitted then the request will be handled by the controller action as shown below UploadController.groovy def upload() { MultipartFile file = request.getFile( 'file' ) file.inputStream.eachCsvLine { row -> String name = row[1] ?: "NA"; String email = row[2] ?: "NA"; // Business Logic here } } Now you can do your bus...