cordova-android/framework/src/com/phonegap/FileUtils.java

125 lines
2.9 KiB
Java
Raw Normal View History

package com.phonegap;
2009-09-12 05:30:24 +08:00
import java.io.*;
2009-10-29 04:43:39 +08:00
import android.webkit.WebView;
2009-09-12 05:30:24 +08:00
public class FileUtils {
2009-10-29 04:43:39 +08:00
WebView mView;
2009-09-12 05:30:24 +08:00
DirectoryManager fileManager;
FileReader f_in;
FileWriter f_out;
2009-10-29 04:43:39 +08:00
FileUtils(WebView view)
{
mView = view;
}
2009-09-12 05:30:24 +08:00
public int testSaveLocationExists(){
if (fileManager.testSaveLocationExists())
return 0;
else
return 1;
}
public long getFreeDiskSpace(){
long freeDiskSpace=fileManager.getFreeDiskSpace();
return freeDiskSpace;
}
public int testFileExists(String file){
if (fileManager.testFileExists(file))
return 0;
else
return 1;
}
public int testDirectoryExists(String file){
if (fileManager.testFileExists(file))
return 0;
else
return 1;
}
/**
* Delete a specific directory.
* Everyting in side the directory would be gone.
* TODO: JavaScript Call backs for success and error handling
*/
public int deleteDirectory (String dir){
if (fileManager.deleteDirectory(dir))
return 0;
else
return 1;
}
/**
* Delete a specific file.
* TODO: JavaScript Call backs for success and error handling
*/
public int deleteFile (String file){
if (fileManager.deleteFile(file))
return 0;
else
return 1;
}
/**
* Create a new directory.
* TODO: JavaScript Call backs for success and error handling
*/
public int createDirectory(String dir){
if (fileManager.createDirectory(dir))
return 0;
else
return 1;
}
public String read(String filename)
{
String data = "";
String output = "";
try {
FileInputStream fstream = new FileInputStream(filename);
DataInputStream in = new DataInputStream(fstream);
while (in.available() !=0)
{
data += in.readLine();
}
2009-10-23 05:25:45 +08:00
2009-09-12 05:30:24 +08:00
} catch (FileNotFoundException e) {
data = "FAIL: File not found";
} catch (IOException e) {
data = "FAIL: IO ERROR";
}
2009-10-29 04:43:39 +08:00
mView.loadUrl("javascript:navigator.FileReader.hasRead('" + data + "')");
2009-09-12 05:30:24 +08:00
return data;
}
2009-11-06 04:57:16 +08:00
public int write(String filename, String data, boolean append)
2009-09-12 05:30:24 +08:00
{
2009-11-14 06:36:51 +08:00
String FilePath= filename;
2009-10-16 04:35:55 +08:00
try {
2009-12-04 09:07:07 +08:00
byte [] rawData = data.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
2009-12-08 06:11:35 +08:00
FileOutputStream out= new FileOutputStream(FilePath, append);
2009-12-04 09:07:07 +08:00
byte buff[] = new byte[rawData.length];
in.read(buff, 0, buff.length);
out.write(buff, 0, rawData.length);
2009-10-16 04:35:55 +08:00
out.flush();
2009-12-04 09:07:07 +08:00
out.close();
mView.loadUrl("javascript:navigator.file.onsuccess('File written')");
2009-10-29 04:43:39 +08:00
} catch (Exception e) {
mView.loadUrl("javascript:navigator.file.onerror('Fail')");
2009-10-29 04:43:39 +08:00
}
2009-10-16 04:35:55 +08:00
return 0;
2009-09-12 05:30:24 +08:00
}
2009-12-04 09:07:07 +08:00
2009-09-12 05:30:24 +08:00
}