Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.util.*;
import java.io.*;

public class Main {
    public static void saveObj(Object obj, String fileName) throws IOException {
        File f = new File(fileName + ".tmp");
        f.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
        out.close();
        fos.flush();
        fos.close();
        File file = new File(fileName);
        file.delete();
        f.renameTo(file);
    }

    public static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean delete(File file) {
        file.setWritable(true);
        try {
            if (!file.delete()) {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(0);
                fos.flush();
                fos.close();
            }
            Log.d("delete", "Deleted file: " + file + " successfully");
            return true;
        } catch (IOException e) {
            Log.d("delete", "The deleting file: " + file + " is not successfully", e);
            return false;
        }
    }

    private static void delete(File file, StringBuilder sb) {
        long length = file.length();
        boolean deleted = file.delete();
        if (deleted) {
            sb.append(file.getAbsolutePath() + " length " + length + " bytes, deleted.\r\n");
        } else {
            sb.append(file.getAbsolutePath() + " length " + length + " bytes, can't delete.\r\n");
        }
    }
}