目录
(1)一次只写一个字符:
- public class FileWriterDemo_01 {
- public static void main(String[] args) throws IOException {
- //创建一个字符输出流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- fw.write(97);
- fw.write(98);
- fw.write(99);
- //关流
- fw.close();
- }
(2)一次写一个字符数组:
- public static void main(String[] args) throws IOException {
- //创建一个字符输出流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- char [] chars = {97,98,99,100};
- fw.write(chars);
- //关流
- fw.close();
- }
(3)一次写一个字符数组的一部分:
- public static void main(String[] args) throws IOException {
- //创建一个字符输出流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- char [] chars = {97,98,99,100,101,102,103,104,105};
- //从0索引开始,一次写三个
- fw.write(chars,0,3);
- //关流
- fw.close();
- }
(4)写一个字符串:
- public static void main(String[] args) throws IOException {
- //创建一个字符输出流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- String line ="我爱中国!";
- fw.write(line);
- //关流
- fw.close();
- }
(5)写字符串的一部分:
- public static void main(String[] args) throws IOException {
- //创建一个字符输出流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- String line ="我爱中国!";
- fw.write(line,0,2);
- //关流
- fw.close();
- }
(1)字符流一次读取一个数据:
- public static void main(String[] args) throws IOException {
- FileReader fr = new FileReader("myFileWriter\\b.txt");
- int ch;
- while ((ch=fr.read())!=-1){
- System.out.println((char) ch);
- }
- fr.close();
- }
(2)字符流一次读取多个字符:
- public static void main(String[] args) throws IOException {
- FileReader fr = new FileReader("myFileWriter\\b.txt");
- //定义一个字符数组,长度为1024
- char[] chars = new char[1024];
-
- int len;
- while ((len = fr.read(chars)) != -1) {//把获取内容后的字符数组传入进去
- System.out.println(new String(chars, 0, len));//把获取的内容转化为字符串进行展示;
- }
- fr.close();
- }
1.如果文件不存在,则自动创建文件,但要保证其父级文件路径存在
(如:myFileWriter\\a.txt,要创建a.txt文件,就要保证父级文件路径myFileWriter的存在)
2.如果文件已经存在,则自动清空其中的内容
3.如果写入的是int型的整数,则输出的是码表的对应字母;写入的是String型,则输出的也是String型
- public class FileWriterDemo_06 {
- public static void main(String[] args) throws IOException {
- //创建一个字符输入流对象
- //FileWriter fw= new FileWriter(new File("myFileWriter"));//方法一
- FileWriter fw = new FileWriter("myFileWriter\\b.txt");//方法二
-
- //写出数据
- String line = "我爱中国!";
- fw.write(line);
-
- // fw.flush(); //刷新流
- fw.write("666");
- // fw.flush();
-
- fw.close(); //关闭流
-
- }
(1)字符缓冲输入流:
- public static void main(String[] args) throws IOException {
- //创建一个字符缓冲输入流,里面传入相应的字符输入流
- BufferedReader br = new BufferedReader(new FileReader("myFileWriter\\c.txt"));
-
- //读取数据
- char [] chars = new char[1024];
- int len;
- while ((len=br.read(chars))!=-1){
- System.out.println(new String(chars,0,len));
- }
-
- //关闭流
- br.close();
-
- }
(2)字符缓冲输出流:
- public static void main(String[] args) throws IOException {
- //创建一个字符缓冲输出流,里面传入相应的字符输出流
- BufferedWriter bw = new BufferedWriter(new FileWriter("myFileWriter\\c.txt"));
- //写数据
- bw.write(97);//写一个数据
- bw.write("\r\n");
-
- char [] chars ={97,98,99,100};//写一个数组
- bw.write(chars);
- bw.write("\r\n");
-
- bw.write("哈哈");//写一个字符串
- bw.write("\r\n");
-
- String line ="abcdefg";//写一个字符串的部分
- bw.write(line,0,5);
- bw.write("\r\n");
-
- //刷新流
- bw.flush();
- //关闭流
- bw.close();
- }