tags:

views:

475

answers:

2

Hello all,

I am writing a webpage using GWT. Now I need to read a text file and display the content in the webpage but have no idea how to do that with GWT.

It is very nice if there is any way in GWT that I can read .properties file. (Please note that this is not the properties file of localization that GWT has already supported )

Does anyone have an idea, please?

Thanks.

+1  A: 

You can read files in your GWT app using RequestBuilder

new RequestBuilder(Method.GET, "path/to/file.txt").sendRequest("", new RequestCallback() {
  @Override
  public void onResponseReceived(Request req, Response resp) {
    String text = resp.getText();
    // do stuff with the text
  }

  @Override
  public void onError(Request res, Throwable throwable) {
    // handle errors
  }
});
Jason Hall
The readability of the file is only restricted by the configuration of the server it's stored on (`.htaccess`, etc) - as long as the file is publicly available you can get it via `RequestBuilder`. If you don't want to make your files public to everyone, you can write a servlet (or similar service) that accesses the files stored on the server (the files then aren't publicly available - only to, for example, registered users). You can configure the server in many ways - serve files only to localhost, read the files from some non-www folder, etc.
Igor Klimer
Good point, edited my answer to reflect that.
Jason Hall
Thanks all for your answers. However, I got problem as follows: I did exactly the code above. I have one folder which contains 3 file: abc.data, abc.file, net.properties. I wanted to read the context of the net.properties file, but the above code always return the context of the abc.file. And if this file does not exist, an exception was thrown, even if I specify the path as http://127.0.0.1:8080/path/net.properties. Any idea, please?
ipkiss
Is it possible to reach the net.properties file in a browser? If not, then your GWT code will also be unable to reach it. It may depend on your hosting configuration which files are accessible to a browser, and thus to your GWT app.
Jason Hall
Yes, I know how to read properties file but in the case we already know that that properties file existing in a location. In my case, the properties file is generated and can only know when we run the web page, it does not exist at the compiling time.Is there any way to read properties file from a url?Thanks all.
ipkiss
Perhaps I'm misunderstanding your problem. Do you not know the URL of the properties file? As long as the file exists and is accessible at runtime, I can't imagine there's any reason GWT can't open it and read it.
Jason Hall
sorry to make thing vague, my problem is that I can only know the path of the properties file at runtime. That's the reason why I cannot read properties file normally in GWT, because I only know how to read properties file (like localization file) at compile time.
ipkiss
That's going to make things very difficult. You may be able to write some servlet with a constant URL that reads the contents of your dynamically-named properties file and prints it out? That way your GWT code will have a constant URL to look to. Other than that I'm not sure if it will be possible.
Jason Hall
A servlet (or a PHP file, etc) should do the trick - pass it the path to the properties file and some sort of token as a proof you are an authenticated user - either way, make sure you put a lot of thought and safeguards into this servlet, since a malicious user could use it to read some security related files from your server (if not guarded properly :)).
Igor Klimer
Yes, you all are right, I wrote a servlet page and it worked like what I was expecting. Thanks all for you effort.
ipkiss
A: 

GWT is Javascript !!!
I love GWT it really helps me build Web apps since I am better at Swing than Web stuff.
Javascript runs in a sandbox and each browser has different ways of getting around these limitations.
The only way you can read and write to and from files is to use platform specific ways of doing so.

If you are targeting a specific browser make a JSNI (Java Script Native Interface) call in GWT and then use something like this.

The other way you can do this is to send a request from GWT to the server, who in return will access the file on the server. (Note: If you are using Google App Engine you can read files but not write them) this way of doing things has already been answered below.

Romain Hippeau