> 文档中心 > Java输入输出处理

Java输入输出处理


Java输入输出处理文章目录


文章目录

  • Java输入输出处理文章目录
  • 一、File类
    • 1.File类的构造方法
    • 2.File类的创建
    • 3.File类判断和获取
    • 4.File类的删除
  • 二、IO流
    • 1.io流的概述和分类
    • 2.字节
      • 1.字节流写数据
      • 2.字节流写数据的三种方式
      • 3.换行和追加
      • 4.字节流读数据(一次只能读一个字节)
      • 5.字节流复制文本文件
      • 6.字节流读数据(一次读一个字节数组)(复制图片操作一样)
      • 7.字节缓冲流
    • 3.字符
      • 1.字符串中编码解码问题
      • 2.字符流中编码解码问题
      • 3.字符流写数据的5种方式
      • 4.字符流读数据的2中方式
      • 5.字符缓冲流
      • 6.字符缓冲流特有功能

一、File类

1.File类的构造方法

在这里插入图片描述
案例:

package com.file;import java.io.File;public class Test1 {    public static void main(String[] args) { File f1=new File("F://Test//java.txt"); System.out.println(f1);  //F:\Test\java.txt File f2=new File("F://Test","java.txt"); System.out.println(f2);  //F:\Test\java.txt File f3=new File("F://Test"); File f4=new File(f3,"java.txt"); System.out.println(f4);  //F:\Test\java.txt    }}

2.File类的创建

在这里插入图片描述
案例:

package com.file;import java.io.File;import java.io.IOException;public class Test2 {    public static void main(String[] args) throws IOException { //在F盘的Test目录下创建一个test.txt文件 File f1=new File("F://Test//test.txt"); System.out.println(f1.createNewFile());  //true //在F盘的Test目录下创建一个Spring目录 File f2=new File("F://Test//Spring"); System.out.println(f2.mkdir());  //true //在F盘的Test目录下创建多个目录 File f3=new File("F://Test//MyBatis//SpringMVC"); System.out.println(f3.mkdirs());   //true    }}

3.File类判断和获取

在这里插入图片描述
案例:

package com.file;import java.io.File;public class Test3 {    public static void main(String[] args) { File f=new File("F:\\Test\\java.txt"); System.out.println(f.isDirectory());  //false System.out.println(f.isFile());  //true System.out.println(f.exists());   //true System.out.println(f.getAbsoluteFile());   //F:\Test\java.txt System.out.println(f.getPath());    //F:\Test\java.txt System.out.println(f.getName());   //java.txt File f1=new File("F:\\Test"); String[] list = f1.list(); for (String s : list) {     System.out.println(s);  /*abjava.txtMyBatisSpringtest.txtTestOne小飞侠小马哥*/ } File[] files = f1.listFiles(); for (File file : files) {     System.out.println(file);/*F:\Test\aF:\Test\bF:\Test\java.txtF:\Test\MyBatisF:\Test\SpringF:\Test\test.txtF:\Test\TestOneF:\Test\小飞侠F:\Test\小马哥      */ }    }}

4.File类的删除

在这里插入图片描述
案例:

package com.file;import java.io.File;import java.io.IOException;public class Test4 {    public static void main(String[] args) throws IOException { //在当前模块下创建one.txt文件 File f1=new File("E:\\JavaSE\\Test\\File\\one.txt");  //true System.out.println(f1.createNewFile()); //删除当前模块下创建one.txt文件 System.out.println(f1.delete());   true //在当前模块下创建one目录 File f2=new File("E:\\JavaSE\\Test\\File\\one"); System.out.println(f2.mkdir());   true //删除当前模块下创建one目录 System.out.println(f2.delete());   true    }}

二、IO流

1.io流的概述和分类

在这里插入图片描述
在这里插入图片描述

2.字节流

1.字节流写数据

在这里插入图片描述
案例:

package com.outPutstream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Test1 {    public static void main(String[] args) throws IOException { //创建字节输出流对象 FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt"); //将字节写入此文件输出流 f.write(97);   //a //释放资源 f.close();    }}

2.字节流写数据的三种方式

在这里插入图片描述
案例:

package com.outPutstream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test2 {    public static void main(String[] args) throws IOException { //创建字节输出流对象 FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt"); //将字节写入此文件输出流/* f.write(97); f.write(98); f.write(99); f.write(100); */  //abcd //将指定的字节数组写入此文件输出流 byte[] bytes={97,98,99,100}; //f.write(bytes); //abcd f.write(bytes,1,2);   //bc  //释放资源 f.close();    }}

3.换行和追加

在这里插入图片描述
案例:

package com.outPutstream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test3 {    public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt",true); for (int i = 0; i <5 ; i++) {     f.write("hello".getBytes());     f.write("\r\n".getBytes()); } f.close();    }}//执行三次的结果/*hellohellohellohellohellohellohellohellohellohellohellohellohellohellohello */

4.字节流读数据(一次只能读一个字节)

在这里插入图片描述
案例:

package com.FileInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Test1 {    public static void main(String[] args) throws IOException { //创建字节输入流对象 FileInputStream f = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt"); //读取数据 /*int by = f.read(); System.out.println(by);  //97 System.out.println((char)by);  //a */ //一次只能读取一个字节 int by = f.read(); while (by != -1){     System.out.print((char) by);  //abc     by=f.read(); } //释放资源 f.close();    }}

5.字节流复制文本文件

案例:

package com.FileInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test2 {    public static void main(String[] args) throws IOException { FileInputStream f1 = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt"); FileOutputStream f2 = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt"); int by = f1.read(); while (by !=-1){     f2.write((char)by);     by=f1.read(); }    }}

6.字节流读数据(一次读一个字节数组)(复制图片操作一样)

案例:

package com.FileInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Test3 {    public static void main(String[] args) throws IOException { FileInputStream f1 = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt"); byte[] bytes=new byte[1024]; int by; by = f1.read(bytes); while (by !=-1){     System.out.println(new String(bytes,0,by));     by=f1.read(bytes); }    }}/*JavaJavaWebPythonSpringMybatis */

7.字节缓冲流

案例:

package com.BufferStream;import java.io.*;public class Test1 {    public static void main(String[] args) throws IOException {BufferedOutputStream b1 = new BufferedOutputStream(new FileOutputStream("E:\\JavaSE\\Test\\File\\buffer.txt")); b1.write("Hello".getBytes()); b1.write("\r\t".getBytes()); b1.write("World".getBytes()); b1.close(); BufferedInputStream b2 = new BufferedInputStream(new FileInputStream("E:\\JavaSE\\Test\\File\\buffer.txt")); int by = b2.read(); while (by !=-1){     System.out.print((char)by);     by=b2.read(); } b2.close();    }}

3.字符流

1.字符串中编码解码问题

在这里插入图片描述

案例:

package com.zifu;import java.io.UnsupportedEncodingException;import java.util.Arrays;public class Test1 {    public static void main(String[] args) throws UnsupportedEncodingException { /*String s1="小马哥"; byte[] bytes = s1.getBytes(); System.out.println(Arrays.toString(bytes));  //[-27, -80, -113, -23, -87, -84, -27, -109, -91] //String s=new String(bytes);  //小马哥 String s=new String(bytes,"UTF-8");  //小马哥 System.out.println(s);*/ String s2="小飞侠"; byte[] bytes =s2.getBytes("GBK"); System.out.println(Arrays.toString(bytes));  //[-48, -95, -73, -55, -49, -64] String s=new String(bytes,"GBK"); System.out.println(s);  //小飞侠    }}

2.字符流中编码解码问题

在这里插入图片描述

案例:

package com.zifu;import java.io.*;public class Test2 {    public static void main(String[] args) throws Exception { OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("E:\\JavaSE\\Test\\File\\java.txt"),"GBK"); o.write("小飞侠"); o.close(); InputStreamReader i = new InputStreamReader(new FileInputStream("E:\\JavaSE\\Test\\File\\java.txt"),"GBK"); int by = i.read(); while (by !=-1){     System.out.print((char)by);  //小飞侠     by=i.read(); }    }}

3.字符流写数据的5种方式

在这里插入图片描述
案例:

package com.zifu;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;public class Test3 {    public static void main(String[] args) throws Exception { OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("E:\\JavaSE\\Test\\File\\java.txt")); //写一个字符 o.write("97");  //97 o.write("a");  //a //刷新流 o.flush(); //写一个字符数组 char[] chars={'a','b','c','d','e'};  //abcde //o.write(chars); //写一个字符数组的一部分 o.write(chars,0,chars.length);  //abcde o.write(chars,1,3);  //bcd //写一个字符串 //o.write("xyz");  //xyz //写一个字符串的一部分o.write("asdfghj",1,5);  //sdfgh o.close();    }}

4.字符流读数据的2中方式

在这里插入图片描述
案例:

package com.zifu;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;public class Test4 {    public static void main(String[] args) throws IOException { InputStreamReader i = new InputStreamReader(new FileInputStream("E:\\JavaSE\\Test\\File\\java.txt")); //一次读一个字符数据 int by = i.read(); while (by !=-1){     System.out.print((char)by);  //aabcdebcdsdfgh     by=i.read(); } //一次读一个字符数组 char[] chars=new char[1024]; int c = i.read(chars); while (c!=-1){     System.out.println(new String(chars,0,c));     c=i.read(chars);/*aabcdebcdsdfgh      */ }     }}

5.字符缓冲流

在这里插入图片描述
案例:

package com.zifu;import java.io.*;public class Test5 {    public static void main(String[] args) throws IOException { /*BufferedWriter w = new BufferedWriter(new FileWriter("E:\\JavaSE\\Test\\File\\java.txt")); w.write("Hello"); w.write("\r\n"); w.write("World");*/ BufferedReader r = new BufferedReader(new FileReader("E:\\JavaSE\\Test\\File\\java.txt")); int by = r.read(); while (by!=-1){     System.out.print((char)by);/*HelloWorld  */     by=r.read(); } //w.close(); r.close();    }}

6.字符缓冲流特有功能

在这里插入图片描述
案例:

package com.zifu;import java.io.*;public class Test6 {    public static void main(String[] args) throws IOException { BufferedWriter w = new BufferedWriter(new FileWriter("E:\\JavaSE\\Test\\File\\java.txt")); for (int i = 0; i <5 ; i++) {     w.write("Java"+i);/*Java0Java1Java2Java3Java4 */     //换行     w.newLine();     w.flush(); } w.close(); BufferedReader r = new BufferedReader(new FileReader("E:\\JavaSE\\Test\\File\\java.txt")); /*String s = r.readLine(); while (s!=null){     System.out.println(s);     s=r.readLine();    }*/ String s; while ((s=r.readLine())!=null){     System.out.println(s); }    }}/*Java0Java1Java2Java3Java4 */