import java.io.*;
public class FileApi extends File{
FileWriter fw;
FileReader fr;
PrintWriter pr;
class public FileApi (String s) throws Exception
{
super(s);
pw = new PrintWriter(fw = new FileWriter(s, true));
}
public void createFile() throws IOException
{
FileApi fa = new FileApi.createFile();
pw.println(fa);
}
public void createFile(String s) throws IOException
{
File fail = new File(s);
pw = new PrintWriter(fail);
}
public void WriteFile(String s) throws IOException
{
pw.println(s);
pw.flush();
}
public String readFile() throws Exception
{
super(s);
fr = new FileReader(new File(s));
String str="";
int st;
while ((st = fr.read())!= -1) {
char ch=(char) st;
str=str+ch;
}
return str;
}
public void close() throws IOException
{
pw.close();
fr.close();
fw.close();
}
public static void main(String [] arg) throws Exception
{
FileApi fileapi = new FileApi("greeting.txt");
if(fileapi.exists())
{
System.out.println("File exists!");
fileapi.writeFile("Putrajaya");
}
System.out.println(fileapi.readFile());
fileapi.close();
}
輸出:
FileApi.java:11:錯誤:預期 類公共FileApi(String s)將拋出異常 FileApi.java:11:錯誤:無效方法聲明;返回類型需要 類公共FileApi(String s)將拋出異常 FileApi.java:68:錯誤:文件的最終達成而解析}
作爲除了下面的答案,將構造函數中的變量名稱pw更改爲pr – user
我可以感受到編譯器並且無法解析代碼。 'super(s)'應該只在你的構造函數中被調用,而不是在方法中('readFile()')'createFile()'是另一個失敗,... –