读写IO流操作
Posted 秋天de枫叶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读写IO流操作相关的知识,希望对你有一定的参考价值。
什么是IO流:
IO流流简单来说就是Input和Output流,IO流主要是用来处理设备之间的数据传输,Java对于数据的操作都是通过流实现,而java用于操作流的对象都在IO包中。分类:
按操作数据分为:字节流和字符流。 如:Reader和InpurStream按流向分:输入流和输出流。如:InputStream和OutputStream
IO流常用的基类:
* InputStream , OutputStream字符流的抽象基类:
* Reader , Writer由上面四个类派生的子类名称都是以其父类名作为子类的后缀:如:FileReader和FileInputStream
经常用的数据流操作实例:
FileInputSteram 与FileOutStream :
public static void main(String[] args)
try
//新建一个文件
File f=new File("E:/IOtest/fileInputStream.txt");
//字节输出流
FileOutputStream fout=new FileOutputStream(f);
String str="往文件中输入数据";
fout.write(str.getBytes());
//字节输入流
FileInputStream fis=new FileInputStream(f);
//建一个水桶,用于存放读的数据
byte[] b=new byte[50];
int hasnum=-1;
String line="";
while((hasnum=fis.read(b))!=-1)
line +=new String(b,0,hasnum);
System.out.println(line);
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
FileReader 与FileWriter
public static void main(String[] args)
try
//新建一个文件
File f=new File("E:/IOtest/fileRead.txt");
//字节输出流
FileWriter fout=new FileWriter(f);
String str="往文件中输入数据--字符流";
fout.write(str);
fout.flush(); //必须执行flush或close方法,因为字符流不是直接写入磁盘的,
fout.close(); //而是先写入内存中的。如果不执行flush与close方法,是不会写入文件中的。
//字符输入流
FileReader fr=new FileReader(f);
//建一个水桶,用于成输入进来的数据
char[] c=new char[50];
int hasnum=-1;
String line="";
while((hasnum=fr.read(c))!=-1)
line +=new String(c,0,hasnum);
System.out.println(line);
catch (Throwable e)
e.printStackTrace();
字节流缓冲区:BufferedInputSteram和BufferedOutputStream
public static void main(String[] args)
try
//新建一个文件
File f = new File("E:/IOtest/bufferedfileInputStream.txt");
//字节输出流
FileOutputStream fout = new FileOutputStream(f);
BufferedOutputStream bufferOut=new BufferedOutputStream(fout);
String outStr = "往文件中输入数据-字节流-缓存";
bufferOut.write(outStr.getBytes());
bufferOut.flush();//缓冲字节流需要进行刷新操作,
bufferOut.close();
//输入流
FileInputStream fis=new FileInputStream(f);
BufferedInputStream buffIn=new BufferedInputStream(fis);
//水桶
byte[] b=new byte[100];
int hasNum=-1;
String inStr="";
while((hasNum=buffIn.read(b))!=-1)
inStr+= new String(b,0,hasNum);
System.out.println(inStr);
catch (Throwable th)
th.printStackTrace();
字符流的缓冲区:BufferedReader和BufferedWreiter
public static void main(String[] args)
try
//新建一个文件
File f = new File("E:/IOtest/bufferedfileReader.txt");
//字节输出流
FileWriter wOut = new FileWriter(f);
BufferedWriter bufferOut=new BufferedWriter(wOut);
String outStr = "往文件中输入数据-字符流-缓存";
bufferOut.write(outStr);
bufferOut.flush();
bufferOut.close();
//输入流
BufferedReader buffRead=new BufferedReader(new FileReader(f));
//水桶
char[] c=new char[100];
int hasNum=-1;
String rStr="";
while((hasNum=buffRead.read(c))!=-1)
rStr+= new String(c,0,hasNum);
System.out.println(rStr);
catch (Throwable th)
th.printStackTrace();
转换流:InputStreamReader 与OutputStreamWriter
public static void main(String[] args)
try
//新建一个文件
File f = new File("E:/IOtest/InputSreamReader.txt");
FileOutputStream fout = new FileOutputStream(f); //字节流
OutputStreamWriter osr=new OutputStreamWriter(fout,"utf-8");//转换流
BufferedWriter fw=new BufferedWriter(osr); //缓冲字符流
String outStr = "往文件中输入数据-字节流-转换流-缓冲流";
fw.write(outStr);
fw.flush();
fw.close();
//输入流
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"));
//水桶
char[] c=new char[100];
int hasNum=-1;
String inStr="";
while((hasNum=br.read(c))!=-1)
inStr+= new String(c,0,hasNum);
System.out.println(inStr);
catch (Throwable th)
th.printStackTrace();
借鉴博客: https://www.cnblogs.com/xll1025/p/6418766.html
以上是关于读写IO流操作的主要内容,如果未能解决你的问题,请参考以下文章