Please help me to do such a thing, let’s say we have a text file, test.txt, about similar to this:
hello hello hello
<link1>http://stackoverflow.com<link1>
The first line of text, and the second link enclosed in the <link1>. I am printing the contents of the file as follows:
if(myName.equals(name)){
InputStreamReader reader = null;
try{
File file = new File("C:\\Users\\ваня\\Desktop\\asksearch\\" + list[i]);
reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
int b;
PrintWriter wr = response.getWriter();
wr.print("<html>");
wr.print("<head>");
wr.print("<title>HelloWorld</title>");
wr.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
wr.print("<body>");
wr.write("<div>");
while((b = reader.read()) != -1) {
wr.write((char) b );
}
wr.write("</div>");
wr.write("<hr>");
wr.print("</body>");
wr.print("</html>");
wr.close();
}
Just a piece of code:
while((b = reader.read()) != -1) {
writer.write((char) b);
}
You want to display, the first line of the file itself, and the second line of the file separately
PrintWriter writer = response.getWriter();
writer.print("<html>");
writer.print("<head>");
writer.print("<title>HelloWorld</title>");
writer.print("<body>");
writer.write("<div>");
// then the first line
writer.write("</div>");
writer.write("<div>");
// then the second line
writer.write("</div>");
writer.print("</body>");
writer.print("</html>");
Create a
BufferedReaderfor your file:Use the
readLinemethod to read a single line( the first line):Hope this helps.