This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainThreadDemo { | |
public static void main(String[] args) { | |
Thread thread = Thread.currentThread(); | |
System.out.println("Current Thread: " + thread.getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try (FileInputStream fis = new FileInputStream("file.txt"); | |
BufferedInputStream bis = new BufferedInputStream(fis)) { | |
// your code belongs to here | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CopyThread extends Thread { | |
private String from; | |
private String to; | |
public CopyThread(String from, String to) { | |
this.from = from; | |
this.to = to; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package threads; | |
public class MyRunnableDemo { | |
public static void main(String[] args) { | |
MyRunnable myRunnable = new MyRunnable(); | |
Thread t1 = new Thread(myRunnable); | |
t1.start(); | |
} |