2016-05-26 55 views
0

我会问你一些问题,如何使用java编程将终端输出导出到txt文件。因为我想用webdriver自动测试日志 这是我的代码。如何使用java导出终端ubuntu输出txt文件

public class Test5 
{ 
    private static WebDriver webDriver; 

    public static void main(String[] args) throws AWTException, InterruptedException, JSchException, IOException 
    { 
     String host = "host"; 
     String user = "user"; 
     String password = "password"; 
     String command1 = "ssh [email protected]"; 

     String userPC = System.getProperty("user.name"); 
     String path = "/home/" + userPC + "/Desktop/ScreenShot/Downloads/"; 
     FirefoxProfile profile = new FirefoxProfile(); 
     profile.setPreference("browser.download.folderList", 2); 
     profile.setPreference("browser.download.dir", path); 
     profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
       "application/pdf, text/csv, application/csv, application/octet-stream, text/plain, text/pdf, application/vnd.google-earth.kml+xml"); 
     webDriver = new FirefoxDriver(profile); 

     Link links = Link.trd; 

     webDriver.get("http://app." + links + ".ams:8080/admin/loginservice.do"); 



     Thread.sleep(1500); 
     webDriver.close(); 

     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "No"); 
     //config.put("PreferredAuthentications", "password"); 
     JSch jsch = new JSch(); 
     com.jcraft.jsch.Session session = jsch.getSession(user, host, 22); 
     session.setPassword(password); 
     session.setConfig(config); 
     //session.setConfig("PreferredAuthentications", "password"); 
     session.connect(); 

     Channel channel = session.openChannel("shell"); 
      channel.connect(); 
     Expect expect = new ExpectBuilder() 
       .withOutput(channel.getOutputStream()) 
       .withInputs(channel.getInputStream(), channel.getExtInputStream()) 
       .withEchoOutput(System.out) 
       .withEchoInput(System.err) 
       .build(); 

     expect.sendLine("less /app/logs/jboss/server.log > qwetest103.txt"); 
     Thread.sleep(1000); 


     PrintStream out = new PrintStream(new FileOutputStream("/home/"+userPC+"/Desktop/ScreenShot/Log/text.txt")); 
     System.setOut(out); 

     String format = "jpg"; 


     Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 
     BufferedImage screenFullImage = robot.createScreenCapture(screenRect); 
     ImageIO.write(screenFullImage, format, new File("/home/"+userPC+"/Desktop/ScreenShot/Log/Log"+new Date()+".png")); 


    } 
} 

你能帮我解决我的问题吗?

Regards

回答

0

这很容易。写下面的代码:

PrintStream out = new PrintStream(new FileOutputStream("/urlocation/output.txt")); 
System.setOut(out); 

这会将您的控制台输出保存到名为output.txt的文本文件中。 希望它能帮助你。

一件事,

其中u把这两个线路,将采取控制台日志行后提交。如果你把这两行放在最上面,它会捕获所有的日志到文件。在声明webdriver之前写这两行。

+0

我已经使用过。看看我的代码,在结束部分 –

+0

这不工作? – noor

+0

是的,它不工作..txt文件被创建,但它的空文件 –