1

Actually I have a file in My Internal Storage named file.txt which has contents as follows

device_type=10-0050F204-5
os_version=01020300
config_methods=physical_display virtual_push_button
p2p_no_group_iface=1
external_sim=1
wowlan_triggers=disconnect

network={
    ssid="KP'S TV"
    psk="thebestwifi12"
    key_mgmt=WPA-PSK
    priority=7
}

network={
    ssid="Pal 2.1"
    psk="paldroid"
    key_mgmt=WPA-PSK
    priority=8
}

network={
    ssid="Dad"
    psk="kaustubh"
    key_mgmt=WPA-PSK
    priority=9
}

network={
    ssid="AndroidAP"
    key_mgmt=NONE
    priority=10
}

I am basically Trying to store all names after 'ssid=' in one array and other array holds all values after 'psk=' such that my array1 of ssid should contains [KP'S TV,Pal 2.1,Dad,AndroidAP] and array2 of psk should contains [thebestwifi12,paldroid,kaustubh,NONE]

As you notice that last loop of network does not contain psk so I want such that in array2 of psk if there is no psk it holds NONE as shown above.

I have tried to print array on two text view

For this here is my Main Activity Code

import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class MainActivity extends AppCompatActivity {
TextView test,test1;
    String temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test = (TextView) findViewById(R.id.textbox1) ; // textbox1 is my first textview
        test1 = (TextView) findViewById(R.id.textbox2) ; // textbox2 is my first textview
        readandgetarray();
    }

    public void readandgetarray(){

       // Reading File

       File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir,"file.txt");
        if(file.exists())
        {
           final StringBuilder textfile = new StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    textfile.append(line);
                }
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
            }
            test.setText(textfile);
             temp = textfile.toString();
        }
        else
        {
            test.setText("Sorry file doesn't exist!!");
        }
               // Storing it into Array Now

                String str = temp;
                StringTokenizer tokenizer = new StringTokenizer(str, "\n");
                List<String> array1 = new ArrayList<>();
                List<String> array2 = new ArrayList<>();
                List<String> arraydef = new ArrayList<>();
                while (tokenizer.hasMoreElements()) {
                    String line = (String) tokenizer.nextElement();
                    String[] split = line.split("=");
                    String key = split[0];
                    String value = split[1];
                    switch (key) {
                        case "ssid":
                            array1.add(value);
                            break;
                        case "psk":
                            array2.add(value);
                            break;
                        default:
                            arraydef.add(value);
                    }
                }
                test.setText(array1.toString());
                test1.setText(array2.toString());
            }

    }

Now When i test this, it gives me both array empty. Am i doing something wrong. Please Anyone Help! Also I have set WRITE_EXTERNAL_STORAGE Permission in Manifest as well as even though i haven't catch run time permission i have enabled that storage permission for my app in settings.

Still I am getting both array as empty. Could any one say what am i doing wrong.

2
  • There is no easy way to get the information you want other than to parse it. I wonder if Android does not have some library for parsing it; you should check for this. Commented Jul 16, 2017 at 14:00
  • Does the file have to be in this format. It would be much easier if you write it in JSON, and then just use the JSON classes. Also, your design is bad, you shouldn't be holding the data in separate arrays, rather you should create a Network class, and hold a single array of it. Using a JSON class, will do the same thing. Commented Jul 16, 2017 at 14:14

2 Answers 2

1

I believe the problem lies in this code:

            while (tokenizer.hasMoreElements()) {
                String line = (String) tokenizer.nextElement();
                String[] split = line.split("=");
                String key = split[0];
                String value = split[1];
                switch (key) {
                    case "ssid":
                        array1.add(value);
                        break;
                    case "psk":
                        array2.add(value);
                        break;
                    default:
                        arraydef.add(value);
                }
            }

It appears that your file has whitespace before "ssid" and "psk" (i.e. the line is " ssid=foo"), so I expect that key is never equal to "ssid", though it might be equal to " ssid".

Sign up to request clarification or add additional context in comments.

Comments

0

I think part of your trouble is that the complexity of your method is making it hard to debug. I suggest the code below, to be used after you've already gotten your file into the file variable.

if(file.exists())
{
    try {
        ArrayList<String> ssids = new ArrayList<>();
        ArrayList<String> psks = new ArrayList<>();
        String data = new String(Files.readAllBytes(file.toPath()));
        Pattern networkPat = Pattern.compile("network=\\{.*?\\}", Pattern.DOTALL);
        Pattern ssidPat = Pattern.compile("ssid=\"(?<ssid>.*?)\"");
        Pattern pskPat = Pattern.compile("psk=\"(?<psk>.*?)\"");
        Matcher netMatch = networkPat.matcher(data);
        while (netMatch.find())
        {
            Matcher ssidMatch = ssidPat.matcher(netMatch.group());
            ssidMatch.find();
            ssids.add(ssidMatch.group("ssid"));
            Matcher pskMatch = pskPat.matcher(netMatch.group());
            psks.add((pskMatch.find()) ? pskMatch.group("psk") : "NONE");
        }
    }
    catch (IOException e) {
       //You'll need to add proper error handling here
    }
}
else
{
    //Do whatever you want here.
}

This just grabs the whole file into one String (data), then scans through all of the networks in the file (as per the regex "network=\\{.*?\\}", a network is defined as network={everything here including newlines}), and for each network, searches for a ssid and a psk, and puts the ssid into the ssid ArrayList, then checks if there is a psk, and puts either the psk or "NONE" into the psk list.

I tested that locally and it works. If you still get empty arrays using that code, I suggest you try to make sure that you are actually loading the correct file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.