JavaIO流理解
JavaIO流理解
文件
什么是文件?
文件是我们保存数据的地方。
文件流
文件在程序中是以流的形式来操作的。
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
常用的文件操作
1、创建文件对象相关构造器和方法
new File(String pathname) //根据路径构建一个 File 对象
new File(File parent,String child) //根据父目录文件+子路径构建
new File(String parent,String child) //根据父目录+子路径构建
createNewFile() //创建文件
点击查看代码
import org.junit.Test; import java.io.File; import java.io.IOException; public class FileCreate { public void main(String[] args) { // create2(); } @Test public void create2(){ File parentfile=new File("D:\\"); String filename ="text2.txt"; File file=new File(parentfile, filename); try{ file.createNewFile(); System.out.println("文本2创建成功"); }catch (IOException e){ throw new RuntimeException(e); } } @Test public void creat1(){ File file=new File("D:\\file1.txt"); try { file.createNewFile(); System.out.println("文本1成功"); } catch (IOException e) { e.printStackTrace(); } } @Test public void creat3(){ File file=new File("D:\\","text3.txt"); try { file.createNewFile(); System.out.println("文本3成功"); } catch (IOException e) { e.printStackTrace(); } } }
2、获取文件的相关信息
常见的 File 相关方法
getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory
获取文件名、获取绝对路径
UTF-8 一个英文一个字节,一个汉字三个字节
点击查看代码
import org.junit.jupiter.api.Test; //import org.testng.annotations.Test; import java.io.File; public class FileShow { public static void main(String[] args) { } //获取文件信息 @Test public void Info(){ //先创建文件对象 File file = new File("D:\\file1.txt"); //调用相应方法,得到对应信息 System.out.println("文件名称:"+file.getName()); System.out.println("文件绝对路径:"+file.getAbsolutePath()); System.out.println("文件父目录:"+file.getParent()); System.out.println("文件大小(字节):"+file.length()); System.out.println("文件是否存在:"+file.exists()); System.out.println("是否是文件:"+file.isFile()); System.out.println("是否是目录:"+file.isDirectory()); } }
3、目录的操作
创建一级目录:mkdir,创建多级目录:mkdirs ,delete 删除空目录或者文件
点击查看代码
import org.junit.Test; import java.io.*; public class fileDirctory { public static void main(String[] args) { } @Test //删除文件 public void fileDelete(){ String filePath = "D:\\file1.txt"; File file = new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println(filePath+"删除成功"); }else { System.out.println(filePath+"删除失败"); }; }else{ System.out.println("文件不存在"); } } //删除目录 @Test public void fileDeleteD(){ String filePath = "D:\\file1.txt"; File file = new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println(filePath+"删除成功"); }else { System.out.println(filePath+"删除失败"); }; }else{ System.out.println("目录不存在"); } } //判断目录是否存在,不存在就创建 @Test public void fileDeleteD1(){ String dirPath = "D:\\test\\dir1.txt"; File file = new File(dirPath); if(file.exists()){ System.out.println(dirPath+"该目录已经存在"); }else{ if(file.mkdirs()){ System.out.println("创建成功"); }else { System.out.println("创建失败"); }; } } //InputStream //OutputStream //Writer //Reader }
4、IO 流原理及流的分类
1、I=input,O=output,用于处理数据传输,读写文件、网络通信
2、Java 程序中,输入输出以流的形式进行
3、java.io 包下
流的分类:
按操作数据单位不同可以分为:字节流(8bit)、字符流(字符,对应字节按照编码确定)
效率:字符流更好,字节流的存在是为了更好的处理二进制文件,音视频
按流向不同可以分为:输入流、输出流
按流的角色不同分为:节点流、处理流
5、Scanner 与 Println
5.1基本键盘输入
我们可以通过Scanner类来获取用户的输入
基本语法:Scanner scanner = new Scanner(System.in);
其主要方法如下所示
点击查看代码
import java.util.Scanner; public class scanPrintTest { public static void main(String[] args) { //创建 Scanner 对象,接受从控制台输入 Scanner input = new Scanner(System.in); //接受 String 类型 String str = input.next(); //输出结果 System.out.println(str); System.out.println("hello world"); } }
5.2常见键盘输入类型
点击查看代码
``` import java.util.Scanner; public class scanTest { public static void main(String[] args) { Scanner input =new Scanner(System.in); //double 类型的数据 System.out.print("请输入一个 double 类型的数:"); double d = input.nextDouble(); System.out.println(d); //int 类型的数据 System.out.print("请输入一个 int 类型的数:"); int i = input.nextInt(); System.out.println(i); //字符串类型的数据 System.out.print("请输入一个 string 类型的数:"); String s = input.next(); System.out.println(s); } } ```6、InputStream
常见的子类
FileInputStream:文件输入流
BufferedStream:缓冲字节输入流
ObjectedStream:对象字节输入流
类图如下
点击查看代码
import org.junit.Test; import java.io.FileInputStream; import java.io.IOException; public class fileInputStream { public static void main(String[] args) { } @Test public void fileIn1(){ String filePath = "D:\\testfile.txt"; int readData; FileInputStream fileInputStream = null; try{ //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read()) != -1){ System.out.println((char)readData); }; }catch (IOException e){ e.printStackTrace(); }finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn2(){ String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字节数组 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try{ //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1){ System.out.println(new String(buf,0,readlength)); }; }catch (IOException e){ e.printStackTrace(); }finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn3(){ String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字节数组 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try{ //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1){ System.out.println(new String(buf,0,readlength)); }; }catch (IOException e){ e.printStackTrace(); }finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
7、OutputStream
1、FileOutputStream
例子:使用 FileOutputStream 在文件夹中写入“hello world”,如果文件不存在,会创建文件
void close()
关闭此输出流并释放与此流有关的所有系统资源。
void flush()
刷新此输出流并强制写出所有缓冲的输出字节。
void write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流。
void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
abstract void write(int b)
将指定的字节写入此输出流。
类图如下
点击查看代码
import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; public class outputStream { public static void main(String[] args) { } //使用 FileOutputStream 将数据写到文件中 //如果文件不存在,则创建文件 @Test public void writeFile(){ String filePath = "d:\\fileout.txt"; //创建对象 FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath); //1、写入一个字节 //fileOutputStream.write('H'); //2、写入一个字符串 //String str = "hello socket"; //str.getBytes()将字符串转换成字节数组 //fileOutputStream.write(str.getBytes()); //3、写入字符串 String str = "hello lst"; fileOutputStream.write(str.getBytes(),0,str.length()); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
8、文件复制
点击查看代码
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class copyPic { //file 读和写实现复制文件 public static void main(String[] args) throws Exception { //创建 file 对象 File f=new File("d:\\test.jpg"); //判断文件是否存在 if(f.exists()){ System.out.println("test.jpg 存在,可以复制"); }else{ f.createNewFile(); System.out.println("test.jpg 不存在,新建成功,可以复制"); } //创建 FileInputStream 对象 FileInputStream inp=new FileInputStream(f); //创建 FileOutputStream 对象 //判断 demo 目录是否存在 File f1=new File("d:\\demo"); if(f1.isDirectory()){ FileOutputStream out=new FileOutputStream("e:\\demo\\"+f.getName()); byte bytes[]=new byte[1024]; int temp=0; //边读边写 while((temp=inp.read(bytes))!=-1){ //读 out.write(bytes,0,temp); //写 } //结束 inp.close(); out.close(); System.out.println("文件拷贝成功!"); }else{ //新建 demo 目录 f1.mkdir(); System.out.println("demo 目录不存在,已经新建成功,继续复制"); FileOutputStream out=new FileOutputStream("d:\\demo\\"+f.getName()); byte bytes[]=new byte[1024]; int temp=0; //边读边写 while((temp=inp.read(bytes))!=-1){ //读 out.write(bytes,0,temp); //写 } //结束 inp.close(); out.close(); System.out.println("文件拷贝成功!"); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!