diff --git a/boot-service/src/main/java/com/xwbing/demo/IODemo.java b/boot-service/src/main/java/com/xwbing/demo/IODemo.java index b438e6c..029dd8c 100644 --- a/boot-service/src/main/java/com/xwbing/demo/IODemo.java +++ b/boot-service/src/main/java/com/xwbing/demo/IODemo.java @@ -16,17 +16,15 @@ public class IODemo { */ /** - * 写 + * 字节流 */ - FileOutputStream fos = new FileOutputStream("fos.txt", true);//追加写模式 - fos.write("你好".getBytes("utf-8")); + /* 写 */ + FileOutputStream fos = new FileOutputStream("io.txt", true);//追加写模式 + fos.write("字节流".getBytes("utf-8")); System.out.println("写出完毕"); fos.close();//关闭流 - - /** - * 读 - */ - FileInputStream fis = new FileInputStream("fos.txt"); + /* 读 */ + FileInputStream fis = new FileInputStream("io.txt"); byte[] data = new byte[fis.available()]; int len = fis.read(data);//数据读入data里 String str = new String(data, 0, len, "utf-8"); @@ -36,19 +34,18 @@ public class IODemo { byte[] da = new byte[is.available()]; is.read(da); is.close(); - /** - * 缓冲流 - * - */ - BufferedOutputStream bos = new BufferedOutputStream(fos); - bos.write("你好".getBytes()); - bos.close();//close前会自动flush,关流只需管最外层高级流 + /** - * 使用缓冲流的形式复制文件 + * 缓冲流 */ - BufferedInputStream bis1 = new BufferedInputStream(fis); - BufferedOutputStream bos1 = new BufferedOutputStream(fos); + /* 写 */ + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt", true)); + bos.write("缓冲流".getBytes()); + bos.close();//close前会自动flush,关流只需管最外层高级流 + /* 使用缓冲流的形式复制文件 */ + BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("io.txt")); + BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream("io.txt", true)); int d; while ((d = bis1.read()) != -1) { bos1.write(d); @@ -56,18 +53,18 @@ public class IODemo { bis1.close(); bos1.close(); - /* + /** * 字符流 * 读写单位是字符 * 可以按照指定的字符集,将写出的字符转换为对应的字节 * 字符流只适合读写文本数据 * 转码:uft-8转gbk */ - InputStreamReader isr = new InputStreamReader(fis, "utf-8"); - OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk"); - int leng; - while ((leng = isr.read()) != -1) { - osw.write(leng); + InputStreamReader isr = new InputStreamReader(new FileInputStream("io.txt"), "utf-8"); + OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("io.tex"), "gbk"); + int length; + while ((length = isr.read()) != -1) { + osw.write(length); } osw.close(); isr.close(); @@ -75,7 +72,7 @@ public class IODemo { /** * 缓冲字符输入流,特点:读取速度快,并且可以按行读取字符串 */ - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fis.txt"))); + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("io.txt"))); String line = null; //readerLIne():连续读取若干字符,知道遇到换行符为止,该字符串中不包含换行符 while ((line = br.readLine()) != null) { @@ -86,11 +83,10 @@ public class IODemo { /** * 缓冲字符输出流 */ - PrintWriter pw = new PrintWriter("e.txt", "utf-8");//PrintWriter可以直接创建基于文件进行写操作 + PrintWriter pw = new PrintWriter("io.txt", "utf-8");//PrintWriter可以直接创建基于文件进行写操作 + pw.close(); - FileOutputStream fos1 = new FileOutputStream("pw1.txt"); - OutputStreamWriter osw1 = new OutputStreamWriter(fos, "utf-8");//可以指定字符 - PrintWriter pw1 = new PrintWriter(osw, true);//当第一个参数为流,可以使用第二个参数来指定是否自动flush + PrintWriter pw1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("io.txt"),"utf-8"), true);//当第一个参数为流,可以使用第二个参数来指定是否自动flush pw1.println("......"); pw1.close(); }