//******** File reading using scanner
/**
* Read given file line by line
* @param m_strFileName File name
* @throws Exception File not exist
* @auther Amit 31/10/2011
*/
void printFile(String m_strFileName) throws Exception {
// Create an instance of File for data file.
File objFile = new File(m_strFileName);
if(objFile.exists() == false)
{
throw new Exception(m_strFileName + "File does not exist");
}
// Reading and displaying file line by line
Scanner objScanner = new Scanner(objFile);
while(objScanner.hasNextLine() == true)
{
System.out.println(objScanner.nextLine());
}
objScanner.close();
}
//********** Using FileInputStream
// Get the object of DataInputStream, passing FileInputStream
DataInputStream in = new DataInputStream(new FileInputStream("anyfile.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the line on the console
System.out.println (strLine);
}
in.close();
Monday, 31 October 2011
Java: File Read / Write
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment