user3445644
user3445644

Reputation: 91

How to open a txt file located on the web and read its contents to a string?

I have looked around on how to do this and I keep finding different solutions, none of which has worked fine for me and I don't understand why. Does FileReader only work for local files? I tried a combination of scripts found on the site and it still doesn't quite work, it just throws an exception and leaves me with ERROR for the variable content. Here's the code I've been using unsuccessfully:

public String downloadfile(String link){
    String content = "";
try {
                URL url = new URL(link);
                URLConnection conexion = url.openConnection();
                conexion.connect();
                InputStream is = url.openStream();
                BufferedReader br = new BufferedReader( new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                   sb.append(line);
                } 
                content = sb.toString();
                br.close();
                is.close();
            } catch (Exception e) {
                content = "ERROR";
                Log.e("ERROR DOWNLOADING",
                        "File not Found" + e.getMessage());
            }
return content;
}

Upvotes: 1

Views: 244

Answers (2)

Pratik Roy
Pratik Roy

Reputation: 734

Use this as a downloader(provide a path to save your file(along with the extension) and the exact link of the text file)

    public static void downloader(String fileName, String url) throws IOException {
    File file = new File(fileName);

    url = url.replace(" ", "%20");
    URL website = new URL(url);
    if (file.exists()) {
        file.delete();
    }
    if (!file.exists()) {
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream(fileName);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();

    }
}

Then call this function to read the text file

   public static String[] read(String fileName) {
    String result[] = null;
    Vector v = new Vector(10, 2);
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(fileName));
        String tmp = "";
        while ((tmp = br.readLine()) != null) {
            v.add(tmp);
        }
        Iterator i = v.iterator();
        result = new String[v.toArray().length];
        int count = 0;
        while (i.hasNext()) {
            result[count++] = i.next().toString();
        }

    } catch (IOException ioe) {
        ioe.printStackTrace();
    }


    return (result);
}

And then finally the main method

        public static void main(){
        downloader("D:\\file.txt","http://www.abcd.com/textFile.txt");
        String data[]=read("D:\\file.txt");
         } 

Upvotes: 2

Lavekush
Lavekush

Reputation: 6166

try this:

 try {
    // Create a URL for the desired page
       URL url = new URL("mysite.com/thefile.txt");

     // Read all the text returned by the server
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
     String str;
     StringBuilder sb = new StringBuilder();

     while ((str = in.readLine()) != null) {
      // str is one line of text; readLine() strips the newline character(s)

        sb.append(str );

     }
     in.close();

     String serverTextAsString = sb.toString();

    } catch (MalformedURLException e) {
   } catch (IOException e) {
 }

Upvotes: 0

Related Questions