2
appendメソッドを持つ暗黙的な追加クラスを作成しようとしています。このメソッドは、ファイルの最後にStringを追加する必要があります。暗黙の追加メソッドが正しく動作しません。
def AppendToFile(file: File, str: String): Unit = {
val append = new FileWriter(file)
try
append.append(str).append("\n")
finally
append.close
}
implicit class RichAppend(p: Path) {
def append(fileName: String) = AppendToFile(p.toFile,fileName)
}
ただし、テストを実行するとエラーが発生します。
"[Second line
[info] ]
[info] " did not equal "[line_1
[info] Second line]
これは、私が実行しているテストです:
test("Paths should have a .append method") {
val pth = Paths.get("test.txt")
val data = " line_1\nSecond"
try {
pth.append(pth,"line_1\n")
pth.append(pth,"Second line\n")
assert(new String(Files.readAllBytes(pth)) == "line_1\nSecond line\n")
}
finally {
Files.deleteIfExists(pth)
}
}
あなたの 'append'メソッドは2つのパラメータをとり、あなたはそれを1つだけ渡しています。 – nattyddubbs
メソッドを変更しました。しかし、今私は別のエラーを得る – rocketman
何が違うのですか? –