How to write a text to file with GWT on Client-side?
Is there any way to implement write/read file with gwt on client-side? I tried with java.io.File, java.io.Writer ... I couldn't succeed.
thx in a开发者_如何学JAVAdvance!
Update: Please see my own answer for a solution
No, you can't write to files on the client-side. GWT only binds a subset of the Java language. Any file IO would need to happen on the server side through RPCs or some kind of web service.
It's possible with HTML5 in some modern browsers. Try lib-gwt-file. This library can read files from client computer and even supports DND. To see it in action follow this link. More information on HTML5 FileAPI you can find in the specification.
To download a file from browser memory to the client computer you can use Data URI. Example is here. But this feature is supported by Google Chrome only. Also take a look at the following helpful function. It runs download without reloading current page:
public static native void setWindowHref(String url)/*-{
$wnd.location.href = url;
}-*/;
Another semi-crossbrowser way is Downloadify. It's based on flash. Check this example.
Recently, I've stumbled upon a library called client-io.
A simple library that brings the Flash File API to regular web apps through GWT. ClientIO will help you offload some of the file generation functionalities to the client, saving resources and heavy computation to the server. Working Demo - http://ahome-it.github.io/ahome-client-io/
In GWT the classes in the client folder are only compiled into javascript hence it is not possible to use
java.io
since GWT does not provide compilation of the package
java.io
Hence you have to write text file through RPC only.
精彩评论