2016-05-26 15 views
0

javaプログラミングを使用して端末の出力をtxtファイルにエクスポートする方法をお尋ねします。私はwebdriver を使って自動的にログをテストしたいので、これは私のコードです。Javaを使用してターミナルの出力をエクスポートする方法

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")); 


    } 
} 

私の問題を解決するのに役立つことができますか?

よろしくお願いします。

答えて

0

これはかなり簡単です。以下のコードを書いてください:

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

これは、出力ファイル名をoutput.txtに変換します。 それがあなたを助けることを願っています。

もう一つ、uはこれら二つのラインを入れて、それがその行の後にファイルにコンソールログを取る

。これらの2行を一番上に置くと、そこからすべてのログがファイルに取り込まれます。 webdriverを宣言する前にこれらの2行を記述してください。

+0

私はそれを使用しました。終わりの部分で私のコードを見てください –

+0

これは動作しませんか? – noor

+0

はい、それは動作していません.. txtファイルが作成されましたが、その空のファイル –

関連する問題