IO

  • 文件专属
    • FileInputStream-FileOutputStream
    • FileReader-FileWriter
  • 转化流
  • 缓冲流BufferedWriter-BufferedReader
  • 数据流专属
  • 标准输出流
  • File类
  • 文件拷贝

文件专属

java.io.FileInputStream;java.io.FileOutputStream;java.io.FileReader;java.io.FileWriter;

要自行构造基本数据类型数组,来进行读写,如char[] int[]
前面两个有Stream的是对字节做处理,可以处理文本文档(所有能被写字板打开的文件)、照片、视频、音频等。
要注意的一点是Stream在写入一个文件时,要将文件目录到具体文件名,不然将会拒绝访问

然后每次对文件进行处理如写入新的内容时,要对文件进行刷新——.flush()
在try语句中,最后要在finally处判断文件是否为空,不为空时要将文件关闭,不然太占内存。

FileInputStream-FileOutputStream

字节流

package com.hdujavaTest.IOTest;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/*文件专属
* 读取字节流  不仅能处理文本文档 还可以读取写入图像等格式 因为底层是对字节做处理
*
* */
public class FileInputStreamTest01 {public static void main(String[] args) {//文件字节输入流  读取文件String txt="C:\\Users\\wish_cai\\Pictures\\大学\\psb.jpg";fileInputStreamfuntion(txt);//文件写入/*String txtwrite="D:\\JAVA\\java_test\\IOTest02.txt";fileOutputStreamfuntion(txtwrite);*///复制 文本文档 还行 但是涉及图片就拒绝访问//之前的复制 是FileOutputStream是涉及到文件夹路径C:\Users\wish_cai\Pictures\作业,而FileOutputStream是写入文件,所以要到具体的文件名String txtwritecopy="C:\\Users\\wish_cai\\Pictures\\作业\\新建文本文档.jpg";fileCopy(txt,txtwritecopy);}//读  输入   硬盘到内存public static String fileInputStreamfuntion(String txt){//idea的默认路径是在Project下//如果文件在其他模块下,那么应该读取时   模块\\src(根据实际)\\文件名//文档字节输入流String s=null;FileInputStream fileInputStream=null;try{/*read()一次读取一个字节 并以int形式返回*//*System.out.println("一次读取一个字节 并以int形式返回");fileInputStream=new FileInputStream(txt);int filer;while ((filer=fileInputStream.read())!=-1){System.out.print(filer+" ");}*//*设置一个字节数组 用read(byte[]) 读取字节,返回的是数量*///前面读取完 之后需要重新新建一个FileInputStream对象//当最后数组不够时,会覆盖前面的,后面数组部分依旧会保留/*System.out.println('\n'+"设置一个字节数组 用read(byte[]) 读取字节,返回的是数量:");fileInputStream=new FileInputStream(txt);byte[] bytesarr=new byte[6];int num;while ((num=fileInputStream.read(bytesarr))!=-1){//System.out.println(num);String s=new String(bytesarr,0,num);System.out.print(s);//System.out.print(new String(bytesarr,0,num));}*///.available();//表示还有多少字节可用//System.out.println('\n'+"表示还有多少字节可用");fileInputStream=new FileInputStream(txt);//fileInputStream.available();//表示还有多少字节可用System.out.println(fileInputStream.available());byte[] bytesarr2=new byte[fileInputStream.available()];fileInputStream.read(bytesarr2);String s2=new String(bytesarr2,0,bytesarr2.length);//System.out.println(s2);s=s2;//跳过多少字节/* System.out.println("跳过多少字节");fileInputStream=new FileInputStream(txt);fileInputStream.skip(8);byte[] bytesarr3=new byte[fileInputStream.available()];fileInputStream.read(bytesarr3);String s3=new String(bytesarr3,0,bytesarr3.length);System.out.println(s3);*///} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}return s;}//写 输出  内存到硬盘  此处txt是我们的路径public static void fileOutputStreamfuntion(String txt){FileOutputStream fileOutputStream=null;try{//谨慎使用  文件不存在 会自动新建立路径和内容  文件之前存在 会覆盖原内容fileOutputStream=new FileOutputStream(txt);System.out.println("输入需要写的内容:");Scanner scanner=new Scanner(System.in);String s=scanner.next();//String s="xinjianyigwenjianj.tsg";//s.getBytes();转byte数组fileOutputStream.write(s.getBytes());//追加写入fileOutputStream=new FileOutputStream(txt,true);System.out.println("追加写入输入需要写的内容:");Scanner scanner1=new Scanner(System.in);String s2=scanner1.next();//String s="xinjianyigwenjianj.tsg";//s.getBytes();转byte数组fileOutputStream.write(s2.getBytes());//前面追加//没有快捷键,那我的想法是 先读取之前文件的,然后写入我想加在开头的,然后再将之前的写入//写完之后要刷新fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}//文件的复制public static void fileCopy(String txtread,String txtwrite){FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;try{//先输入一个流对象fileInputStream=new FileInputStream(txtread);//读fileOutputStream=new FileOutputStream(txtwrite);//写//那就一边读  一边写int r;byte[] bytes=new byte[1024*1024];while ((r=fileInputStream.read(bytes))!=-1){fileOutputStream.write(bytes,0,r);}}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null){try{fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream!=null){try{fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}

FileReader-FileWriter

字符流
用字符流 读取文本文件
但是只能处理文本文档

package com.hdujavaTest.IOTest;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*文件专属
用字符流  读取文本文件(能用记事本打开的) 复制文本文件
但是只能处理文本文档
* */
public class Fileread01 {public static void main(String[] args) {/*String txt="D:\\JAVA\\java_test\\IOTest04copy.txt";FileReadfun(txt);*//*String txt1="C:\\Users\\wish_cai\\Pictures\\作业\\Write12.txt";FileWriterfun(txt1);*/String txt="D:\\JAVA\\java_test\\IOTest04copy.txt";String tet2="D:\\JAVA\\java_test\\IOrTow001.txt";CopyFile(txt,tet2);}public static void FileReadfun(String txt){FileReader fileReader=null;try{fileReader=new FileReader(txt);char[] chars=new char[30];int readnum;fileReader.read(chars);for (char c:chars) {System.out.print(c);}fileReader=new FileReader(txt);char[] chars2=new char[30];while ((readnum=fileReader.read(chars2))!=-1){System.out.println(new String(chars2,0,readnum));}fileReader=new FileReader(txt);while ((readnum=fileReader.read())!=-1){System.out.println(readnum);}fileReader=new FileReader(txt);}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileReader!=null){try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}}}//只能写入普通文本  因为处理的是字符    ,前面流是处理字节public static void FileWriterfun(String txt){FileWriter fileWriter=null;try{fileWriter=new FileWriter(txt);System.out.println("input need your contents");Scanner scanner=new Scanner(System.in);String x=scanner.nextLine();fileWriter.write(x);fileWriter.flush();FileReadfun(txt);//要刷新之后才能读取} catch (IOException e) {e.printStackTrace();} finally {if(fileWriter!=null){try {fileWriter.close();} catch (IOException e) {e.printStackTrace();}}}}//复制   只能拷贝普通文本  先读取 再写入public static void CopyFile(String readtxt,String writetxt){FileReader fileReader=null;FileWriter fileWriter=null;try{fileReader=new FileReader(readtxt);fileWriter=new FileWriter(writetxt);char[] rTow=new char[30];int numread=0;while ((numread=fileReader.read(rTow))!=-1){fileWriter.write(rTow);}fileWriter.close();FileReadfun(writetxt);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {}}}

转化流

将字节流转化为字符流

后面实用缓冲流的时候
我们可以不必自定义数组来辅助读写
而缓冲流的构造方法的参数是

 public BufferedReader(Reader in) {this(in, defaultCharBufferSize);}
public BufferedWriter(Writer out) {this(out, defaultCharBufferSize);}

可见是Reader和Writer类型,而在字符流和字节流中,只有字符流 FileWriter和FileReader是符合的。
如果我们想要在缓冲流的构造方法参数中传入字节流 就要用到转换流

OutputStreamWriter(new FileOutputStream("txt"))
InputStreamReader(new FileInputStream("txt"));

缓冲流BufferedWriter-BufferedReader

**
缓冲流专属

  • 不需要自定义char数组,byte数组,自带缓冲
  • 将字符流 ————Reader下面的类FileReader 传入包装流的构造方法
  • FileReader extends InputStreamReader ;;;;; InputStreamReader extends Reader
  • 而如果是字节流 FileInputStream 如何传入呢
  • 通过转换流 转化
  • 可以一行一行读
    **
package com.hdujavaTest.IOTest;import java.io.*;/*
* 缓冲流专属
* 不需要自定义char数组,byte数组,自带缓冲
* 将字符流 ————Reader下面的类FileReader  传入包装流的构造方法
* FileReader extends InputStreamReader         ;;;;; InputStreamReader extends Reader
*
* 而如果是字节流 FileInputStream  如何传入呢
* 通过转换流 转化* 可以一行一行读
* */
public class Buffered01 {public static void main(String[] args) throws IOException {//缓冲流  读  不需构造特定数组String txt="D:\\JAVA\\java_test\\IOTest\\out\\production\\IOTest\\com\\hdujavaTest\\IOTest\\Fileread01.class";Bufferread(txt);//缓冲流 写 也不需构造特定数组String txtwrite="C:\\Users\\wish_cai\\Pictures\\作业\\tete.txt";Bufferwirte(txtwrite);}public static void Bufferread(String txt) throws IOException {//字符流传入缓冲流BufferedReader bufferedReader=null;FileReader fileReader=new FileReader(txt);bufferedReader=new BufferedReader(fileReader);//String x=bufferedReader.readLine();String x;while ((x=bufferedReader.readLine())!=null){System.out.println(x);}bufferedReader.close();//字节流传入缓冲流  通过转化流FileInputStream in=new FileInputStream(txt);//********字节流InputStreamReader inputStreamReader=new InputStreamReader(in);//***********通过转换流 转化为字符流BufferedReader bufferedReader1=null;bufferedReader1=new BufferedReader(inputStreamReader);//*****传入bufferedReader1.close();/*在new 后面的构造方法参数里 就是一个节点流* 用哪个构造方法 其引用就是一个包装流* *//*合并写*/BufferedReader bufferedReader2=new BufferedReader((new InputStreamReader(new FileInputStream(txt))));}public static void Bufferwirte(String txt) throws IOException {BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(txt));//字符流bufferedWriter.write("qweqhuiwfhais.!!!");//对文件作出修改 记得刷新bufferedWriter.flush();bufferedWriter.close();/*转换流*/BufferedWriter bufferedWriter1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txt,true)));//true  追加bufferedWriter1.write("\n"+"1111111111");bufferedWriter1.flush();bufferedWriter1.close();}
}

数据流专属

能够保存数据类型+实际内容
且只能用对应的数据流读写
DataOutputStream

package com.hdujavaTest.IOTest;import java.io.*;public class DataStream01 {public static void main(String[] args) throws IOException {String txt1="D:\\bilibili\\JJDown\\Download\\Java零基础教程视频(适合Java 0基础,Java初学入门)\\Data)";DataOutputStream(txt1);DataInputStream(txt1);}//数据字节输入流  且带有数据基本类型//且只能通过对应的DataInputstream打开public static void DataOutputStream(String txt) throws IOException {DataOutputStream dataOutputStream=new DataOutputStream(new FileOutputStream(txt));int a=1;int b=2;dataOutputStream.writeInt(a);dataOutputStream.writeInt(b);dataOutputStream.flush();dataOutputStream.close();}//读取专属数据流//而且要知道其专门的顺序public static void DataInputStream(String txt) throws IOException {DataInputStream dataInputStream=new DataInputStream(new FileInputStream(txt));int x=dataInputStream.readInt();int y=dataInputStream.readInt();System.out.println(x);System.out.println(y);}
}

标准输出流

直接写入到文件中

package com.hdujavaTest.IOTest;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
//不需要手动关闭close
public class PrintStream01 {public static void main(String[] args) throws FileNotFoundException {System.out.println("标准输出流——print");PrintStream ps=System.out;ps.println("adsad");ps.println(1);ps.println(true);//写入到固定路径的文件中PrintStream ps1=new PrintStream(new FileOutputStream("D:\\bilibili\\JJDown\\Download\\Java零基础教程视频(适合Java 0基础,Java初学入门)\\ad"));ps1.println(11111);ps1.println("ahuidqaas");}
}

File类

File和四大家族没什么关系,所以File不能完成文件的读和写
*File对象代表什么?

  • C:\ddd
  • C:\dasdf\asf\asfasf\asfa.txt
  • File对象可能是对应的目录也可能是文件
  • File只是一个路径的抽象表示形式
    在对于文件的处理上也十分便捷。
    提供了判断文件路径是否存在,不存在时新建,返回上一次修改的时间,删除文件路径、返回路径的绝对形式、返回文件名、返回所有子文件的文件名(listFiles)等。
public class File01 {public static void main(String[] args) {File file=new File("C:\\Users\\wish_cai\\Pictures\\作业\\");System.out.println("文件是否存在:"+file.exists());System.out.println(file.length());//不存在 以文件形式新建File file1=new File("C:\\Users\\wish_cai\\Pictures\\作业\\xinjian");if(!file1.exists()) {try {file1.createNewFile();} catch (IOException e) {e.printStackTrace();}}File file2=new File("C:\\Users\\wish_cai\\Pictures\\a\\b\\c\\");if(!file2.exists())file2.mkdirs();//mkdirs建立多重目录  否则 mkdir只能新建一个,在多重目录要求下 没有s将不会建立。}
}

文件拷贝

java|IO流实现文件拷贝

package com.hdujavaTest.IOTest;import java.io.*;/***问题一:得到子文件夹的目录之后,因为FileInputStream  需要读到文件名 所以其拒绝访问————递归调用* 涉及到 新建目录** */
public class CopyFile01 {public static void main(String[] args) {File filesrc=new File("C:\\Users\\wish_cai\\Pictures\\作业");File filedest=new File("D:\\bilibili\\JJDown\\Download\\Java零基础教程视频(适合Java 0基础,Java初学入门)\\copy\\");CopyDir(filesrc,filedest);}private static void CopyDir(File filesrc, File filedest){if(filesrc.isFile()){//是文件先拷贝,再跳出该次递归,否则即为目录,进入目录操作。//System.out.println(filesrc.getAbsolutePath());//是文件就进行拷贝处理copyfile(filesrc,filedest);return;}File temFile=null;File[] files=filesrc.listFiles();for (File f:files) {//文件或目录File newdest=null;File newsrc=null;if(f.isDirectory()){/*当f是目录中的一个子目录时 进入*/String name=f.getName();String destDir=filedest.getAbsolutePath().endsWith("\\")?(filedest.getAbsolutePath()+name):(filedest.getAbsolutePath()+"\\"+name);/*System.out.println(destDir);*///在拷贝路径中生成新的目录newdest=new File(destDir);if(!newdest.exists())newdest.mkdirs();/*String srcDir=f.getAbsolutePath().endsWith("\\")?f.getAbsolutePath():(f.getAbsolutePath()+"\\");*/String srcDir=f.getAbsolutePath();System.out.println(srcDir);newsrc=new File(srcDir);temFile=newdest;//更新拷贝路径CopyDir(newsrc,temFile);}//temFile=newdest;/*当目录下的是一个文件时  进入下一次循环,然后直接拷贝*/if(!(f.isDirectory())){CopyDir(f,filedest);}}}private static void copyfile(File src,File dest){FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;String txt=dest.getAbsolutePath().endsWith("\\")?dest.getAbsolutePath():(dest.getAbsolutePath()+"\\")+src.getName();System.out.println(txt);try{fileInputStream=new FileInputStream(src);fileOutputStream=new FileOutputStream(txt);byte[] bytes=new byte[1024*1024];int readcount;while ((readcount=fileInputStream.read(bytes))!=-1){fileOutputStream.write(bytes,0,readcount);}fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}

JAVA|IO流的练习相关推荐

  1. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter

    Java IO流学习总结四:缓冲流-BufferedReader.BufferedWriter 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/det ...

  2. Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...

  3. Java IO流思维导图

    Java IO流

  4. Java读取文件流用什么对象_使用Java IO流实现对文本文件的读写过程中,通常需要处理下列( )异常。_学小易找答案...

    [论述题]请根据第一次平时作业的选题,结合第二次课内容,自拟一个论文提纲. [单选题]在 switch ( expression )语句中, expression 的数据类型不能是 ( ) [单选题] ...

  5. java io流大全_Java IO流系统整理

    Java IO流的分类 Java中的流,可以从不同的角度进行分类. 按流向分类: 输入流: 程序可以从中读取数据的流. 输出流: 程序能向其中写入数据的流. 按数据传输单位分类: 字节流:以字节(8位 ...

  6. Java IO流学习总结八:Commons IO 2.5-IOUtils

    Java IO流学习总结八:Commons IO 2.5-IOUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/550519 ...

  7. Java IO流学习总结七:Commons IO 2.5-FileUtils

    Java IO流学习总结七:Commons IO 2.5-FileUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/5497 ...

  8. Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream

    Java IO流学习总结六:ByteArrayInputStream.ByteArrayOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ar ...

  9. Java IO流学习总结二:File

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54581478 本文出自[赵彦军的博客] Java IO流学习总结一:输入输出流 J ...

  10. java io流(字符流) 文件打开、读取文件、关闭文件

    java io流(字符流) 文件打开 读取文件 关闭文件 //打开文件 //读取文件内容 //关闭文件 import java.io.*;public class Index{public stati ...

最新文章

  1. w ndows连接USB不正常,Raspberry Pi Zero W 连接电脑 – 针对Windows 10 缺少RNDIS驱动
  2. CKEditor上传图片—配置CKFinder
  3. C ++ 指针 | 指针的详细概念和使用_1
  4. 28天打造专业红客(四)
  5. 【案例】复制静止问题一则
  6. 网站服务器和空间大小,网站服务器和空间大小
  7. 苹果电脑系统如果删除驱动
  8. Visual Foxpro 6.0教程
  9. docker安装微信
  10. BF-5R对讲机改频
  11. java实现将.acc格式转化为mp3格式
  12. 鸿蒙系统背后的故事,华为“鸿蒙”刷屏背后,这7本书是中国人的终极浪漫
  13. 154. 正则表达式匹配
  14. 有监督的SAR变化检测公开数据集
  15. 1. 从键盘输入一系列字符(以回车符结束,字符的个数不超过 200 个),统计输入字符串中数字与非数字字符的个数,并将计数结果输出。
  16. Transformer主干网络——ViT保姆级解析
  17. 模电—初探MOSFET
  18. 你听不出是AI在唱歌!这个日本虚拟歌姬,横扫中英日三种语言
  19. 舔砖加瓦(java)之java常用类
  20. 数据库系统中的三级模式、两级映像以及数据独立性(附绪论思维导图)

热门文章

  1. 分享一种很好看的字体
  2. 2011年11月份第二周51Aspx源码发布详情
  3. echarts无数据时显示暂无数据或者用图片代替
  4. 基于SOM算法的Iris数据分类
  5. document.execCommand(‘copy‘) 复制不成功 不同浏览器兼容问题
  6. VB的阶乘和伽马函数
  7. app源码+php+l,android商城APP全套源码(服务端+客户端)
  8. java生成pdf417条形码_python生成417条形码(PDF417)
  9. 2015年全国大学生电子设计竞赛A题(双向DC-DC变换器)训练总结(硬件部分)
  10. 华为HCIA-DATACOM题库(带答案及解析) 全网首发