I want, if you try to copy to the directory, a message is displayed and the program shuts down. In the case of a file, display the file size and the time it was last modified. I don't know exactly, how can i show out file size, and last time modified.
..............................................................................................................................................................
import java.io.*;
public class KopeeriFail {
private static void kopeeri(String start, String end) throws Exception {
InputStream sisse = new FileInputStream(start);
OutputStream välja = new FileOutputStream(end);
byte[] puhver = new byte[1024];
int loetud = sisse.read(puhver);
while (loetud > 0) {
välja.write(puhver, 0, loetud);
loetud = sisse.read(puhver);
}
sisse.close();
välja.close();
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Did you gave name to the file");
System.exit(1);
}
kopeeri(args[0], args[0] + ".copy");
}
}
You can easily fetch BasicFileAttributes which stores size and last modification timestamp.
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Specify file name");
return;
}
Path initial = Paths.get(args[0]);
if (!Files.exists(initial)){
System.err.println("Path is not exist");
return;
}
if (Files.isDirectory(initial)) {
System.err.println("Path is directory");
return;
}
BasicFileAttributes attributes = Files.
readAttributes(initial, BasicFileAttributes.class);
System.out.println("Size is " + attributes.size() + " bytes");
System.out.println("Last modified time " + attributes.lastModifiedTime());
Files.copy(initial, initial.getParent()
.resolve(initial.getFileName().toString() + ".copy"));
}
Hope it helps!
Related
I have 3 Classes: Regulate, Luminosity, Test
From the class Regulate, I which to setting an attribute in the class Luminosity by invoking the method setAttribute
Then in class Test, I calling the method getAttribute.
The problem is, When I calling the method getAttribute, I find a different value that I set it.
This is the Class Luminosity
public class Luminosity{
public static int attribute;
public static int getAttribute(){
return attribute;
}
public static void setAttribute(int v) {
attribute=v;
try {
File fichier = new File("../../WorkspaceSCA/Lamp/value.txt");
PrintWriter pw = new PrintWriter(new FileWriter(fichier)) ;
String ch=Integer.toString(attribute);
pw.append(ch);
pw.println();
pw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
the Regulate Code:
public class Regulate {
public static void main(String[] args) throws InterruptedException {
Luminosity.setSensedValue(50));
System.out.println("Value of Luminosity= "+ Luminosity.getSensedValue());
}
}
this shows me: Value of Luminosity= 50
Now, I want to recover this value from a different class(Test), like this:
public class Test {
public static void main(String[] args) throws InterruptedException {
System.out.println("Value = "+ Luminosity.getSensedValue());
this shows me: Value= 0
I want to recover the same value.
Thank's in advance
You are start two different classes in two different threads.
Of course Luminosity doesn't have previous value, it was setting in different JVM.
If you want to setup an attribute and transfer it between two threads you can place it in a text file.
public class Luminosity {
private static final String FILE_NAME = "attribute.txt";
private int attribute;
public void writeAttribute(int val) throws IOException {
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
fileWriter.append("" + val);
fileWriter.flush();
}
attribute = val;
}
public int readAttribute() throws IOException {
StringBuilder sb = new StringBuilder();
try (FileReader fileReader = new FileReader(FILE_NAME)) {
int r;
while (true) {
char[] buffer = new char[100];
r = fileReader.read(buffer);
if (r == -1) break;
sb.append(new String(Arrays.copyOf(buffer, r)));
}
} catch (FileNotFoundException e) {
return 0;
}
if (sb.length() == 0) return 0;
return Integer.parseInt(sb.toString());
}
public static void main(String[] args) throws IOException {
Luminosity luminosity = new Luminosity();
System.out.println("attribute after start: " + luminosity.readAttribute());
luminosity.writeAttribute(50);
System.out.println("new attribute: " + luminosity.readAttribute());
}
}
I am wondering if anyone can help me. I am only learning Java, what I am trying to do is read every file on c:\ and create a md5 hash of that file to compare at a later stage as well as displaying some basic counts and meta. I can't seem to recursively loop over every file and folder in the c:\ drive and I am not sure how to tackle creating an MD5 hash of each file. I am also not sure is this the best approach for so many files.
public static void main(String[] args) throws IOException {
int FileCount = 0,
DirCount = 0,
HiddenFiles = 0,
HiddenDirs = 0;
File folder = new File("/");
File[] listOfFiles = folder.listFiles();
for (File listOfFile : listOfFiles) {
Path file = listOfFile.toPath();
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
System.out.println("isOther: " + attr.isOther());
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("isSymbolicLink: " + attr.isSymbolicLink());
System.out.println("size: " + attr.size());
if (listOfFile.isFile()) {
System.out.println("File " + listOfFile.getName());
System.out.println("isHidden " + listOfFile.isHidden());
if (listOfFile.isHidden()) {
HiddenFiles++;
}
System.out.println("getPath " + listOfFile.getPath());
FileCount++;
} else if (listOfFile.isDirectory()) {
System.out.println("Directory " + listOfFile.getName());
System.out.println("isHidden " + listOfFile.isHidden());
System.out.println("getPath " + listOfFile.getPath());
if (listOfFile.isHidden()) {
HiddenDirs++;
}
DirCount++;
}
System.out.println("DirCount " + DirCount);
System.out.println("FileCount " + FileCount);
System.out.println("HiddenDirs " + DirCount);
System.out.println("HiddenFiles " + FileCount);
}
}
Let's create classes that will do it what do you want:
File, Folder
File is already exist, let's create class that has name XFile, not good but you will be find something better in future.
public class XFile {
private final File file;
public XFile(File file) {
this.file = file;
}
public String name() {
return file.getName();
}
//other data you want to know, create getters for all wanted information from File.
public byte[] md5() {
try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
MessageDigest md5 = MessageDigest.getInstance("MD5");
int tempByte;
while ((tempByte = input.read()) != -1) {
md5.update((byte) tempByte);
}
return md5.digest();
} catch (IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
and class Folder (that should be in jdk all this time)
public class Folder {
private final File underlyingDir;
private List<File> elements = null;
public Folder(File underlyingDir) {
this.underlyingDir = underlyingDir;
}
private void fetchElements() {
List<File> firstLevelElements = new ArrayList<>(Arrays.asList(underlyingDir.listFiles()));
elements = this.recursive(firstLevelElements, Integer.MAX_VALUE);
}
public List<XFile> files() {
if (elements == null) {
fetchElements();
}
return elements.stream()
.filter(File::isFile)
.map(XFile::new)
.collect(Collectors.toList());
}
public long foldersCount() {
if (elements == null) {
fetchElements();
}
return elements.stream()
.filter(File::isDirectory)
.collect(Collectors.toList())
.size();
}
public long filesCount() {
if (elements == null) {
fetchElements();
}
return elements.stream()
.filter(File::isFile)
.collect(Collectors.toList())
.size();
}
public long hiddenFiles() {
if (elements == null) {
fetchElements();
}
return elements.stream()
.filter(File::isFile)
.filter(File::isHidden)
.collect(Collectors.toList())
.size();
}
public long hiddenDirs() {
if (elements == null) {
fetchElements();
}
return elements.stream()
.filter(File::isDirectory)
.filter(File::isHidden)
.collect(Collectors.toList())
.size();
}
private List<File> recursive(List<File> elements, int depth) {
if (depth == -1) return Collections.emptyList();
List<File> result = new ArrayList<>();
for (File element : elements) {
if (element.isDirectory()) {
depth--;
result.add(element);
if (nonNull(element.listFiles())) {
result.addAll(recursive(Arrays.asList(element.listFiles()), depth));
}
} else {
result.add(element);
}
}
return result;
}
}
and test class Main
public class Main {
public static void main(String[] args) {
Folder folder = new Folder(new File("F:/"));
System.out.println("files " + folder.filesCount());
System.out.println("folders " + folder.foldersCount());
System.out.println("hidden dirs " + folder.hiddenDirs());
System.out.println("hidden files " + folder.hiddenFiles());
for (XFile file : folder.files()) {
System.out.printf("\nname: %s MD5 hash %s ", file.name(), Arrays.toString(file.md5()));
}
}
}
I have a program that should process the files in the directory and if the file size is more than 50 bytes delete it. Otherwise, if the file size is less then 50 bytes program should rename the args[1] file to the allFilesContent.txt(same directory), and write all the files to this file, separated by "n" (110 ASCII code). But instead the program just creates another file and writes to the very first args[1] file. What's the problem?
public class Solution
{
public static void main(String [] args) throws IOException
{
File path = new File(args[0]);
File resultFileAbsolutePath = new File(args[1]);
ArrayList<File> allFiles = new ArrayList<>();
boolean isRenamed = false;
for(File file : path.listFiles())
{
if(file.length() > 50)
{
FileUtils.deleteFile(file);
}
else if(file.length() <= 50)
{
if(!isRenamed)
{
FileUtils.renameFile(resultFileAbsolutePath, new File(resultFileAbsolutePath.getParent()+"\\allFilesContent.txt"));
isRenamed = true;
}
if(!file.getName().equals(resultFileAbsolutePath.getName()))
{
allFiles.add(file);
}
}
}
Collections.sort(allFiles, new Comparator<File>()
{
#Override
public int compare(File o1, File o2)
{
return o1.getName().compareTo(o2.getName());
}
});
FileOutputStream fileOutputStream = new FileOutputStream(resultFileAbsolutePath, true);
for (File file : allFiles)
{
try(FileInputStream fileInputStream = new FileInputStream(file))
{
if(allFiles.indexOf(file) != 0) fileOutputStream.write(110);
int data;
while(fileInputStream.available() > 0)
{
data = fileInputStream.read();
fileOutputStream.write(data);
}
}
}
fileOutputStream.close();
}
public static void deleteFile(File file)
{
if (!file.delete())
{
System.out.println("Can not delete file with name " + file.getName());
}
}
}
And FileUtils class
import java.io.File;
public class FileUtils
{
public static void deleteFile(File file)
{
if (!file.delete())
{
System.out.println("Can not delete file with name " + file.getName());
}
}
public static void renameFile(File source, File destination)
{
if (!source.renameTo(destination))
{
System.out.println("Can not rename file with name " + source.getName());
}
}
}
You have following statement: "FileOutputStream fileOutputStream = new FileOutputStream(resultFileAbsolutePath, true);"
Instead of "true" put "false". It should work.
Having this kind of subdirectories:
C:\test\foo\a.dat (100kb)
C:\test\foo\b.dat (200kb)
C:\test\foo\another_dir\jim.dat (500kb)
C:\test\bar\ball.jpg (5kb)
C:\test\bar\sam\sam1.jpg (100kb)
C:\test\bar\sam\sam2.jpg (300kb)
C:\test\somefile.dat (700kb)
I want to get size of all subdirectories, but only show the top directory, Running the command java DU c:\test should produce the following output:
DIR C:\TEST\FOO 800KB
FILE C:\TEST\SOMEFILE.DAT 700KB
DIR C:\TEST\BAR 405KB
any help will be great, my code so far is close but not getting the expected output ? :/
import java.io.File;
public class DU {
public static void main(String[] args) {
File file = new File(args[0]);
if (file.isDirectory()) {
File[] filesNames = file.listFiles();
for (File temp : filesNames) {
if (temp.isDirectory()) {
File dirs = new File(temp.getPath());
getDirSize(dirs);
} else {
System.out.println("FILE - " + temp.getPath() + " "
+ friendlyFileSize(temp.length()));
}
}
} else {
System.out.println("THIS IS NOT A FILE LOCATION!");
}
}
private static long getDirSize(File dirs) {
long size = 0;
for (File file : dirs.listFiles()) {
if (file.isFile())
size += file.length();
else
size += getDirSize(file);
}
System.out.println("DIR - " + dirs+" "+ friendlyFileSize(size));
return size;
}
public static String friendlyFileSize(long size) {
String unit = "bytes";
if (size > 1024) {
size = size / 1024;
unit = "kb";
}
if (size > 1024) {
size = size / 1024;
unit = "mb";
}
if (size > 1024) {
size = size / 1024;
unit = "gb";
}
return " (" + size + ")" + unit;
}
}
This code get the output of all subdirectories instead showing a total of all of them and printing just top directory ??? Many thx for any help :D
FILE - c:\test\baba.pdf (4)mb
FILE - c:\test\babdb.txt (67)kb
DIR - c:\test\one\oneone (67)kb
DIR - c:\test\one (814)kb
DIR - c:\test\two\twotwo (322)kb
DIR - c:\test\two (368)kb
Your recquirement is that input a folder then show the sizes of all files(file including folder) of the folder.
We define the size of a folder is the sum of the size of files in the folder(including sub-folder).
So, the process of the source code is below:
(1) list-up all files of the folder as input.
(2) calculate file-size of listing in (1).
(3) show file-type(FILE OR DIR), the paths of files listing in (1), and file-size calculated in (2).
the source code of (1) and (3) is below:
public static void showFileSizes(File dir) {
File[] files = dir.listFiles(); // (1)
long[] fileSizes = new long[files.length];
for(int i = 0; i < files.length; i++) {
fileSizes[i] = calculateFileSize(file[i]);//invoke the method corresponding to (2).
boolean isDirectory = files[i].isDirectory();
System.out.println(((isDirectory)?"DIR":"FILE") + " - " + files[i].getAbsolutePath() + friendlyFileSize(fileSizes[i]));// as (3)
}
}
the source code of (2) is below:
public static long calculateFileSize(File file) {
long fileSize = 0L;
if(file.isDirectory()) {
File[] children = file.listFiles();
for(File child : children) {
fileSize += calculateFileSize(child);
}
}
else {
fileSize = file.length();
}
return fileSize;
}
The only thing you have to do is invoke showFileSizes method.
It is quite easy by using Java 7 new File I/O NIO.2 framework mostly by using Files.walkFileTree(Path, Set, int, FileVisitor) method.
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicLong;
public class Main {
private static class PrintFiles extends SimpleFileVisitor<Path> {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (attr.isDirectory()) {
try {
System.out.format("Directory: %s, size: %d bytes\n", file, getDirSize(file));
} catch (IOException e) {
e.printStackTrace();
}
} else if (attr.isRegularFile()) {
System.out.format("Regular file: %s, size %d bytes\n", file, attr.size());
}
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.err.println(exc);
return FileVisitResult.CONTINUE;
}
/**
* Walks through directory path and sums up all files' sizes.
*
* #param dirPath Path to directory.
* #return Total size of all files included in dirPath.
* #throws IOException
*/
private long getDirSize(Path dirPath) throws IOException {
final AtomicLong size = new AtomicLong(0L);
Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
size.addAndGet(attrs.size());
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
//just skip
return FileVisitResult.CONTINUE;
}
});
return size.get();
}
}
/**
* Main method.
*
* #param args
*/
public static void main(String [] args) {
Path p = Paths.get("d:\\octopress");
try {
Files.walkFileTree(p, EnumSet.noneOf(FileVisitOption.class), 1, new PrintFiles());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Keep it short and simple by using FileUtils.sizeOfDirectory(). You have to import org.apache.commons.io.FileUtils for this.
if (temp.isDirectory()) {
File dirs = new File(temp.getPath());
System.out.println("DIR - " + dirs+" "+ friendlyFileSize(FileUtils.sizeOfDirectory(dirs)));
}
Staying close to what you've already done, you would have to increment the directory size everytime you encounter it.
for (File temp : filesNames) {
if (temp.isDirectory()) {
dirs = new File(temp.getPath());
heavy += getDirSize(dirs);
} else {
System.out.println("FILE - " + temp.getPath() + " "
+ friendlyFileSize(temp.length()));
}
}
You also would want to display the the size after summing it all up against the parent of the subdirectories
public static void main(String[] args) {
...
...
System.out.println("DIR - " + dirs.getParent() + " "
+ friendlyFileSize(heavy));
}
Also, you need to check for whether the directory has any files or not, else dirs.listFiles() would cause a NPE
private static long getDirSize(File dirs) {
long size = 0;
if (dirs != null && dirs.listFiles() != null) {
for (File file : dirs.listFiles()) {
if (file.isFile())
size += file.length();
else
size += getDirSize(file);
}
}
return size;
}
Your whole code slightly modified:
public class SubDirs {
static long heavy;
public static void main(String[] args) {
File file = new File("C:\\Program Files");
File dirs = null;
if (file.isDirectory()) {
File[] filesNames = file.listFiles();
for (File temp : filesNames) {
if (temp.isDirectory()) {
dirs = new File(temp.getPath());
heavy += getDirSize(dirs);
} else {
System.out.println("FILE - " + temp.getPath() + " "
+ friendlyFileSize(temp.length()));
}
}
} else {
System.out.println("THIS IS NOT A FILE LOCATION!");
}
System.out.println("DIR - " + dirs.getParent() + " "
+ friendlyFileSize(heavy));
}
private static long getDirSize(File dirs) {
long size = 0;
if (dirs != null && dirs.listFiles() != null) {
for (File file : dirs.listFiles()) {
if (file.isFile())
size += file.length();
else
size += getDirSize(file);
}
}
return size;
}
public static String friendlyFileSize(long size) {
String unit = "bytes";
if (size > 1024) {
size = size / 1024;
unit = "kb";
}
if (size > 1024) {
size = size / 1024;
unit = "mb";
}
if (size > 1024) {
size = size / 1024;
unit = "gb";
}
return " (" + size + ")" + unit;
}
}
Im making a backup program, and I want everything that i have the program backing up displayed on a JTextArea. well, it works, but only after the program is finished with the backup. How do i fix this? The code i have running this is here:
backup method
public void startBackup() throws Exception {
// txtarea is the JTextArea
Panel.txtArea.append("Starting Backup...\n");
for (int i = 0; i < al.size(); i++) {
//al is an ArrayList that holds all of the backup assignments selected
// from the JFileChooser
File file = new File((String) al.get(i));
File directory = new File(dir);
CopyFolder.copyFolder(file, directory);
}
}
Copy Folder class:
public class CopyFolder {
public static void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
// if directory not exists, create it
if (!dest.exists()) {
dest.mkdir();
Panel.txtArea.append("Folder " + src.getName()
+ " was created\n");
}
// list all the directory contents
String files[] = src.list();
for (String file : files) {
// construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// recursive copy
copyFolder(srcFile, destFile);
}
} else {
try {
CopyFile.copyFile(src, dest);
} catch (Exception e) {
}
}
}
}
CopyFile class
public class CopyFile {
public static void copyFile(File src, File dest) throws Exception {
// if file, then copy it
// Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
// System.out.println("File copied from " + src + " to " + dest);
Panel.txtArea.append("File copied " + src.getName() + "\n");
}
}
Thanks for the help in advance, and let me know of any assistance i can give. I did a google search on this, and it does seem to be a big problem, but i just cant think of how to fix it. Oh, and please dont downvote this just because it doesnt apply to you, its very aggravating. Thanks in advance again!
EDIT:
This is what i got:
public class test extends SwingWorker<Void, String> {
String txt;
JTextArea txtArea = null;
public test(JTextArea txtArea, String str) {
txt = str;
this.txtArea = txtArea;
}
protected Void doInBackground() throws Exception {
return null;
}
protected void process(String str) {
txtArea.append(str);
}
protected void getString() {
publish(txt);
}
}
The main problem you're having is you're trying to perform blocking actions in the Event Dispatching Thread. This will prevent the UI from been updated as repaint requests are not reaching the repaint manager until AFTER you've finished.
To over come this, you're going to need to off load the blocking work (ie the back up process) to a separate thread.
For this I suggest you have a read through the Concurrency in Swing Trail which will provide you with some useful strategies to solve your particular problem. In particular, you'll probably benifit from using a SwingWorker
Take a close look at doInBackground and the process methods
UPDATED with Example
Okay, so this is a REALLY simple example. This basically walks you C:\ drive to 3 directories deep and dumps the content to the supplied JTextArea
public class BackgroundWorker extends SwingWorker<Object, File> {
private JTextArea textArea;
public BackgroundWorker(JTextArea textArea) {
this.textArea = textArea;
}
#Override
protected Object doInBackground() throws Exception {
list(new File("C:\\"), 0);
return null;
}
#Override
protected void process(List<File> chunks) {
for (File file : chunks) {
textArea.append(file.getPath() + "\n");
}
textArea.setCaretPosition(textArea.getText().length() - 1);
}
protected void list(File path, int level) {
if (level < 4) {
System.out.println(level + " - Listing " + path);
File[] files = path.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.isFile();
}
});
publish(path);
for (File file : files) {
System.out.println(file);
publish(file);
}
files = path.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.isDirectory() && !pathname.isHidden();
}
});
for (File folder : files) {
list(folder, level + 1);
}
}
}
}
You would simply call new BackgroundWorker(textField).execute() and walk away :D
UPDATED with explicit example
public class BackgroundWorker extends SwingWorker<Object, String> {
private JTextArea textArea;
private File sourceDir;
private File destDir;
public BackgroundWorker(JTextArea textArea, File sourceDir, File destDir) {
this.textArea = textArea;
this.sourceDir = sourceDir;
this.destDir = destDirl
}
#Override
protected Object doInBackground() throws Exception {
if (sourceDir.isDirectory()) {
// if directory not exists, create it
if (!destDir.exists()) {
destDir.mkdir();
publish("Folder " + sourceDir.getName() + " was created");
}
// list all the directory contents
String files[] = sourceDir.list();
for (String file : files) {
// construct the src and dest file structure
File srcFile = new File(sourceDir, file);
File destFile = new File(destDir, file);
// recursive copy
copyFolder(srcFile, destFile);
}
} else {
try {
copyFile(sourceDir, destDir);
} catch (Exception e) {
}
}
return null;
}
public void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
// if directory not exists, create it
if (!dest.exists()) {
publish("Folder " + src.getName() + " was created");
}
// list all the directory contents
String files[] = src.list();
for (String file : files) {
// construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// recursive copy
copyFolder(srcFile, destFile);
}
} else {
try {
copyFile(src, dest);
} catch (Exception e) {
}
}
}
public void copyFile(File src, File dest) throws Exception {
// if file, then copy it
// Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
publish("File copied " + src.getName());
}
#Override
protected void process(List<String> chunks) {
for (String msg : chunks) {
textArea.append(msg + "\n");
}
textArea.setCaretPosition(textArea.getText().length() - 1);
}
}
Now to run...
new BackgroundWorker(textArea, sourceDir, destDir).execute();