How to move files and directories - java

How to move files and directories into another directory in Java? I am using this technique to copy but I need to move:
File srcFile = new File(file.getCanonicalPath());
String destinationpt = "/home/dev702/Desktop/svn-tempfiles";
copyFiles(srcFile, new File(destinationpt+File.separator+srcFile.getName()));

You can try this:
srcFile.renameTo(new File("C:\\folderB\\" + srcFile.getName()));

Have you read this "http://java.about.com/od/InputOutput/a/Deleting-Copying-And-Moving-Files.htm
"
Files.move(original, destination, StandardCopyOption.REPLACE_EXISTING)
move the files to destination.
If you want to move directory
Use this
File dir = new File("FILEPATH");
if(dir.isDirectory()) {
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++) {
//move files[i]
}
}

Java.io.File does not contains any ready make move file method, but you can workaround with the following two alternatives :
File.renameTo()
Copy to new file and delete the original file.
public class MoveFileExample
{
public static void main(String[] args)
{
try{
File afile =new File("C:\\folderA\\Afile.txt");
if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
For Copy and delete
public class MoveFileExample
{
public static void main(String[] args)
{
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile =new File("C:\\folderA\\Afile.txt");
File bfile =new File("C:\\folderB\\Afile.txt");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
//delete the original file
afile.delete();
System.out.println("File is copied successful!");
}catch(IOException e){
e.printStackTrace();
}
}
}
HOPE IT HELPS :)

Related

How to copy content from a zip to an Android directory?

Hello everyone.
Ask this question so that they could give me a hand and guide me on my way.
My problem
I want to be able to unzip a zip into a folder or directory on the SD card, but my code has not reached the target. Its error is that it does not decompress or copy any of the files in the destination directory. The zip is located in the resources resource folder.
My code
private boolean copyFile1(String filename1, String outPath1) {
AssetManager assetManager = this.getAssets();
final int CHUNK_SIZE = 1024 * 4;
InputStream in;
OutputStream out;
try {
in = assetManager.open(filename1);
String newFileName = outPath1;
ZipInputStream zipStream = new ZipInputStream(in);
ZipEntry zEntry = null;
while ((zEntry = zipStream.getNextEntry()) != null) {
if (zEntry.isDirectory()) {
} else {
FileOutputStream fout = new FileOutputStream(new File(outPath1));
BufferedOutputStream bufout = new BufferedOutputStream(fout);
byte[] buffer = new byte[CHUNK_SIZE];
int read = 0;
while ((read = zipStream.read(buffer)) != -1) {
bufout.write(buffer, 0, read);
}
zipStream.closeEntry();
bufout.close();
fout.close();
}
}
zipStream.close();
Log.d("Unzip", "Unzipping complete. path : " );
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
return true;
}
If they realize where I failed in my code or I know otherwise. Please let me know. Thank you

export/zip eclipse project via java program for Windows and MAC

I need to export a (java) eclipse project without using eclipse. I tried to copy all relevant files and pack them with a ZipOutputStream. So good so far, I can load this zip-file in Windows. But MAC users have the problem, that the file structure is not automaticaly detected and create files with names like "vorlage\src\de\tuberlin..." next to the src directory.
(example picture)
Is there a way to properly export an eclipse project with a java programm?
The reason is that I created programming exercises for a lecture and marked the solutions with special comments. During copying the files these comments and the solutions are left out.
One solution would be importing the zip-file in Windos and exporting again with the help of eclipse. But this costs a lot of time doing this for each exercise.
Although I cannot see any difference between the two zip-files, there seems to be something.
Here my code to copy the files:
public static void copyDir(File source, File target, ArrayList<String> exclude)
throws FileNotFoundException, IOException {
File[] files = source.listFiles();
File newFile = null;
target.mkdirs();
if (files != null) {
for (int i = 0; i < files.length; i++) {
newFile = new File(target.getAbsolutePath() + System.getProperty("file.separator") + files[i].getName());
if (files[i].isDirectory()) {
copyDir(files[i], newFile, exclude);
}
else if ( !exclude.contains(files[i].getName()) ) {
copyFile(files[i], newFile);
}
}
}
}
public static void copyFile(File file, File target) throws FileNotFoundException, IOException {
if (file.getName().endsWith(".java")) {
copyJavaFile(file, target);
return;
}
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target, true));
int bytes = 0;
while ((bytes = in.read()) != -1) {
out.write(bytes);
}
in.close();
out.close();
}
private static void copyJavaFile(File file, File target) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(target);
boolean stopped = false;
String line = null;
while ( (line = in.readLine() ) != null) {
stopped |= line.contains("IF EXCLUDE");
if (!stopped && !line.contains("ENDIF"))
out.println(line);
stopped &= !line.contains("ELSE");
}
in.close();
out.close();
}
I copy the following (sub-)directories/files:
.settings
src
.classpath
.project
I create the zip-file with:
public Zip(final File projDir, final File outFile) {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(
new BufferedOutputStream(
new FileOutputStream(outFile)));
for (File file : projDir.listFiles()) {
writeFiles(file, output, "");
}
output.finish();
} catch (IOException ex) {
System.err.println("IO Error: " + ex.getMessage());
} finally {
try {output.close();} catch (IOException ex) {}
}
}
private void writeFiles(File f, ZipOutputStream output, String dir) throws IOException {
if (f.isDirectory()) {
// recursively write files in directory
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + File.separator);
}
} else {
// write this file to archive
FileInputStream fis = new FileInputStream(f);
ZipEntry entry = new ZipEntry(dir + f.getName());
entry.setTime(f.lastModified());
output.putNextEntry(entry);
copy(fis, output);
output.closeEntry();
fis.close();
}
}
private void copy(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = is.read(buffer)) > 0) {
os.write(buffer, 0, bytes);
}
}
It is a problem of the path separator. Using "/" instead of "File.separator" solves the problem.
In the example above the for-loop in the method writeFiles(...) must be:
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + "/");
}
See also this answer.

Asynchronous File copy in java

I want to know how we can move a file to a different location asynchronously,
basically one of my program is keep on appending data to a temporary file and another is checking its size on exceeding the predefined size of the temporary file it should be copied to a permanent directory and the previous program should keep create a new temporary file and continue writing, in between the data copied should not lost.
I tried to do the same with the following code but some of the data are either misplaced or lost.
import java.io.*;
import org.apache.log4j.Logger;
public class FileSizeMonitor {
static Logger log = Logger.getLogger(FileSizeMonitor.class.getName());
private static final String tempDirectory = "c:\\my-temp-dir";
private static final String tempFileName = tempDirectory + "\\" + "my-temp-file.txt";
void FileMonitor() {
File file = new File(tempFileName);
double bytes = 0;
if (file.exists()) {
bytes = file.length();
if (bytes > 2458.0001) {
int length;
byte[] buffer = new byte[1024];
try {
InputStream inStream = null;
OutputStream outStream = null;
String source = tempFileName;
String target = "C:\\Users\\data";
File sourceFile = new File(source);
String fileName = sourceFile.getName();
File targetFile = new File(target + fileName);
inStream = new FileInputStream(sourceFile);
outStream = new FileOutputStream(targetFile);
log.debug("File size exceded the predefined limit,moving the file data to C:\\Users\\data");
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
sourceFile.delete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}

Java zip util adding extra folders

I'm having an issue zipping up folders.
I.e. I'll have a folder like this
C:\Lazy\Test
containing files File1.cpp, File2.hpp,.. etc
The zipped up folder looks like C:\Lazy\Test.zip -> Lazy\Test which contains all the cpp and hpp files.
I want to remove the extra subfolders (Lazy\Test) that get created. Why is this happening?
In other words, the zipped up files are not directly underneath the zip file, I have to navigate two more folders to get to them.
Where can I find this issue in the code?
private void zipDirectory() {
File lazyDirectory = new File(defaultSaveLocation);
File[] files = lazyDirectory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Zipping up " + file);
zipContents(file);
}
}
}
public static void addToZip(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
public static void zipContents(File dirToZip) {
List<File> fileList = new ArrayList<File>();
File[] filesToZip = dirToZip.listFiles();
for (File zipThis : filesToZip) {
String ext = "";
int i = zipThis.toString().lastIndexOf('.');
if (i > 0) {
ext = zipThis.toString().substring(i+1);
}
if(ext.matches("cpp|bem|gz|h|hpp|pl|pln|ppcout|vec|xml|csv")){
fileList.add(zipThis);
}
}
try {
FileOutputStream fos = new FileOutputStream(dirToZip.getName() + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : fileList) {
addToZip(file.toString(), zos);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Change the addToZip file to take File object. Use it to open the file stream, but only use File#getName to create the ZipEntry, as follows...
public static void addToZip(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}

Creating Folders in a Zip Folder in Java [duplicate]

This question already has answers here:
directories in a zip file when using java.util.zip.ZipOutputStream
(6 answers)
Closed 9 years ago.
My task requires me to save a file directory into a zip folder. My only problem is I need to keep the sub-folders as folders from the main Directory. The file system will look something like
C\\Friends
C:\\Friends\\Person1\\Information.txt
C:\\Friends\\Person2\\Information.txt
C:\\Friends\\Person3\\Information.txt
.
.
.
Right now I am able to write just the txt files inside of my zip folder, but in my zip folder I need to keep that folder structure. I know the way my code is right now will tell me the file I'm trying to write is closed(No access). My Functions thus far:
private String userDirectroy = "" //This is set earlier in the program
public void exportFriends(String pathToFile)
{
String source = pathToFile + ".zip";
try
{
String sourceDir = userDirectory;
String zipFile = source;
try
{
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, fileSource);
zout.close();
System.out.println("Zip file has been created!");
}
catch(Exception e)
{
}
}
catch(Exception e)
{
System.err.println("First Function: " + e);
}
}
private static void addDirectory(ZipOutputStream zout, File fileSource) {
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for(int i=0; i < files.length; i++)
{
if(files[i].isDirectory())
{
try
{
byte[] buffer = new byte[1024];
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
}
catch(Exception e)
{
System.err.println(e);
}
addDirectory(zout, files[i]);
continue;
}
try
{
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
catch(IOException ioe)
{
System.out.println("IOException :" + ioe);
}
}
}
Any help would be much appreciated. Thank You
For each folder, you need to add a empty ZipEntry of the path.
For each file, you need to supply both the path and file name. This will require you to know the part of the path to strip off, this would be everything after the start directory
Expanded concept
So, from your example, if the start directory is C:\Friends, then the entry for C:\Friends\Person1\Information.txt should look like Person1\Information.txt
public void exportFriends(String pathToFile) {
String source = pathToFile + ".zip";
try {
String sourceDir = "C:/Friends";
String zipFile = source;
try {
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, sourceDir, fileSource);
zout.close();
System.out.println("Zip file has been created!");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getRelativePath(String sourceDir, File file) {
// Trim off the start of source dir path...
String path = file.getPath().substring(sourceDir.length());
if (path.startsWith(File.pathSeparator)) {
path = path.substring(1);
}
return path;
}
private static void addDirectory(ZipOutputStream zout, String sourceDir, File fileSource) throws IOException {
if (fileSource.isDirectory()) {
// Add the directory to the zip entry...
String path = getRelativePath(sourceDir, fileSource);
if (path.trim().length() > 0) {
ZipEntry ze = new ZipEntry(getRelativePath(sourceDir, fileSource));
zout.putNextEntry(ze);
zout.closeEntry();
}
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDirectory(zout, sourceDir, files[i]);
} else {
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(getRelativePath(sourceDir, files[i])));
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
}
}
}

Categories

Resources