2009-11-10 09:45:02 +08:00
|
|
|
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
|
|
|
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(){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.testSaveLocationExists())
|
2009-09-12 05:30:24 +08:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getFreeDiskSpace(){
|
2010-07-09 06:51:18 +08:00
|
|
|
long freeDiskSpace=DirectoryManager.getFreeDiskSpace();
|
2009-09-12 05:30:24 +08:00
|
|
|
return freeDiskSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int testFileExists(String file){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.testFileExists(file))
|
2009-09-12 05:30:24 +08:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int testDirectoryExists(String file){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.testFileExists(file))
|
2009-09-12 05:30:24 +08:00
|
|
|
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){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.deleteDirectory(dir))
|
2009-09-12 05:30:24 +08:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a specific file.
|
|
|
|
* TODO: JavaScript Call backs for success and error handling
|
|
|
|
*/
|
|
|
|
public int deleteFile (String file){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.deleteFile(file))
|
2009-09-12 05:30:24 +08:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new directory.
|
|
|
|
* TODO: JavaScript Call backs for success and error handling
|
|
|
|
*/
|
|
|
|
public int createDirectory(String dir){
|
2010-07-09 06:51:18 +08:00
|
|
|
if (DirectoryManager.createDirectory(dir))
|
2009-09-12 05:30:24 +08:00
|
|
|
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
|
|
|
|
2010-02-26 02:09:58 +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();
|
2010-02-26 02:09:58 +08:00
|
|
|
//mView.loadUrl("javascript:navigator.FileReader.onsuccess('File written')");
|
2009-10-29 04:43:39 +08:00
|
|
|
} catch (Exception e) {
|
2010-02-26 02:09:58 +08:00
|
|
|
//mView.loadUrl("javascript:navigator.FileReader.onerror('Fail')");
|
|
|
|
// So, do we just return -1 at this point!
|
|
|
|
return -1;
|
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
|
|
|
}
|