package com.test.io01; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test03 { public static void main(String[] args) throws IOException { //程序--->文件 //1.有个目标文件:--->创建一个File类的对象 File f = new File("d:\\demo.txt"); //2.利用FileWriter这个管怼到文件上去 //FileWriter fw = new FileWriter(f);//这个默认就是false。 //FileWriter fw = new FileWriter(f,false); FileWriter fw = new FileWriter(f,true);//后面追加字符 //3.开始动作:输出动作 //一个字符一个字符的往外输出 /*String str = "hello"; for (int i=0;i<str.length();i++) { //System.out.print(str.charAt(i));//hello你好 //一个字符一个字符的向外输出 //public char charAt(int index)返回char指定索引处的值。 //指数范围为0至length() - 1 。 该序列的第一个char值在索引0 ,下一个索引为1 ,依此类推,与数组索引一样。 fw.write(str.charAt(i)); }*/ String str = "你好"; //public char[] toCharArray()将此字符串转换为新的字符数组。 char[] chars = str.toCharArray(); fw.write(chars); //4.关闭流 //流,数据库,网络资源,靠jvm本身没有办法帮我们关闭,此时必须程序员手动关闭 fw.close(); } }
文章来源地址https://uudwc.com/A/Eg1a
文章来源:https://uudwc.com/A/Eg1a