Skip to content

Instantly share code, notes, and snippets.

View rokon12's full-sized avatar
🎯
Focusing

A N M Bazlur Rahman rokon12

🎯
Focusing
View GitHub Profile
public class MainThreadDemo {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
System.out.println("Current Thread: " + thread.getName());
}
}
try (FileInputStream fis = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(fis)) {
// your code belongs to here
} catch (IOException e) {
e.printStackTrace();
}
import java.io.FileInputStream;
import java.io.IOException;
public class FilePrinter {
private static void printFileJava7() {
try (FileInputStream fis = new FileInputStream("file.txt")) {
int data;
while ((data = fis.read()) != -1) {
import java.io.FileInputStream;
import java.io.IOException;
public class FilePrinter {
private void printFile() {
FileInputStream fis = null;
try {
fis = new FileInputStream("file.txt");
int data;
package java.io;
import java.io.IOException;
public interface Closeable extends AutoCloseable {
/**
* Closes this stream and releases any system resources associated
* with it. If the stream is already closed then invoking this
* method has no effect.
rokonoid@rokonoid:~/multi-threaded-file-copier/src$ javac *.java
rokonoid@rokonoid:~/multi-threaded-file-copier/src$ java CopyApp
java CopyApp source target-dir
java CopyApp source-dir target-dir
rokonoid@rokonoid:~/multi-threaded-file-copier/src$ java CopyApp /home/rokonoid/Videos/Webcam /home/rokonoid/Videos/copied
copying file from: /home/rokonoid/Videos/2017-01-07-211505.webm -> to: /home/rokonoid/Videos/copied/2017-01-07-211505.webm
copying file from: /home/rokonoid/Videos/2017-01-07-212508.webm -> to: /home/rokonoid/Videos/copied/2017-01-07-212508.webm
copying file from: /home/rokonoid/Videos/2017-02-21-213126.webm -> to: /home/rokonoid/Videos/copied/2017-02-21-213126.webm
copying file from: /home/rokonoid/Videos/2017-02-21-213210.webm -> to: /home/rokonoid/Videos/copied/2017-02-21-213210.webm
Copy complete
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CopyApp {
public void copyFiles(String from, String to) {
List<File> files = listFile(from);
public class CopyThread extends Thread {
private String from;
private String to;
public CopyThread(String from, String to) {
this.from = from;
this.to = to;
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Copier {
public void copy(String from, String to) {
System.out.println("copying file from: " + from + " -> to: " + to);
package threads;
public class MyRunnableDemo {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread t1 = new Thread(myRunnable);
t1.start();
}