如何使用ajax请求逐行读取文件,而文件是由其他程序使用java编写的? [英] How to read from file line by line using ajax request while file is written by other program using java?

查看:116
本文介绍了如何使用ajax请求逐行读取文件,而文件是由其他程序使用java编写的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,
1)我已经使用logback添加了logger。记录器在运行项目时写入文件。

In my project, 1) I have added logger using logback. The logger writes in a file while running a project.

2)我想在记录器写入文件时读取该文件,并在html / jsp上显示它的输出页面。

2) I want to read this file while it is being written by logger and display output of it on html/jsp page.

3)要读取文件,我想从html / jsp页面发送ajax请求,并逐行显示输出。即读取一行并在html页面上显示。

3) To read file I want to send an ajax request from html/jsp page and display output as line by line. i.e. read one line and display it on html page.

这是我做的-----

Here is what I did-----

  //To read file 
  try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        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 + "'");                  
         // ex.printStackTrace();
    }

我的html页面是

<!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(){
//alert("ok");

$.ajax({
    type:"GET",
//    contentType: "text/plain; charset=utf-8",
    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 do I Read file line by line and display it??

推荐答案

您可以使用JQuery动画逐行显示常量..

You can play with JQuery animation to display constants line by line..

$("#animate").click(function() {
    $("#content")
        .animate({"height": "100px"}, {"queue": false, "duration": 500})
        .animate({"width": "250px"}, 500);
}); 

或者您可以使用互联网上提供的Jquery日志查看器库..参考这个 link

or you can use Jquery Log Viewer libraries available on internet.. refer this link

这篇关于如何使用ajax请求逐行读取文件,而文件是由其他程序使用java编写的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆