Denne side indeholder artikler med forskellige perspektiver på Identity & Access Management i private og offentlige organisationer. Artiklerne behandler aktuelle IAM-emner og leveres af producenter, rådgivere og implementeringspartnere.
public static void copy(String from_name, String to_name) throws IOException{ File from_file = new File(from_name); // Get File objects from Strings File to_file = new File(to_name);
// First make sure the source file exists, is a file, and is readable. if (!from_file.exists()) abort("FileCopy: no such source file: " + from_name); if (!from_file.isFile()) abort("FileCopy: can't copy directory: " + from_name); if (!from_file.canRead()) abort("FileCopy: source file is unreadable: " + from_name);
// If the destination is a directory, use the source file name // as the destination file name if (to_file.isDirectory()) to_file = new File(to_file, from_file.getName());
String parent = to_file.getParent(); // Get the destination directory File dir = new File(parent); // Convert it to a file. if (!dir.exists()) abort("FileCopy: destination directory doesn't exist: " + parent); if (dir.isFile()) abort("FileCopy: destination is not a directory: " + parent); if (!dir.canWrite()) abort("FileCopy: destination directory is unwriteable: " + parent);
FileInputStream from = null; // Stream to read from source FileOutputStream to = null; // Stream to write to destination try { from = new FileInputStream(from_file); // Create input stream to = new FileOutputStream(to_file); // Create output stream byte[] buffer = new byte[4096]; // A buffer to hold file contents int bytes_read; // How many bytes in buffer while((bytes_read = from.read(buffer)) != -1) // Read bytes until EOF to.write(buffer, 0, bytes_read); // write bytes } // Always close the streams, even if exceptions were thrown finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } }
/** A convenience method to throw an exception */ private static void abort(String msg) throws IOException { throw new IOException(msg); } }
Alt indtil FileInputStream from = null er bare for at sikre at forholdende er ok (dvs. at filen, som skal kopieres faktisk eksisterer og at der ikke forsøges at overskrive en skrivebeskyttet fil etc.)
try-catch blokken laver så en til og fra stream... Læser fra fra-streamen og skriver til til-streamen med 4k per gang inden til den møder EOF (End-Of-File)
while((bytes_read = from.read(buffer)) != -1) hvad sker der her
Synes godt om
Slettet bruger
05. juni 2002 - 00:58#4
Så længe at det der noget at læse fra filen vil den returnere en positiv (+) værdi, når den når enden af filen og forsøger at læse ud over enden returneres der en negativ værdi, nemlig -1. På den måde kan man "måle" på om man er færdig med at læse filen
Ved at teste for en negativ værdi (mere specifikt -1) kan man teste om man nu er nået til slutningen af filen.
While-løkken vil tage "bidder" af 4k fra kilde-filen og putte i kopi-filen indtil der ikke er fler "bidder" at tage af. og det finder den ud af ved at opdage at den har læst -1 bytes fra kildefilen
For at vise, hvordan du kan bruge Beanen, så er her et eksempel
import com.skroeder.bean.util.FileCopy; class CopyTest { public static void main(String args[]) { /* Da copy er en statisk metode, behøves der ikke at laves et nyt */ /* instantieret FileCopy-objekt. Metoden kan bruges som følger */
/* Dette eksempel vil kopierer "C:\autoexec.bat" til a:-drevet */ FileCopy.copy("c:\\autoexec.bat", "a:\\"); } }
/** * * @author reinke * @version */ public class dirMove extends Object { /** The error code */ private int error=0;
File sourceDir,destDir;
public dirMove() { }
/** Moves directory from source to dest including everything beneath it * @param source The filepath to the source directory * @param dest The filepath to the destination * @return According to result of the moving of the directory */ public boolean move(String source, String dest) { //check if either source or dest is empty or null if ( (source.trim().length()==0) || (source==null) || (dest.trim().length()==0) || (dest==null) ) { error=1; return false; }
/** Returns the error text according to the value of 'error' * @return String containing the Error Text according to the value of {@link error} */ public String getError() { if(error==1) return "source eller dest er tom"; if(error==2) return "source er ikke en directory"; if(error==3) return "destination findes allerede!!!"; if(error==4) return "Could not create Directory"; if(error==5) return "File copy failed"; return ""; } }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.