繁体   English   中英

尝试使用Selenium Webdriver将文本复制到文件时编译错误的解决方案

[英]Solution to compilation error on trying to copy text into a file using Selenium Webdriver

我是Selenium webdrivers的初学者。 我收到以下代码的编译错误。任何人都可以协助吗?

我正在尝试将消息复制到文件中,而不是在控制台上显示它。

testResultFile="C:\\CopyMessageTest.txt";
File file = new file(testResultFile).canWrite();
FileOutputStream fis = new FileOutputStream(file); 
PrintStream out = new PrintStream(fis);
System.setOut(out);
System.out.println("----------Sucessfully Logged In ----------------");

错误就行了

File file = new file(testResultFile).canWrite();

新文件(testResultFile).canWrite(); 将返回布尔值以检查我们是否可写。 你必须以这种方式来检查它

boolean  bool = new file(testResultFile).canWrite();

要写入文件,您可以使用

   FileOutputStream fop = null;
    File file;
    String content = "This is the text content";

    try {

        file = new File("c:/newfile.txt");
        fop = new FileOutputStream(file);

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        fop.write(contentInBytes);
        fop.flush();
        fop.close();

        System.out.println("Done");

    } catch (Exception e) {
        e.printStackTrace();
    }

根据评论以其他方式更新

   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.PrintStream;

 public class PrintTest {

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub



    File file=new File("C:\\CopyMessageTest.txt"); //my file where i want to write

    boolean bool = file.canWrite(); //cross checking file can be writable or not

    if(bool==false){

        file.setWritable(true); //if not writable then set it to writable
    }

    FileOutputStream fis = new FileOutputStream(file); 
    PrintStream out = new PrintStream(fis);
    System.setOut(out);
    System.out.println("----------Sucessfully Logged In ----------------");



    }

 }

谢谢你,穆拉利

您在代码中完成的错误很少。 在第一行中,您没有提到它是什么类型的数据,它应该是一个String。 在第二行中,您创建的对象应该是File类型,您提到了文件,同样,canWrite()方法的返回类型是一个布尔值,如果您仍想使用该提及,请在下一个中明确定义它线。 请参阅下面的更正代码。

    String testResultFile="C:\\CopyMessageTest.txt";
    File file = new File(testResultFile);
    file.canWrite();
    FileOutputStream fis = new FileOutputStream(file); 
    PrintStream out = new PrintStream(fis);
    System.setOut(out);
    System.out.println("----------Sucessfully Logged In ----------------");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM