Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;

import java.io.ObjectInputStream;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Main {
    private static Map<UUID, String> uuidsToNames = null;
    private static String fileName = "yourName";

    public static String getValueByKeyMap(UUID key, File folder) {
        ensureMapIsUpToDate(folder);
        return uuidsToNames.get(key);
    }

    private static void ensureMapIsUpToDate(File folder) {
        if (uuidsToNames == null) {
            uuidsToNames = readPageNameHashesFromFile(folder);
        }
    }

    private static Map<UUID, String> readPageNameHashesFromFile(File folder) {
        Map<UUID, String> map = new HashMap<UUID, String>();

        try {
            FileInputStream fis = new FileInputStream(folder.getAbsolutePath() + fileName);
            ObjectInputStream ois = new ObjectInputStream(fis);
            map = (Map<UUID, String>) ois.readObject();
            ois.close();
            printMap();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Read from file", e.toString());
        }

        return map;
    }

    public static void printMap() {
        if (uuidsToNames != null) {
            String mapString = "Map: ";
            for (Map.Entry<UUID, String> entry : uuidsToNames.entrySet()) {
                mapString += entry.toString();
                mapString += "\n";
            }
            Log.e("Helpers: Map", mapString);
        } else {
            Log.e("Helpers: Map", "Map is null :(");
        }

    }
}