2013年9月1日 星期日

write_in() & read_in() (folder in folder) (internal)

protected void write_in(String string, String string2, String string3) {
// TODO Auto-generated method stub
// This will get the SD Card directory and create a folder named MyFiles
// in it.
File sdCard = getFilesDir();
File directory = new File(sdCard.getAbsolutePath() + "/" + string);

// Now create the file in the above directory and write the contents
// into it
File file = new File(directory, string2);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
try {
osw.write(string3);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

-------------------------------------------------------------------

private String read_in(String directory, String string) {
// TODO Auto-generated method stub
// Read in. st. from File"fi"
String fileName = string;
int readed;
String content = "";
byte[] buff = new byte[256]; // input stream buffer
// Input stream
try {
File file = new File(getFilesDir() + "/" + directory, string);
FileInputStream reader = new FileInputStream(file);
// FileInputStream reader =
// context.getApplicationContext().openFileInput(fileName);
while ((readed = reader.read(buff)) != -1) {
content += new String(buff).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// End of Read in. st. from File"fi"
return content;
}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...