我用2種方法創建了一個類,它們應該處理寫入文件或讀取文件。Java:讀取/寫入文件等
伊夫想出了這樣的事情:
package YBot;
import java.io.*;
public class FollowerChecker {
public static StringBuilder sb;
static String readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
public static void Writer() {
FileWriter fw = null;
try {
fw = new FileWriter("donottouch.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringWriter sw = new StringWriter();
sw.write(TwitchStatus.totalfollows);
try {
fw.write(sw.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
現在我的問題是:
我如何添加創建「donottouch.txt」文件中的函數,如果犯規存在,它已經或者它的空寫入「0」呢?當我的程序啓動時,它會讀取一個數字的文件,稍後,如果數字發生改變,它會重寫它。所以它最好是隻要它試圖閱讀,而它不在那裏,它就會立刻創建並重讀它。希望some1可以給我任何的例子=)
你可以閱讀http://docs.oracle.com/javase/tutorial/essential/io/ –