Need a quick help. I am new to Java..In my project I have input & output folder in resources. Inside input folder I have a csv file. I need to move that file to output folder through java code. How to copy that input file to output directory. I have tried in google but not getting a working solution. My project structure is based on standard maven project.
Asked
Active
Viewed 8,348 times
-4
-
Is this a maven question? Do you want to copy the file during a build? – Paul Hicks Aug 30 '17 at 02:51
-
Basically I have to copy that file from input folder to output folder. How to do that? – Sujata Roy Aug 30 '17 at 02:52
-
No. I want to do this through java coding – Sujata Roy Aug 30 '17 at 02:52
-
I would try googling, or searching here on stackoverflow. Copying a file using Java is a solved problem. – Paul Hicks Aug 30 '17 at 02:53
-
But not from one resource folder to another. I was getting Nullpointer while trying other solution – Sujata Roy Aug 30 '17 at 02:54
-
Try a little bit of googling like Paul suggested. Also have a look at https://stackoverflow.com/help/how-to-ask – Balwinder Singh Aug 30 '17 at 02:54
-
Have a look in the "Related" column on the right. The second suggestion for me is "[Standard consise way to copy a file in Java](https://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java?rq=1)". Would that help? – Paul Hicks Aug 30 '17 at 02:54
-
@BalwinderSingh tried almost 2 days..Still no luck – Sujata Roy Aug 30 '17 at 02:55
-
@PaulHicks No it did not helped – Sujata Roy Aug 30 '17 at 02:55
-
1@SujataRoy share the relevant code and explain what part isn't working and what is the error you are getting. Go through https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask links and then update your question accordigly – Balwinder Singh Aug 30 '17 at 02:55
-
sure let me share the code – Sujata Roy Aug 30 '17 at 02:57
-
ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("/input").getFile()); Path path = Paths.get("/output"); Files.copy(file.toPath(), path,StandardCopyOption.REPLACE_EXISTING); – Sujata Roy Aug 30 '17 at 02:59
-
This is the code.. I am getting nul pointer while trying to get output folder absolute path – Sujata Roy Aug 30 '17 at 03:00
-
1You need to share the code in the question itself. Also sharing the code won't help until you share the error and your observations. Like I said go through stackoverflow.com/help/mcve and stackoverflow.com/help/how-to-ask links and then update your question accordingly – Balwinder Singh Aug 30 '17 at 03:06
1 Answers
2
I think there are at least four methods you can use to move your csv file to some other directory.
I assume that you already know the absolute directory path.
Method 1. The way of apache commons
You can use apache commons io library.
public static void moveWithApacheCommonsIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
try {
FileUtils.moveFile(sourceFile, destinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Method 2. The way of java's NIO
You can also use nio (non-blocking io) in java
public static void moveWithFileNIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
sourceFile.deleteOnExit();
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final FileChannel inChannel = inputStream.getChannel();
final FileChannel outChannel = outputStream.getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
inChannel.close();
outChannel.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Method 3. The traditional way of java's IO
You can use traditional method with file in and output stream in java.(Blocking IO)
public static void moveWithFileInOutStream()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
InputStream fin = null;
OutputStream fout = null;
sourceFile.deleteOnExit();
try {
fin = new BufferedInputStream(new FileInputStream(sourceFile));
fout = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] readBytes = new byte[1024];
int readed = 0;
while((readed = fin.read(readBytes)) != -1)
{
fout.write(readBytes, 0, readed);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Method 4. Using JNI(Java Native Interface)
Finally, you can use some function like mv or MovFile depending on your Operating system with JNI.
I think it is out of scope of this topic. you can google it or see JNA library to accomplish your task if you want.
Here are complete code for you.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import org.apache.commons.io.FileUtils;
public class MovefileTest {
public static void moveWithApacheCommonsIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
try {
FileUtils.moveFile(sourceFile, destinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void moveWithFileInOutStream()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
InputStream fin = null;
OutputStream fout = null;
sourceFile.deleteOnExit();
try {
fin = new BufferedInputStream(new FileInputStream(sourceFile));
fout = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] readBytes = new byte[1024];
int readed = 0;
while((readed = fin.read(readBytes)) != -1)
{
fout.write(readBytes, 0, readed);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void moveWithFileNIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
sourceFile.deleteOnExit();
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final FileChannel inChannel = inputStream.getChannel();
final FileChannel outChannel = outputStream.getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
inChannel.close();
outChannel.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//moveWithApacheCommonsIO();
//moveWithFileNIO();
moveWithFileInOutStream();
}
}
Regards,

tommybee
- 2,409
- 1
- 20
- 23
-
If you just want to move a file then Files.move is easy to use, it provides the ability the replace an existing file if you need it. – Alan Bateman Sep 09 '17 at 15:08
-
What if you don have Files.move? This is not a question. I did answer. why don you use it as an answer – tommybee Sep 10 '17 at 01:10