- How to implement the hashCode and equals method using Apache Commons in Java.?
- How to write to a file line by line using Apache Commons-io API in Java ?.
- How to clean contents of a directory using Apache commons-io API in Java ?
- How to search for files in a directory based on extension using Apache commons-io API in Java ?
- How to read a file contents and convert it to String using Apache commons-io API in Java ?
- How to read a file contents line by line using Apache commons-io API in Java ?
Free Data Structures and Algorithms Course
Showing posts with label Apache API in Java. Show all posts
Showing posts with label Apache API in Java. Show all posts
Apache API in Java
How to implement the hashCode and equals method using Apache Commons in Java.?
Program to demonstrate how to implement the hashCode and equals method using Apache Commons in Java.
In order to use below code, you have to get Apache's commons-lang jar.
Output of the program :
In order to use below code, you have to get Apache's commons-lang jar.
package com.hubberspot.apache.commons;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class Author {
private int id;
private String name;
private List < String > books;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List < String > getBooks() {
return books;
}
public void setBooks(List < String > books) {
this.books = books;
}
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(id)
.append(name)
.append(books)
.toHashCode();
}
@Override
public boolean equals(final Object object) {
if(object instanceof Author){
final Author other = (Author) object;
return new EqualsBuilder()
.append(id, other.id)
.append(name, other.name)
.append(books, other.books)
.isEquals();
} else {
return false;
}
}
public static void main(String[] args) {
// Creating Author1
Author author1 = new Author();
author1.setId(1234);
author1.setName("Martin Fowler");
List < string > books = new ArrayList < String >();
books.add("Code Refactoring in Java");
books.add("Java Design Patterns");
author1.setBooks(books);
// Creating Author2
Author author2 = new Author();
author2.setId(1234);
author2.setName("Martin Fowler");
books = new ArrayList < String >();
books.add("Code Refactoring in Java");
books.add("Java Design Patterns");
author2.setBooks(books);
// Creating Author3
Author author3 = new Author();
author3.setId(1235);
author3.setName("Steve Holzner");
books = new ArrayList < String >();
books.add("Design Patterns for Dummies");
books.add("Ajax for Dummies");
author3.setBooks(books);
System.out.println("author1.hashCode() = " +
author1.hashCode());
System.out.println("author2.hashCode() = " +
author2.hashCode());
System.out.println("author1.equals(author2) = "
+ author1.equals(author2));
System.out.println("author1.hashCode() = " +
author1.hashCode());
System.out.println("author3.hashCode() = " +
author3.hashCode());
System.out.println("author1.equals(author3) = " +
author1.equals(author3));
System.out.println("author2.hashCode() = " +
author2.hashCode());
System.out.println("author3.hashCode() = " +
author3.hashCode());
System.out.println("author2.equals(author3) = " +
author2.equals(author3));
}
}
Output of the program :
How to write to a file line by line using Apache Commons-io API in Java ?.
Program to demonstrate how to write to a file line by line using Apache Commons-io API in Java.
Output of the program :
package com.hubberspot.apache.commons.examples;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
public class WriteContentsToFile {
public static void main(String[] args) {
Collection< String > welcome = new ArrayList< String >();
welcome.add("Welcome to Hubberspot.com");
welcome.add("Its a site for Java developers.");
welcome.add("Learn Java by Examples.");
welcome.add("Everything you want to know about Java.");
welcome.add("Tutorials, Source Codes, SCJP, SCWCD, Frameworks,");
welcome.add("Java EE and Design Patterns.");
File file = new File("D:/hubberspot/welcome.txt");
try {
FileUtils.writeLines(file, welcome);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output of the program :
How to clean contents of a directory using Apache commons-io API in Java ?
Program to demonstrate how to clean contents of a directory using Apache commons-io API in Java.
package com.hubberspot.apache.commons.examples;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class CleanDirectoryDemo {
public static void main(String[] args) {
// Create a File object and pass path of the directory
// where files needs to be cleaned
File file = new File("D:/files");
// FileUtils class of org.apache.commons.io
// has a method by name cleanDirectory() which takes in
// the directory where files need to be
// cleaned.
try {
FileUtils.cleanDirectory(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
How to search for files in a directory based on extension using Apache commons-io API in Java ?
Program to demonstrate how to search for files in a directory based on extension using Apache commons-io API in Java.
Output of the program :
package com.hubberspot.apache.commons.examples;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
public class RecursiveFileSearch {
public static void main(String[] args) {
// Create a File object and pass path of the directory
// where files need to be searched
File file = new File("D:/files");
try {
// an array of files extensions to be searched in
// the file created above.
String[] extOfTheFiles = {"jar", "java", "pdf"};
// FileUtils class of org.apache.commons.io
// has a method by name listFiles() which takes in
// three parameters the directory where files need to be
// searched , extension of the files to be searched and
// boolean value to make a recursive search for the files
// The method returns back a collection of File objects
Collection< File > listOfFiles= FileUtils.listFiles(file, extOfTheFiles, true);
// Create a iterator for the collection of files
Iterator< File > fileIterator = listOfFiles.iterator();
// loop over each file in the collection
while(fileIterator.hasNext()) {
File searchedFile = fileIterator.next();
// print the name of the file on the console
System.out.println(searchedFile.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of the program :
How to read a file contents and convert it to String using Apache commons-io API in Java ?
Program to demonstrate how to read a file contents and convert it to String using Apache commons-io API in Java.
Output of the program :
package com.hubberspot.apache.commons.examples;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class FileContentsToString {
public static void main(String[] args) {
// Create a File object and pass name of the file along
// with its path whose contents is to be read
File file = new File("C:/Program Files/glassfish-3.1.2/glassfish/domains/" +
"domain1/logs/server.log");
try {
// org.apache.commons.io package has a class by name
// FileUtils which has a method called as readFileToString()
// This method takes in a file object and return back
// contents of file in the form of String representing
// file contents as a String object
String fileContents = FileUtils.readFileToString(file);
// In order to print the contents of file on the console
// we just print the it on the console using
// System.out.println(String)
System.out.println(fileContents);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output of the program :
How to read a file contents line by line using Apache commons-io API in Java ?
Program to demonstrate how to read a file contents line by line using commons-io API in Java.
Output of the program :
package com.hubberspot.apache.commons.examples;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class ReadFileLineByLine {
public static void main(String[] args) {
// Create a File object and pass name of the file along
// with its path whose contents is to be read
File file = new File("C:/Program Files/glassfish-3.1.2/glassfish/domains/" +
"domain1/logs/server.log");
try {
// org.apache.commons.io package has a class by name
// FileUtils which has a method called as readLines()
// This method takes in a file object and return back
// contents of file in the form of List representing
// each line as String object
List fileContents = FileUtils.readLines(file);
// In order to print the contents of file on the console
// we use for loop which iterates over List line by line
// and prints on the console.
for (String line : fileContents) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output of the program :
Subscribe to:
Posts (Atom)




