Question: Consider the following code:

```
def print_mah(n):
    if n <= 0:
        return
    else:
        print('mah') 
        print_mah(n-1) 

print_mah(3)

```

Here Python checks if n is less than or equal to 0, it finds that this is **False** so it prints 'mah' and calls the same function with n-1 until n is equal to 0, so 'mah' is printed 3 times.

But consider this manipulated code:

```
def print_mah(n):
    if n <= 0:
        return
    else: 
        print_mah(n-1)
        print('mah')

print_mah(3)

```

Python checks if n is less than or equal to 0, it finds that this is **False** so calls the same function again with n-1, and 'mah' is printed 3 times, too.

My question is that why 'mah' isn't printed just one time, in other words why print\_mah isn't called with n=2, then Python finds that the condition is **False**, so it calls it with n=1, and finds the condition is **False**, so it calls it with n==0, and finds that the condition is **True**, so the function returns, and after that 'mah' is printed, only, once.

Answer: 
There are two *separate* concerns here:

1. getting a URL from the .txt file
2. loading a URL in `WebView`

You should think of them separately. You already seem to know how to do 2. (load a URL into `WebView`), so focus on 1.

You can learn how to open and read a file in Java from many resources. Including [SO](https://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java). The only Android specific part is accessing the file. Android applications have restricted access to most storage locations. On user (not rooted) devices, apps have access to only few location:

### Application Storage

Each app can access own storage (in `/data/data/{your.package}`). You can open these files directly using [`openFileInput`](https://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)). Or get the path using [`getFilesDir()`](https://developer.android.com/reference/android/content/Context.html#getFilesDir()).

### External Storage

If the file is in `/sdcard` your app will need the `READ_EXTERNAL_STORAGE` permission and you might have to ensure that external storage is mounted. [Learn more here](https://developer.android.com/guide/topics/data/data-storage.html#filesExternal).

### `/data/local/tmp`

This is one location on most Android devices (but **not all**) that all apps can "enter", so if you put the file in there and make sure it's world readable (`chmod o+r /data/local/tmp/myfile.txt`) then you will be able to open it in any app.
I would like to support the approach of @szym. Here you can find some code for your better understanding.

```
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
    }
    br.close();

String url = "";

String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
    Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
    Matcher urlMatcher = pattern.matcher(text.toString());

    while (urlMatcher.find())
    {
        url = text.toString().substring(urlMatcher.start(0),
                urlMatcher.end(0));
        break;
    }

    if(""!=url){
         WebView webview = (WebView) findViewById(R.id.webView1);
        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
    }

}
catch (IOException e) {
    //You'll need to add proper error handling here
}

```