在子类中获取WriteTo文件的内容

时间:2017-01-11 01:18:05

标签: java

这里我将这段代码缩短以显示最重要的方法:方法void writeToFile(string filename)在我的超类中称为“Words”

.photo {
  background-image: url(https://s30.postimg.org/v67rh5bdd/image.jpg);
  background-attachment: fixed;
  background-position: center top;
  background-repeat: no-repeat;
  background-size: cover;
  width: 100%;
  height: 55vh;
  position: absolute;
}

.photo::before {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;

  background: 
    linear-gradient(
      to bottom, 
      rgba(255,255,255,0) 80%, 
      rgba(255,255,255,0.1) 82%, 
      rgba(255,255,255,0.2) 84%, 
      rgba(255,255,255,0.3) 86%,
      rgba(255,255,255,0.4) 88%, 
      rgba(255,255,255,0.5) 90%, 
      rgba(255,255,255,0.6) 92%,
      rgba(255,255,255,0.7) 94%, 
      rgba(255,255,255,0.8) 96%, 
      rgba(255,255,255,0.9) 98%,
      rgba(255,255,255,1) 100%
    );
}

我的具体子类下面也有这个方法,它创建一个给定名称的文件:

public void writeToFile(String filename) throws IOException {

        FileWriter out = null;
        try {
            File outFile = new File(filename);
            out = new FileWriter(outFile);

            out.write("Writing to a text file");

        }
        catch(IOException e) {
            System.err.println(e.getMessage());
        }
        finally {
            if(out != null) { out.close(); }
        }
    }

如果从子类调用方法writeToFile(“vowels.txt”)来创建新文件,这不会创建文件?如何从我的子类调用writeToFile方法来创建这个vowels.txt文件?

1 个答案:

答案 0 :(得分:0)

我发现解决方案是将writeToFile(" vowels.txt")从我的元音方法移动到另一个地方,例如在我的构造函数中或创建一个新方法并调用此writeToFile(&#34) ; vowels.txt")有效。另外正如Robertos Attias使用System.out.println(System.getProperty(" user.dir"))所建议的那样;显示当前目录。