Java IO基础总结
Java中使用IO(输入输出)来读取和写入,读写设备上的数据、硬盘文件、内存、键盘…,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是输出流。
根据处理的数据类型可分为字节流和字符流
1.字节流可以处理所有数据类型的数据,在java中以Stream结尾
2.字符流处理文本数据,在java中以Reader和Writer结尾。
流的详细分类如下:
按照数据流向的不同:输入流 输出流
按照处理数据的单位的不同:字节流 字符流(处理的文本文件)
按照角色的不同:节点流(直接作用于文件的) 处理流
IO的体系
抽象基类 | 节点流(文件流) | 缓冲流(处理流的一种) |
---|---|---|
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedOutputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
我们来看个IO流的详解图:
IO流的本质是对字节和字符的处理,那么我们平时也是用来处理文件的,就从文件处理开始接触这方面的知识。
// 创建一个文件路径
File file = new File("D:\\testData.txt");
if(file.exists()){
//得到文件路径
System.out.println(file.getAbsolutePath());
//得到文件大小
System.out.println("文件大小:"+file.length());
}
//创建文件和创建文件夹
File file1 = new File("d:\\iotest.txt");
if(!file1.exists())
{
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("文件已存在");
}
//创建文件夹
File file2 = new File("d:\\testIO");
if(file2.isDirectory())
{
System.out.println("文件夹存在");
}else{
file2.mkdir();
}
//列出一个文件夹下的所有文件
File f = new File("d:\\testIO");
if(f.isDirectory())
{
File lists[] = f.listFiles();
for(int i=0;i<lists.length;i++)
{
System.out.println(lists[i].getName());
}
}
注意:
在使用IO流写入文件时,如果忘记关闭文件(close)同时也没有调用flush(),则被写入的文件中是没有内容的。在关闭文件的同时系统会自动调用flush()
常用字节流FileInputStream和FileOutputStream:
FileInputStream
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\testData.txt");
byte bytes[]=new byte[1024];
int n=0;
while((n=fis.read(bytes))!= -1){
String str = new String(bytes,0,n);
// 把文件内容输入到控制台
System.out.print(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
程序运行结果如下:
FileOutputStream:
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\\testData.txt");
// 把字符串写入文件
String str = "报效国家,舍生忘死";
byte bytes[] = str.getBytes();
fos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
如果是续写文件,则可以加上参数:
字符流FileReader和FileWriter:
//字符流
//文件写出 输入流
FileReader freader = null;
//写入到文件 输出流
FileWriter fwriter = null;
try {
//创建输入对象
freader = new FileReader("d:\\testData.txt");
//创建输出对象
File f1 = new File("e:\\testData.txt");
if(!f1.exists()){
f1.createNewFile();
}
fwriter = new FileWriter(f1);
//读入到内存
char chars[] = new char[1024];
int n=0;
while((n=freader.read(chars))!= -1)
{
fwriter.write(chars);
//System.out.println(chars);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try{
freader.close();
fwriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
缓冲字符流 bufferedReader bufferedWriter
项目的路径如下图:
public static void main(String[] args) throws IOException{
BufferedReader br = null;
BufferedWriter bw = null;
FileReader fr = null;
FileWriter fw = null;
try {
// 绝对路径
fr = new FileReader("test.txt"); // 输入源文件位置
fw = new FileWriter("test-output.txt", false); // 输出结果文件位置
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
//循环读取
String s ="";
while((s = br.readLine()) != null)
{
// 需要调用close()或者调用flush()才会写入磁盘,否则不会写入
bw.write(s);
bw.newLine();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
bw.close();
br.close();
fw.close();
fr.close();
}
}
注意:在使用BufferedWriter写入文件时,如果忘记关闭文件(close)同时也没有调用flush(),则被写入的文件中是没有内容的。在关闭文件的同时系统会自动调用flush()
FileInputStream与FileOutputStream文件输入流与文件输出流
FileInputStream读取的两种方法:逐字节读;以字节数组读取两种方式;
public static void main(String[] args) {
//创建文件对象,指定要读取的文件路径(要读的文件一定要存在)
File file=new File("E:\\a.text");
try {
//创建文件输入流对象.指定要读取的文件对象
FileInputStream fin=new FileInputStream(file);
/***********方法一(将输入流的数据传递给字节数组)*********/
//创建字节数组,准备将文件流中的数据传给字节数组
/*byte[] b=new byte[fin.available()];
//将字节流中的数据传递给字节数组
fin.read(b);
//将字节数组转为字符串
String s=new String(b);
System.out.println(s);*/
/*********************************************/
/********方法二(逐字节读取数据从字节输入流)***********/
int l;
while ((l=fin.read())!=-1) {
System.out.println((char)l);
//测试read()方法的含义,什么是逐字节读,及int类型的l代表什么意思,测试结果l代表存储的内容的int的表现形式,与进制相关,不做深究
//System.out.println((char)l+"\t"+l);
}
fin.close();
/************************************************/
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream写入文件的两种方式:以字节数组写入;逐字节写入;
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建文件对象,指定要写出的文件路径
File file=new File("d:\\d.text");
try {
//创建文件字节输出流对象,准备向d.txt文件中写出数据,true表示在原有的基础上增加内容
FileOutputStream fout=new FileOutputStream(file,true);
Scanner sc=new Scanner(System.in);
System.out.println("请写出一段字符串:");
String msg=sc.next()+"\r\n";;
/******************(方法一)按字节数组写入**********************/
//byte[] bytes = msg.getBytes();//msg.getBytes()将字符串转为字节数组
//fout.write(bytes);//使用字节数组输出到文件
/******************(方法一)逐字节写入**********************/
byte[] bytes = msg.getBytes();
for (int i = 0; i < bytes.length; i++) {
fout.write(bytes[i]);//逐字节写文件
}
fout.flush();//强制刷新输出流
fout.close();//关闭输出流
System.out.println("写入完成!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
字节流转化为字符流
//创建字节输出流对象
FileOutputStream fout=new FileOutputStream(new File("student.xml"));
//创建字节流缓冲区,加快写出速度
BufferedOutputStream bout=new BufferedOutputStream(fout);
//创建字符输出流对象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(bout, "utf-8"));
doc.write(bw);
bw.flush();
bw.close();
为什么将字节流转化为字符流?
因为字节流是二进制读写的,在外部打开是乱码,转化为字符串或字符可以让文件在外部也可以看到内容!根据具体的需求确定用哪个!
new FileOutputStream的构造函数如下
因此new FileOutputStream里面只能接收File对象或者一个路径。
new FileInputStream的构造函数如下
FileInputStream里面只能接收File对象或者一个路径。
BufferedInputStream的构造函数如下
BufferedInputStream可以接收inputSream流