// File Objekt für die Ursprungs-Datei erzeugen
File source = new File("e:/test.txt");
// File Objekt für die neue Datei erzeugen
File destination = new File("e:/test/test.txt");
// Datei wird umbenannt/verschoben
if(!source.renameTo(destination)){
System.err.println("Fehler beim Umbenennen der Datei: " + source.getName());
}
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
File oldFile = new File("C:/test/test.txt");
File newFile = new File("C:/test/test/test.txt");
if(!oldFile.renameTo(oldFile)){
// Wenn das nicht geht (andere Platte / von der VM nicht unterstützt)
FileInputStream reader = new FileInputStream (oldFile);
FileOutputStream writer = new FileOutputStream (newFile);
byte buffer[];
while(reader.available() > 0){
/* Wenn die verbleibende Groesse kleiner als die Buffergroesse ist */
buffer = new byte[1024 > reader.available() ? reader.available() : 1024];
reader.read(buffer);
writer.write(buffer);
}
reader.close();
writer.close();
oldFile.delete();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
File alt = new File("C:/feb/queue/");
File neu = new File("C:/feb/queue/gesendet/");
File[] files = alt.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
if(!files[i].renameTo(neu)) {
System.err.println("Fehler beim Umbennen der Datei: " + files[i].getName());
}
}
}
}
}
renameTo
cannot find sysbol
symbol: method renameTo(java.io.File)
location:class java.lang.String
public static void main(String[] args) throws FileNotFoundException, IOException {
File alt = new File("C:/feb/queue/");
File neu = new File("C:/feb/queue/gesendet/");
File[] files = alt.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
boolean success = files[i].renameTo(new File(neu, files[i].getName()));
if (!success) {
System.err.println("Fehler beim Umbennen der Datei: " + files[i].getName());
}
System.out.println(files[i].getName());
}
}
}
}
public static void main(String[] args)
{
File alt = new File("E:/");
String targetDir = "E:/test/";
File[] files = alt.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
File targetFile = new File(targetDir+files[i].getName());
if (targetFile.isFile()){
System.out.println("Datei bereits vorhanden");
continue;
}
if(!files[i].renameTo(targetFile)) {
System.err.println("Fehler beim Umbennen der Datei: " + files[i].getName());
}else {
System.out.println("Verschoben:"+ files[i].getName());
}
}
}
}