Facing:方法readLine()未定义String类型

时间:2015-04-29 08:26:43

标签: java jsp java-ee

enter image description here我有一个名为P_Builder.java的java程序。

import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.lang.*;
    import java.text.*;
    import java.util.*;

    public class P_Builder {



        public String PB_Reader( String file_location, String pb_cmd)throws IOException
        {

            File file=new File(file_location);

            ProcessBuilder pb = new ProcessBuilder("cmd","/C",pb_cmd);

            pb.directory(file);

            Process p = pb.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            String readline= reader.toString();
            return readline;
            /*String readline=null;
            do{
            return readline;
            }while((readline = reader.readLine()) != null);*/
        }



    }

现在我在A.JSP中使用这个P_Builder.java倍数,因为我需要多次运行流程构建器。

我希望将P_Builder的行方式输出发送到A.JSP,以便我可以使用readLine()方法读取行方式输出并使用A.JSP打印输出,其中我在输出中省略了几行。

A.JSP

<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.text.DecimalFormat"%>
<%@ page import="license_Dashboard.P_Builder" %>
String pcm= "lmutil.exe lmstat -a -c 426@STIOSP569";
P_Builder p1=new P_Builder();
String readline1=p1.PB_Reader("D:\\SINGULA\\Log", pcm);  
String readline;
int j=0;
while ((readline = readline1.readLine()) != null) {
    if(j==2 || j==3 || j==6)
    {
    //out.println(readline);
    //out.println("<br>");
    }
    else if(j>9)
    {
    out.println(readline);
    out.println("<br>");
    }
    else
    {
    //out.println("<br>");
    }
    j++;
    }

我遇到错误后: - “循环中A.JSPin中的类型字符串”未定义方法readLine()。

更新: - 下午5:23

import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
        import java.lang.*;
        import java.text.*;
        import java.util.*;

        public class P_Builder {



            public String PB_Reader( String file_location, String pb_cmd)throws IOException
            {

                File file=new File(file_location);

                ProcessBuilder pb = new ProcessBuilder("cmd","/C",pb_cmd);

                pb.directory(file);

                Process p = pb.start();

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));


                return reader;
                //String readline= reader.toString();
                //return readline;

                /*String readline=null;
                do{
                return readline;
                }while((readline = reader.readLine()) != null);*/
            }


        }

即使这也给出了错误,因为它需要以String类型返回。如何将返回类型设置为BufferedReader因为我想在bufferedReader读取它们时返回多个值。

之后,任何人都可以告诉我如何捕获JSP中的值并在网页上打印它们。

更新: - 晚上7:00

 public class P_Builder {



            public String PB_Reader( String file_location, String pb_cmd)throws IOException
            {

                File file=new File(file_location);

                ProcessBuilder pb = new ProcessBuilder("cmd","/C",pb_cmd);

                pb.directory(file);

                Process p = pb.start();

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                StringBuffer result=new StringBuffer();
                String readline;

        while((readline = reader.readLine()) != null)
        {


            //this.fin_readline[j]=readline+"\n";
            result.append(readline);
            result.append("\n");


        }
            }
            return result.toString();


        }

现在A.JSP已被修改: -

<%  


 out.println("\n");
    try{
    P_Builder p1=new Test_P_Builder();
    String pcm= "lmutil.exe lmstat -a -c 426@STIOSP569";

    String res=p1.B_Reader("D:\\SINGULA\\Log", pcm);

    String[] lines = res.toString().split("\\n");
    int j=0;
    for(String readline:lines)
    {
        if(j>3)
    {
    out.println(readline);
    }
    j++;
    }
    }
    catch(Exception e)
    {
        out.println(e);
    }

%GT;

现在输出没有对齐。屏幕打印已附加。enter image description here

非常感谢你决定让输出对齐吗?

在给出out.println(readline +“
”)之后输出

;在JSP中

1 个答案:

答案 0 :(得分:3)

您的PB_Reader方法需要返回BufferedReader而不是String

String readline= reader.toString();这是您出于某种原因决定获取读者的字符串表示的地方。不要那样做。