During project development over the past few days, if you want to read the content in a text file, write a method to read the content in the text file. The Code is as follows:
// Read the content of a text file
Public static string readtxtfile (string strfilepath)
{
String Path = strfilepath;
String content = ""; // file content string
// Open the file
File file = new file (PATH );
// If path is a passed parameter, you can make a non-directory judgment.
If (file. isdirectory ())
{
Log. D ("testfile", "the file doesn't not exist .");
}
Else
{
Try {
Inputstream instream = new fileinputstream (File );
If (instream! = NULL)
{
Inputstreamreader inputreader = new inputstreamreader (instream );
Bufferedreader buffreader = new bufferedreader (inputreader );
String line;
// Read by branch
While (line = buffreader. Readline ())! = NULL ){
Content + = line + "\ n ";
}
Instream. Close ();
}
}
Catch (Java. Io. filenotfoundexception E)
{
Log. D ("testfile", "the file doesn't not exist .");
}
Catch (ioexception E)
{
Log. D ("testfile", E. getmessage ());
}
}
Return content;
}