Package com. RDPs. Dev. fliu. server;
Import java. Io. bufferedwriter;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. filewriter;
Import java. Io. ioexception;
/**
* File test class.
* @ Author Frank
* @ Since 11/18/2010
**/
Public class filetest {
Public static void main (string [] ARGs ){
String content = filetest. readfile ("/home/Frank/desktop/testfile ");
System. Out. println (content );
Filetest. writefile ("Write test data into file.", "/home/Frank/desktop", "testfile ");
}
/**
* Write the content to a file.
**/
Public static Boolean writefile (string content, string path, string filename ){
File file = new file (PATH );
System. Out. println ("the file's location is:" + path + "/" + filename );
Try {
Bufferedwriter BW = new bufferedwriter (New filewriter (File + "/" + filename, true ));
Bw. Write (content );
Bw. Write ("");
Bw. Flush ();
Bw. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return true;
}
/**
* Read content from a file.
**/
Public static string readfile (string path ){
Stringbuilder content = new stringbuilder ("");
File file = new file (PATH );
Try {
Fileinputstream FCM = new fileinputstream (File );
Byte [] bytearray = new byte [FCM. Available ()];
Int bytetotal = Fi. Available ();
While (FCM. Read (bytearray, 0, bytetotal )! =-1 ){
String temp = new string (bytearray, 0, bytetotal );
Content. append (temp );
}
} Catch (filenotfoundexception e ){
System. Out. println ("Frank's file not exits ....");
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return content. tostring ();
}
}