0% found this document useful (0 votes)
14 views2 pages

JavaScript - JavaScript Bible (Gold Edition)

This document presents the AdminArchivo class in Java, which contains methods to manage files such as creating directories, copying content between files, retrieving files from a directory, and writing, moving, and modifying files. The main method demonstrates how to use the createDirectory and copyFileContent methods to copy the content from one file to another after creating a destination directory if necessary.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

JavaScript - JavaScript Bible (Gold Edition)

This document presents the AdminArchivo class in Java, which contains methods to manage files such as creating directories, copying content between files, retrieving files from a directory, and writing, moving, and modifying files. The main method demonstrates how to use the createDirectory and copyFileContent methods to copy the content from one file to another after creating a destination directory if necessary.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The provided text is a URL and does not contain translatable content.

package epn.javaadv.stream;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
Class to manage the general operations of files using JAVA,
in which the use of the fundamental objects FileReader, FileWriter will be demonstrated
* @author martosfre
See www.matoosfe.com
Sep 23, 2010
*/
public class AdminArchivo {

/**
Method to create a directory, it is not necessary to calculate the IOException exception
* @param directoryName
* @param path
* @return
*/
public boolean createDirectory(String directoryName, String path){
File directory = new File(path + File.separatorChar + directoryName);
return directory.mkdir();
}

/**
Method to copy the content of one file to another
* @param sourceFile
* @param destinationFile
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public boolean copyFileContent(File sourceFile, File destinationFile) throws IOException,
ClassNotFoundException{

boolean copyFlag = false;


int car = 0;
We create an object to read the data from the file
FileReader sourceFileReader = new FileReader(sourceFile);
We create an object to write the read data to the new file.
FileWriter destinationFileWriter = new FileWriter(destinationFile);

while((car = sourceFileReader.read()) != -1){


destinationFileWriter.write(car);
}
sourceFileReader.close();
destinationFileWriter.close();
banderaCopiar = true;
return banderaCopiar;

/**
Method to recover the files given a directory
* @param directory
* @return
*/
public File[] recoverFilesFromDirectory(File directory) throws IOException{

File[] files = null;

We check that it is a directory and that it exists


if(directory.exists() && directory.isDirectory()) {
files = directory.listFiles();
for (File file : files) {
System.out.println("File:" + archivo.getName());
}
}
return files;
}

/**
Method for writing data to a file
* @return
*/
public File writeFile() {
return null;

1 of 2 September 18, 2012


The provided text is a URL and cannot be translated.

/**
Method to move a file from one location to another
* @return
*/
public boolean moveFile() {
return false;
}

/**
Method to modify a file following what is already there
* @return
*/
public boolean modifyFile() {
return false;
}

public static void main(String[] args) {


It is necessary to use the separator character in order to scale from an operating system.
to another avoiding name issues.
*/
String fileLocation = File.separatorChar + "home" + File.separatorChar + "martosfre";

AdminFile adminFile = new AdminFile();

try {
The sourceFile.txt must be created beforehand before proceeding as it is the data source.
File sourceFile = new File(fileLocation + File.separatorChar + "sourceFile.txt");
File destinationFile = new File(fileLocation + File.separatorChar + "destinationFile.txt");
We verify that the directory exists
if(new File(locationFile + File.separatorChar + "cec_epn").exists()){
:
if(successfulCopy){
adminArc.retrieveDirectoryFiles(new File(fileLocation));
}
translatedText
if(adminArc.createDirectory("cec_epn", fileLocation)){
boolean successfulCopy = adminArc.copyFileContent(sourceFile, destinationFile
if(successfulCopy){
adminArc.recoverFilesDirectory(new File(fileLocation));
}
else {
Problems creating directory, possible permission error
}
}
} catch (IOException e) {
System.out.println("Error IO:" + e.getMessage());
e.printStackTrace();
catch (ClassNotFoundException e) {
System.out.println("Error CNF:" + e.getMessage());
e.printStackTrace();
}catch(Exception e){
System.out.println("General Error:" + e.getMessage());
}
}
}

2 of 2 09/18/2012 16:08

You might also like