实例:
- 创建一个
FileWriter
对象,并写入一个字符:
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("src\\com\\chao\\a.txt");
fw.write(97);
fw.close();
}
}
a.txt
内容:
a
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("src\\com\\chao\\a.txt");
fw.write(97);
fw.flush();
fw.write(98);
fw.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("src\\com\\chao\\a.txt");
char[] c = {'a', 'b', 'c'};
fw.write(c);
fw.write(c,1,2);
fw.close();
}
}
a.txt
内容:
abcbc
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("src\\com\\chao\\a.txt");
char[] c = "我很强".toCharArray();
fw.write(c);
fw.write(c, 1, 2);
fw.write("我无敌,你随意");
fw.write("我无敌,你随意", 1, 3);
fw.close();
}
}
a.txt
内容:
我很强很强我无敌,你随意无敌,
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("src\\com\\chao\\a.txt", true);
fw.write("时间是真理的孩子");
fw.close();
}
}
a.txt
内容:
我无敌,你随意无敌,时间是真理的孩子