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.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    public static final String TAG = "P2PUtils";

    public static <T> List<T> readList(String root, String filename, Class<T> type) {
        List<T> objects = new ArrayList<>();
        File file = new File(root, filename);
        try {
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object object = ois.readObject();
                if (object instanceof List) {
                    for (Object it : (List) object) {
                        objects.add(type.cast(it));
                    }
                }
                ois.close();
                return Collections.unmodifiableList(objects);
            }
        } catch (Exception e) {
            Log.e(TAG, String.format("Failed to read [%s]", file), e);
        }
        return Collections.emptyList();
    }
}