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 {                 publicstatic 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                 */                publicstatic ArrayList<String> read(String file) throws IOException {                               ArrayList<String>lst = new ArrayList<String>();                                Stringline = null;                                BufferedReaderbr = new BufferedReader(new InputStreamReader(                                                               newFileInputStream(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();                                returnlst;                }                 /**                 * clear spaces and tabs                 *                 * @param row                 * @return                 */                publicstatic String clearRow(String row) {                               Stringres = row.replaceAll(" ", "");// clear all spaces                               res= res.replaceAll("     ","");// clear all tabs                               returnres;                } }

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!
LATEST TUTORIALS
APPROVED BY CLIENTS