In my project, 1) I added a logger using logback. The registrar writes a file during project execution.
2) I want to read this file while it is being written by the registrar and displays it on the html / jsp page.
3) To read the file, I want to send an ajax request from the html / jsp page and display the output as line by line. i.e. read one line and display it on the html page.
Here is what I did -----
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
And my html page
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"> </script>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function funReadFile(){
$.ajax({
type:"GET",
url: '/manageCollector/readFile',
success : function (response){
$("#myResponse3out").html(response);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
}
</script>
</head>
<body onload = "funReadFile()">
<textarea name="textarea" id="myResponse3out" rows="27" cols="70" ></textarea>
<h2>Test</h2>
</body>
</html>
How to read a file line by line and display it?
source
share