Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes JTextArea has to be updated when copying directories and files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JTextArea has to be updated when copying directories and files" Watch "JTextArea has to be updated when copying directories and files" New topic

JTextArea has to be updated when copying directories and files

Kamal ddddd

Joined: Dec 03, 2012
Posts: 1
Have a JTextArea and wanted to show the directories and files being copied. Here is my code but no idea how to proceed(Basically, I want all the System.out.println statements in the code to be shown in TextArea. I used append but it doesn't do anything. Can someone help

public class copy {

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Copy c = new Copy();
c.setTitle("Copy folders");

JPanel jp = new JPanel();

TextArea ta = new JTextArea(5, 50);
DefaultCaret caret = (DefaultCaret) ta.getCaret();
JScrollPane scrollPane = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
scrollPane.setBounds(6, 625, 1035, 296);
jp.add(scrollPane); //Adding to JPanel

public Copy() {

public void build() {
String source = "\\hostname\\d$\\somedirecotry";
String detination = "\\C:\\foldername";
File s = new File(source);
File s2 = new File(detination);

if (!s.exists()) {
System.out.println("Directory does not exist.");
} else if (!s2.exists()) {
System.out.println("Directory is not accessible or Server is down");
} else {
try {
copyFolder(s, s2);
} catch (IOException e) {

public static void copyFolder(File src, File dest)
throws IOException {
if (src.isDirectory()) {
//if directory not exists, create it
if (!dest.exists()) {
System.out.println("Directory copied from " + src + " to " + dest);

//list all the directory contents
String files[] = src.list();

for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
copyFolder(srcFile, destFile);
} else {
//if file, then copy it
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 = > 0) {
out.write(buffer, 0, length);

System.out.println("File copied from " + src + " to " + dest);
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I agree. Here's the link:
subject: JTextArea has to be updated when copying directories and files
It's not a secret anymore!