Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import java.util.ArrayList;

import android.accounts.OperationCanceledException;

public class Main {
    /**
     * replaces a list of files with another list of files, indicated by names
     * @param originalList
     * @param newList
     * @return
     */
    public static void replaceFiles(ArrayList<String> originalList, ArrayList<String> newList)
            throws UnsupportedOperationException, OperationCanceledException {

        if (originalList.size() != newList.size())
            throw new UnsupportedOperationException();
        else {
            String name = null;
            for (int i = 0, size = originalList.size(); i < size; i++) {
                name = originalList.get(i);
                File f = new File(name);
                File newF = new File(newList.get(i));
                if (f.exists() && newF.exists()) {
                    if (f.delete()) {
                        File temp = new File(name);
                        newF.renameTo(temp);
                    } else {
                        throw new OperationCanceledException("Delete failed");
                    }
                } else {
                    throw new UnsupportedOperationException("Wrong lists of file names");
                }
            }
        }
    }
}