Answer to Question #20049 in Java | JSP | JSF for nohra

Question #20049
An HTML file starts with and ends with . How to write a short java program that reads a file and checks whether that is true.
1
Expert's answer
2012-12-06T09:42:51-0500
import java.io.BufferedReader;


import java.io.FileInputStream;


import java.io.IOException;


import java.io.InputStreamReader;


import java.util.ArrayList;





public class main {





public
static void main(String[] args) throws IOException {


ArrayList<String>
lst = read("1.html");// read to arraylist





if
(lst.get(0).toLowerCase().startsWith("<html>")


&&
lst.get(lst.size() - 1).toLowerCase().endsWith("</html>"))//
checking HTML for starts with <HTML> and ends with </HTML>


{


System.out.println(true);//
An HTML file starts with <HTML> and ends with </HTML> (true)


}
else {


System.out.println(false);


}


}





/**


* read file


*


* @param file


* @return


* @throws IOException


*/


public
static ArrayList<String> read(String file) throws IOException {


ArrayList<String>
lst = new ArrayList<String>();





String
line = null;





BufferedReader
br = new BufferedReader(new InputStreamReader(


new
FileInputStream(file)));





lst
= new ArrayList<String>();





try
{


while
((line = br.readLine()) != null) {


if
(!(line = clearRow(line)).equals("")) {


lst.add(line);


}


}


}
catch (IOException e) {


e.printStackTrace();


}





br.close();





return
lst;


}





/**


* clear spaces and tabs


*


* @param row


* @return


*/


public
static String clearRow(String row) {


String
res = row.replaceAll(" ", "");// clear all spaces


res
= res.replaceAll(" ",
"");// clear all tabs


return
res;


}





}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS