JavaRush /Java Blog /Random EN /Coffee break #99. How to copy files from one directory to...

Coffee break #99. How to copy files from one directory to another in Java. Strings in Java

Published in the Random EN group

How to copy files from one directory to another in Java

Source: DZone I've been programming in Java for a long time, and you won't believe it, but before Java 7, there was no method for copying files in the Java API. Our only options were to write it ourselves, using FileInputStream, FileOutputStream and a buffer to copy bytes from one to another; or better yet, use the FileChannel.transferTo() method or Apache Commons FileUtils, which were a lifesaver in those days and still exist today. Coffee break #99.  How to copy files from one directory to another in Java.  Strings in Java - 1The JDK has now evolved and there is already a decent API for copying files from one directory to another. In this article, I will show you how we copied files from one directory to another before Java 7, as well as a modern way that makes this task much easier.

1. Copying files from one directory to another before Java 7

Dependency: Apache Commons IO

1.1 FileUtils.copyFile(file source, file destination) This method copies the file to a new location while maintaining the file's timestamp. It also copies the contents of the specified source file to the specified destination file. The directory containing the target file is created if it does not exist . If the target file exists, then this method will overwrite it.
import java.io.File;

import java.io.IOException;



import org.apache.commons.io.FileUtils;



/**

* Java program to copy a file from one directory to another e.g. from src to dest

*

* @author Javin

*/

public class FileCopyDemo {



public static void main(String args[]) {



// Using Apache Commons FileUtils class

File srcFile = new File("bin/HelloWorld.class");

File destFile = new File("target/HelloWorld.class");

try {

FileUtils.copyFile(srcFile, destFile);

System.out.println("File successfully copied in Java");

} catch (IOException e) {

e.printStackTrace();

}



}



}

Output:

System.out.println("File successfully copied in Java");
1.2 Copy a file to a directory while maintaining a timestamp This method copies the contents of the specified source file to a file with the same name in the specified target directory. The target directory is created if it does not exist . If the target file exists, this method will overwrite it.
import java.io.File;

import java.io.IOException;



import org.apache.commons.io.FileUtils;



/**

* Java program to copy a file from one directory to another like from src to dest

*

* @author Javin Paul

*/

public class Testing {



public static void main(String args[]) {



// Using Apache Commons FileUtils class

File srcFile = new File("bin/HelloWorld.class");

File destDir = new File("target");

try {

FileUtils.copyFileToDirectory(srcFile, destDir);



System.out.println("File successfully copied to destination directory in Java");

} catch (IOException e) {

e.printStackTrace();

}



}



}



Output

File successfully copied to destination directory in Java

2. Copy files from one directory to another using Java 7 NIO 2 API.

Java 7 has a standard method for copying files: Files.copy . It integrates with native I/O O/S to provide high performance.
import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;



import static java.nio.file.StandardCopyOption.*;

import static java.nio.file.LinkOption.*;



/**

* Java program to copy file using Java 7 Files.copy() method

*

* @author Javin Paul

*/

public class FileCopyDemo {



public static void main(String args[]) {



try {

Path bytes = Files.copy(

new Java.io.File("bin/HelloWorld.class").toPath(),

new java.io.File("target/HelloWorld.class").toPath(),

REPLACE_EXISTING,

COPY_ATTRIBUTES,

NOFOLLOW_LINKS);

System.out.println("File successfully copied using Java 7 way");



} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}



}
You can also copy files in Java by writing code using FileInputStream and FileOuputStream , but this is not necessary if you have Java 7 installed. The Apache Commons IO FileUtils class is also a handy alternative. For high-speed file copying and transferring, you can also take advantage of the java.nio and FileChannel classes , but be aware that there is a bug in Windows that prevents you from transferring more than 64GB of channel data.

Strings in Java

Source: Dev.to Like other programming languages, a String in Java is a sequence of characters. But Java does not implement a string as an array of characters, but treats it as a complete String object . Coffee break #99.  How to copy files from one directory to another in Java.  Strings in Java - 2

How to create a string object using the new keyword and literals

There are two ways to create a string object: using the new keyword or using literals. Literal representation means representing its value as an integer or string. The code below shows how we can create a string using the new keyword.
String a = new String();
As we know, the new keyword is used to create an instance of this class. Above we created an instance of “a” type string without characters. To create a string with a value in it, you can do it like this.
char name[] = {'x','y','z'}
String a = new String(chars);
Above we created a character array name[ ] with the values ​​“x”, “y”, “z”, and then assigned this complete array to the string “a”. We used the constructor of the String class to initialize the value of the string. As we saw above, creating a string using the new keyword is a bit confusing and time-consuming. But there is a really simple way, and this is where literals come to our rescue.
String s = "xyz";
This is how we create a string in Java using literals. For every string literal in a program, Java automatically creates a String object with an initial value provided to it. You can use a string literal anywhere in a program to create a string object from it.

Example string

Here is the code for a simple Hello world program with a few lines.
public class HelloWorld {
    public static void main(String args[]) {

        String s1 = new String("Hello World using new keyword"); // Using new keyword
        String s2 = "Hello World using literals";

        System.out.println(s1);
        System.out.println(s2);
}
}
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION