流是一组有序的数据序列,分为输入流和输出流。IO操作主要是对文件进行操作,而所有的IO操作都在java.io包中,主要是:File,InputStream,OutputStream,Reader,Writer和Serializable。
File操作

    public static void main(String[] args)throws Exception{File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt");//判断父目录是否存在,如果不存在,创建目录,创建文件if (!file.getParentFile().exists()){file.getParentFile().mkdirs();file.createNewFile();}else {    //如果父目录存在,判断文件是否存在,不存在创建新文件if (!file.exists()) {file.createNewFile();System.out.println("创建新文件!");}}System.out.println("文件存在!");}

程序结果:文件存在

   public static void main(String[] args)throws Exception{File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt");if (!file.getParentFile().exists()){file.getParentFile().mkdirs();}if (file.exists()) {System.out.println("文件名称:"+file.getName());System.out.println("文件长度:"+file.length());System.out.println("是文件:"+file.isFile());}

程序结果:
文件名称:text8.txt
文件长度:0
是文件:true

目录查询

public static void main(String[] args)throws Exception {File file=new File("d:"+File.separator+"demo");if (file.isDirectory()){File result[]=file.listFiles();for (int x=0;x<result.length;x++){System.out.println(result[x]);}}}

程序结果:
d:\demo\testA.txt
d:\demo\testB.txt
d:\demo\text8.txt

字节流与字符流
outputStream针对的内存而言,是向文件写入。

public static void main(String[] args)throws Exception{File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt");if (!file.getParentFile().exists()){file.getParentFile().mkdirs();}OutputStream os=new FileOutputStream(file);String str="阿珍爱上了阿强";byte data[]=str.getBytes();os.write(data);os.close();System.out.println("写入成功");}

程序结果:写入成功

InputStream针对文件读取

 public static void main(String[] args)throws Exception{File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt");if (file.exists()){InputStream is=new FileInputStream(file);byte data[]=new byte[1024];int foot=0;int temp=0;while ((temp=is.read(data))!=-1){data[foot++]=(byte) temp;}is.close();System.out.println(new String(data,0,foot));}}

程序结果:阿珍爱上了阿强
使用系统自带的记事本打开会出现乱码。

创建文件输入“阿珍爱上了阿强”并且读取。

public static void main(String[] args) throws IOException {File file=new File("d:"+File.separator+"demo"+File.separator+"test.txt");if (file.exists()) {file.delete();System.out.println("已有文件删除!");}else {try {file.createNewFile();System.out.println("创建文件!");} catch (Exception e) {e.printStackTrace();}}if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}try {OutputStream os=new FileOutputStream(file);String str="阿珍爱上了阿强";byte data[]=str.getBytes();os.write(data);os.close();} catch (Exception e) {e.printStackTrace();}if (file.exists()) {InputStream is=new FileInputStream(file);byte data[]=new byte[1024];int temp=0;int foot=0;while ((temp=is.read())!=-1) {data[foot++]=(byte)temp;}is.close();System.out.println(new String(data,0,foot));}}

文件复制,需要先初始化文件

 public static void main(String[] args)throws  Exception{long start=System.currentTimeMillis();if (args.length!=2) {System.out.println("命令错误");System.exit(1);}File inFile=new File(args[0]); //源文件路径if (!inFile.exists()){System.out.println("文件不存在");System.exit(1);}File outFile=new File(args[1]);  //复制文件路径if (!outFile.getParentFile().exists()) {outFile.getParentFile().mkdirs();}InputStream is=new FileInputStream(inFile);OutputStream os=new FileOutputStream(outFile);byte data[]=new byte[1024];int temp=0;while ((temp=is.read(data))!=-1){os.write(data,0,temp);}is.close();os.close();long end=System.currentTimeMillis();System.out.println("文件复制时间:"+(end-start));}

缓存是对I/O的性能优化,,缓存流增加了内存缓存区。字符缓冲区流:BufferedReader、BufferedWriter。字节缓冲区流:BufferedInputStream、BufferedOutputStream。

 public static void main(String[] args) {File file=new File("d:"+File.separator+"demo"+File.separator+"test.txt");String content[]= {"阿珍","爱上了","阿强"};if (file.exists()) {try {FileWriter fw=new FileWriter(file);BufferedWriter bw=new BufferedWriter(fw);for (int i = 0; i < content.length; i++) {bw.write(content[i]); //遍历输出bw.newLine();  //每一个元素换行显示}bw.close();fw.close(); } catch (Exception e) {e.printStackTrace();}}if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}try {FileReader fr=new FileReader(file);BufferedReader br=new BufferedReader(fr);String str=null;while ((str=br.readLine())!=null) {System.out.println(str);}br.close();fr.close();} catch (Exception e) {e.printStackTrace();}}

程序结果:
阿珍
爱上了
阿强

java基础-IO编程相关推荐

  1. JAVA网络IO编程

    2019独角兽企业重金招聘Python工程师标准>>> JAVA网络IO编程(BIO NIO AIO) 一.传统的BIO编程 1.网络编程的基本模型是C/S模型,即两个进程间的通信. ...

  2. JAVA基础 网络编程

    JAVA基础 网络编程 网络我们每天都会用到的,在我们生活中占的地位也在不断的提高,我们下面就简单了解下网络编程的内容. 想要学习网络编程,首先就需要了解网络编程包含哪些内容. 网络编程包括 IP节点 ...

  3. # Java基础——IO流

    Java基础--IO流 File类的使用(熟悉构造器和方法的使用) File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹) File类的声明在java.io包下 文件和文件目录路径的抽象表示 ...

  4. Java基础-IO流对象之数据流(DataOutputStream与DataInputStream)

    Java基础-IO流对象之数据流(DataOutputStream与DataInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据流特点 操作基本数据类型 ...

  5. 黑马 程序员——Java基础---IO(下)

    黑马程序员--Java基础---IO(下) ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 一.概述 Java除了基本的字节流.字符流之外,还提供 ...

  6. Java基础IO流(二)字节流小案例

    JAVA基础IO流(一)https://www.cnblogs.com/deepSleeping/p/9693601.html ①读取指定文件内容,按照16进制输出到控制台 其中,Integer.to ...

  7. JAVA基础入门编程题练习(一)

    JAVA基础入门编程题练习(一) 1.设计一个学生类Student和它的一个子类Undergraduate.要求如下: (1) Student类有name和age属性,一个包含两个参数的构造方法,用于 ...

  8. Java基础IO流之字符流的使用

    ☆引言☆ 大家好,我是痛而不言笑而不语的浅伤.IO流分为字节流和字符流,而上一篇文章我们学习了字节流(Java基础IO流之字符流的使用),这篇文章带大家一起来学习字符流吧.对文章中描述错误的希望大家积 ...

  9. java基础--IO流之File类

    一.File类概述 用来将文件或者文件夹封装成对象,方便对文件与文件夹的属性信息进行操作,File对象可以作为参数传递给流的构造函数 二.File类常见方法: 1,创建 boolean createN ...

最新文章

  1. 在Chrome中打开网页时出现以下问题 您的连接不是私密连接 攻击者可能会试图从 x.x.x.x 窃取您的信息(例如:密码、通讯内容或信用卡信息)
  2. c语言括号匹配的检验,检验括号匹配的算法
  3. DCIC共享单车数据可视化教程!
  4. win bat 输入参数
  5. ITK:对图像中的结构进行分割
  6. 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
  7. python水平_如何在python中水平透视表
  8. JDBC结合JSP使用(2)
  9. python面试自我介绍_如何拿到半数面试公司Offer——我的Python求职之路
  10. Java多线程之Runable与Thread
  11. Postgre中的 select for update 和 select for update nowait
  12. 油猴脚本Tampermonkey初体验
  13. ubuntu20.4安装gcc5.4
  14. scrapy python中文手册_scrapy 官方文档读完总结
  15. 查看CentOS版本信息
  16. excel单元格中换行的办法/word中添加脚注的方法
  17. PHP 第三方调用 UC_Center用户登录认证
  18. 如何搜集你想要的信息
  19. 分块详解(优雅的暴力)
  20. 经典密码学与现代密码学

热门文章

  1. 《数据结构》-第七章 查找(知识点总结)
  2. 【装机知识】硬盘知识整理
  3. 十三.SpringCloudAlibaba极简入门-集成阿里云OSS对象存储
  4. 弹弹堂为什么我早上登陆显示加载服务器列表失败fail,弹弹堂加载服务器列表失败...
  5. DAG(有向无环图)易懂介绍
  6. 贝莱德伦敦分部面试 - Java岗位
  7. 有些事情,一辈子坚持那么一次,就已经足够了 .
  8. 项目管理:项目进度管理该怎么做?
  9. .\Objects\stm32f4.axf: Error: L6320W 错误分析
  10. Unity mac上运行命令行