Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;

public class Main {
    public static ArrayList<String> getIPAddress() {
        ArrayList<String> address_list = new ArrayList<String>();

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;
            while ((line = br.readLine()) != null) {

                if (line.startsWith("192")) {
                    //there exists an ip address
                    String[] deviceNetworkInfo = line.split(" ");
                    String necessaryInfo[] = new String[6];
                    int j = 0;
                    for (int i = 0; i < deviceNetworkInfo.length; i++) {
                        if (deviceNetworkInfo[i].length() != 0) {
                            necessaryInfo[j] = deviceNetworkInfo[i];
                            j++;
                        }
                    }

                    address_list.add(necessaryInfo[0]);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //return the value
        return address_list;
    }
}