Wednesday, 11 September 2013

How to read data from a file stored in web server by java eclipse program

How to read data from a file stored in web server by java eclipse program

I have stored a text file in google docs and that is a shared public file.
I am trying to read the content of that file by the url. but it is not
showing the contents. I am attaching my code here. Please suggest me where
should I change my code to read the content of the file.the address I have
given in URL is showing content when I am writing it in the address bar of
any web browser. I actually do not know where to correct the code. The
output looks like :
e&a=v&chrome=true&redirect=false","isNativeGView":false},"webstoreui":{"mimeType":"text/plain","fileExtension":"txt","moreDriveAppsUrl":"https://chrome.google.com/webstore/category/collection/drive_apps"}});
package FileContent;
import java.awt.*;
import java.net.*;
import java.io.*;
public class FileContent extends Frame {
public static void main(String[] args) {
BufferedReader br = null;
TextArea FileText = new TextArea(" Content of the File
\'temp1.txt\' :", 11, 24, TextArea.SCROLLBARS_NONE);
try {
// here is the url
URL url = new
URL("https://docs.google.com/file/d/0B9MOgXFCss2iSWpicmVKSW9OOWM/edit");
InputStream is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (MalformedURLException e) {
System.out.println("Bad URL");
} catch (IOException e) {
System.out.println("IO Error : " + e.getMessage());
}
FileText.setBackground(Color.cyan);
FileText.append(String.valueOf('\n'));
Frame f = new Frame("File Content");
f.setSize(200, 200);
f.add(FileText);
f.setVisible(true);
try {
String s;
boolean eof = false;
s = br.readLine();
while (!eof) {
FileText.append(s + String.valueOf('\n'));
try {
s = br.readLine();
if (s == null) {
eof = true;
br.close();
}
} catch (EOFException eo) {
eof = true;
} catch (IOException e) {
System.out.println("IO Error : " + e.getMessage());
}
}
} catch (IOException e) {
System.out.println("IO Error : " + e.getMessage());
}
}
}

No comments:

Post a Comment