Saturday, January 5, 2013

Removing redundant zeros from a String

string.replaceAll("^0+", "");

Removing redundant spaces from a String

string.replaceAll("\\s{2,}", "");

Escaping "$" and "\" in Java

string.replaceAll("(\\$|\\\\)", "\\\\$0");

Fixing StringIndexOutOfBoundsException on replaceAll

Exception StackTrace to String using Java

public String stringValue(Throwable e) {
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
return result.toString();
}

Friday, January 4, 2013

Get InputStream from a given file path using Java

public InputStream fetchInputStream(String filePath) {
InputStream inputStream = this.getClass().getResourceAsStream(filePath);
if (inputStream == null) {
File file = null;
file = new File(filePath);
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return inputStream;
}

String to InputStream using Java

public InputStream toInputStream(String string) {
InputStream inputStream = new ByteArrayInputStream(string.getBytes());
return inputStream;
}

Write InputStream to a given file path using Java

public static void writeToFile(InputStream inputStream, String filePath) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
writer = new BufferedWriter(new FileWriter(filePath));
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.flush();
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

ResourceBundle to Properties using Java

public Properties toProperties(ResourceBundle resourceBundle) {
Set keys = resourceBundle.keySet();
Properties properties = new Properties();
for (String key : keys) {
properties.put(key, resourceBundle.getString(key));
}
return properties;
}

Clob to String using Java

public String stringValue(Clob clob) {
BufferedReader br = null;
StringBuffer buffer = null;
try {
br = new BufferedReader(clob.getCharacterStream());
int c;
buffer = new StringBuffer();
while ((c = br.read()) != -1) {
buffer.append((char) c);
}
return buffer.toString();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toString();
}

InputStream to String using Java

public String stringValue(InputStream inputStream) {
BufferedReader br = null;
StringBuffer buffer = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
buffer = new StringBuffer();
while ((line = br.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toString();
}

Get all files and folders in FTP location

package test;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class GetAllFTPFiles {
public void getAllFTPFiles(String remoteHostName, String username,
String password, String workingDirectory) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(remoteHostName);
ftpClient.login(username, password);
ftpClient.changeWorkingDirectory(workingDirectory);
getFTPFiles(ftpClient, "");
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
}
try {
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void getFTPFiles(FTPClient ftpClient, String path)
throws IOException {
FTPFile[] ftpFiles = ftpClient.listFiles(path);
if (ftpFiles == null) {
log("No FTP Files");
} else {
log("No. of FTP Files : " + ftpFiles.length);
}
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isDirectory()) {
String newPath = (!path.equals("")) ? path + "/"
+ ftpFile.getName() : ftpFile.getName();
System.out.println(newPath);
getFTPFiles(ftpClient, newPath);
} else {
log(path + "/" + ftpFile.getName());
}
path = path.replaceAll(ftpFile.getName(), "");
}
}

private void log(Object log) {
System.out.println(log);
}
}