filename stringlengths 8 46 | code stringlengths 184 6.34k |
|---|---|
ConnectToDatabaseJDBC.java | import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectToDatabaseJDBC {
public static Connection getConnection1(String serverName, int port, String database, String driver, String username, String password) throws SQLException {
Con... |
CallMethodUsingReflection.java | import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
//((getMethod)|(getDeclaredMethod))
//invoke
public class CallMethodUsingReflection {
public static Object callMethod1(Object object, String methodName, Object args[]) throws NoSuchMethodExcep... |
ConvertDateFormat.java | import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDateFormat {
public static String convertDateFormat(String date, String srcFormat, String destFormat) throws ParseException {
// Parse date string into a Date object using the... |
BubbleSort.java | public class BubbleSort {
public static void BubbleSortInt1(int[] num) {
boolean flag = true; // set flag to true to begin first pass
int temp; // holding variable
while (flag) {
flag = false; // set flag to false awaiting a possible swap
for (int j = 0; j < num.length - 1; j++) {
if (num[j] > num[j ... |
BinarySearch.java |
public class BinarySearch {
//Heuristic: (([0-9,a-z,A-Z,_,$]+\s*\=\s*\(\s*[0-9,a-z,A-Z,_,$]+\s*\+\s*[0-9,a-z,A-Z,_,$]+\s*\)\s*/\s*2\s*;)|([0-9,a-z,A-Z,_,$]+\s*=\s*[0-9,a-z,A-Z,_,$]+\s*\+\s*\(\s*[0-9,a-z,A-Z,_,$]+\s*-\s*[0-9,a-z,A-Z,_,$]+\s*\)\s*/\s*2\s*;))
// imid = imin + (imax - imin)/2;
// imid = (imax + imin)/2... |
CopyDirectoryTree.java | package database;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisito... |
CopyFileSamples.java | import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.util.Scanner;
import org.apache.... |
CRC32FileChecksum.java | import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
public class CRC32FileChecksum {
public static long checksum1(File file) throws ... |
CreateJavaProject.java | import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime... |
CreateRSAKeys.java | import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.... |
DeleteFolderRecursively.java | import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class DeleteFolderRecursively {
//isDirectory AND listFiles AND delete
publi... |
DBUpdateAndRollback.java | import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUpdateAndRollback {
public static void Sample1(String myField, String condition1, String condition2) throws SQLException {
Connection connection = ... |
DownloadWebpage.java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import org.apache.http.... |
EncryptFile.java | import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import jav... |
ExecuteExternalProcessAndReadInput.java | import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.util.Map;
public class ExecuteExternalProcessAndReadInput {
// start AND ProcessBuilder AND (redirect)|(getInputStream|getOutputStream)
public static... |
ExtactUsingRegex.java | import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExtactUsingRegex {
// find AND start AND end AND compile AND matcher
public static List<String> extractMatches(String text, String pattern) {
List<String> matches = new LinkedList<Strin... |
Fibonacci.java |
public class Fibonacci {
public static int getFibonacci(int n) {
if(n == 0)
return 0;
else if (n == 1)
return 1;
else
return getFibonacci(n-1) + getFibonacci(n-2);
}
}
|
FileChooser.java | import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class FileChooser {
public static File chooseFileOpen(JFrame frame) {
File retval;
//Create and configure file chooser
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select input file.");
fc.setFileSelec... |
FTP_ApacheCommonsNet_Samples.java | import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPHTTPClient;
import org.apache.commons.net.ftp.FTPSClient;
public class FTP_ApacheCommonsNet_Samples {
public FTPClient sample1a(String server, int port, String username, St... |
FTP_FTP4J_Samples.java | import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ft... |
GCD.java |
//Heuristic:
//((while\s*\(\s*[a-z,A-Z,0-9,_,$]+\s*!=\s*0\s*\).*[a-z,A-Z,0-9,_,$]+\s*=\s*[a-z,A-Z,0-9,_,$]+\s*%\s*[a-z,A-Z,0-9,_,$]+)|(while\s*\(\s*[a-z,A-Z,0-9,_,$]+\s*!=\s*[a-z,A-Z,0-9,_,$]+\s*\).*[a-z,A-Z,0-9,_,$]+\s*=\s*[a-z,A-Z,0-9,_,$]+\s*-\s*[a-z,A-Z,0-9,_,$]+)|(return\s+[a-z,A-Z,0-9,_,$]+\s*\(\s*[a-z,A-Z,0-9,_... |
getMACAddresInStandardForm.java | import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class getMACAddresInStandardForm {
public static String getMyMacAddress1() throws SocketException, UnknownHostException {
InetAddress ip = InetAddress.getByName("192.168.0.1... |
InstantiateNamedClassUsingReflection.java | import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InstantiateNamedClassUsingReflection {
public static Object instantiate(String clazz, Object[] pvalues, Class[] ptypes) throws ClassNotFoundException, NoSuchMethodException, Securit... |
LoadFileIntoByteArray.java | import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
public class LoadFileIntoByteArray {
public static byte[] loadFile(File file) throws Exception {
if (!file.exists() || !file.canRead()) {
String message = "Cannot read file: " + file.getCanonicalPath();
... |
LoadFont.java | import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
//HEURISTIC: createFont AND registerFont
// 1 - Create the font. 2 - Register the font with ... |
MD5.java | public class MD5_Target {
public String getMD5(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < byteData.length; i++) {
sb.append(I... |
OpenFileInDesktopApp.java | import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class OpenFileInDesktopApp {
public static void openFileInDesktopApp(File file) throws IOException {
Desktop desktop = Desktop.getDesktop();
if(Desktop.isDesktopSupported()) {
desktop.open(file);
} else {
throw new Unsupport... |
OpenURLInSystemBrowser.java | import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class OpenURLInSystemBrowser {
public static void openURL(URI uri) throws IOException, URISyntaxException {
if(Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
desktop.b... |
ParseCSVFile.java | package database;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ParseCSVFile {
// c AND ,
public static void parseCSV(File file) throws IOException {
StringTokenizer toks;
BufferedReader in = new Buffere... |
ParseXML2Dom.java | import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
publi... |
PlaySound.java | import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUna... |
PrimeFactors.java | import java.util.ArrayList;
import java.util.List;
public class PrimeFactors {
public static List<Integer> primeFactors1(int number) {
int n = number;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
return fact... |
ResizeObjectArray.java | public class ResizeObjectArray {
public static Object[] resizeArray(Object[] oldArray, int newSize) {
//implementation for any object type
int oldSize = oldArray.length;
Class<?> elementType = oldArray.getClass().getComponentType();
Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);
... |
ScreenShot.java | import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenShot {
//createScreenCapture
public static void s... |
SendEMail.java | //FROM :http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
//Copyright © 2008-2014 Mkyong.com, all rights reserved.
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java... |
SetUpGraphicalViewerSelectionEventHandler.java | public class SetUpGraphicalViewerSelectionEventHandler {
public void setup() {
final GraphicalViewer viewer = new ScrollingGraphicalViewer();
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
}
});
}
}
|
SetUpScrollingGraphicalViewer.java | public class SetUpScrollingGraphicalViewer {
public void createPartControl(Model model) {
GraphicalViewer viewer = new ScrollingGraphicalViewer();
viewer.setContents(model);
}
}
|
ShuffleArrayInPlace.java | import java.util.Random;
public class ShuffleArrayInPlace {
// Heuristic
// Need to swap:
// a[i] = a[j] AND a[j] = tmp
// Need a random index:
// random.nextInt(
// #1:
// j = i + random.nextInt(length-i);
// #2:
// j = random.nextInt(i+1);
public static void shuffle1(int[] a) {
//Standard Fishe... |
TestPalindrome.java | public class Plaindrome {
public static boolean isPalindrome(String original) {
//A not very efficient example
String reverse = "";
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
return true;
else
return ... |
TransposeMatrix.java | package database;
public class TransposeMatrix {
public static int[][] transpose(int[][] m) {
int[][] retval = new int[m[0].length][m.length];
for(int i = 0; i < m.length; i++) {
for(int j = 0; j < m[0].length; j++) {
retval[j][i] = m[i][j];
}
}
return retval;
}
// public static void print(int[][] ... |
UnZip.java |
public class UnZip {
public static void unzip1(File zipfile, File outputdir) throws IOException {
//Buffer for copying the files out of the zip input stream
byte[] buffer = new byte[1024];
//Create parent output directory if it doesn't exist
if(!outputdir.exists()) {
outputdir.mkdirs();
}
//C... |
WritePDF.java | import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
/**
http://tutorials.jenkov.com/java-itext/getting-started.html
*/
public class WritePDF {
... |
XMPPSendMessage.java | import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;
public class XMPPSen... |
ZipFiles.java | import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFiles {
public static void ZipFiles(File zipfile, File[] files) throws IOExcepti... |
README.md exists but content is empty.
- Downloads last month
- 3