您的位置:首页 > 编程语言 > Java开发

JAVA之IO(输入输出)学习

2018-03-02 18:45 375 查看
简介:

Java中,将这种通过不同输入输出设备(键盘, 内存, 显示器, 网络等)之间的数据传输抽象表述为”流”,程序通过流的方式与输入输出设备进行数据传输。在Java中,流都位于java.io包中,所以又称之为IO流(输入输出)流。根据操作数据的不同可以分为字节流和字符流,根据数据传输方向的不同可分为输入流和输出流,程序从输入流中读取数据,向输出流中写入数据。

在Java的IO包中,字节流的输入输出流: java.io.InputStream和java.io.OutputStream

字符流的输入输出流: java.io.Reader和java.io.Writer



字节流:

其实在我们所说的计算机中,无论是文本、图片、音频还是视频数据,所有的文件都是以二进制(字节)形式存在,IO流针对字节的输入输出提供了一系列的流,统称为字节流。在Java中,提供两个抽象类InputStream和OutputStream,这个两个是字节流的顶级父类,所有的字节输入输出流都继承自它们。由于这两个类是抽象类,不能被实例化,因此针对不同的功能,它们都提供了不同的子类,这些子类形成了一个体系结构,如下图。





从图可以看出,InputStream和OutputStream的子类很多都是大致对应的。图中列出的IO流都是程序中常见的。

字节流Demo:

public static void FileInput() throws IOException {
//创建一个文件字节输入流
FileInputStream in = new FileInputStream("src\\Zhuanti\\text.txt");
//定义一个int类型的变量b,记住每次读取的一个字节
int b = 0;
while (true)
{
//变量b记住读取的一个字节
b = in.read();
//如果读取的字节为-1,结束
if (b==-1)
break;
System.out.println(b);
System.out.println((char)b);
}
in.close();
}


//这种方式在已存在的文件中在写入数据,会覆盖原有数据
public static void FileOutput() throws IOException {
//创建一个文件字节输出流
FileOutputStream out = new FileOutputStream("src\\Zhuanti\\out.txt");
String str = "hello world!";
byte[] b = str.getBytes();
for (int i=0; i<b.length; i++)
{
out.write(b[i]);
}
out.close();
}

//追加数据,不会覆盖原有数据
public static void AppendFileOutput() throws IOException {
//创建一个文件字节输出流,追加数据到文件末尾
FileOutputStream out = new FileOutputStream("src\\Zhuanti\\out.txt",true);
String str = "hello world!";
byte[] b = str.getBytes();
for (int i=0; i<b.length; i++)
{
out.write(b[i]);
}
out.close();
}


//文件的拷贝,同时用到输入输出流
public static void CopyFlie() throws IOException {
InputStream in = new FileInputStream("src\\Zhuanti\\source.txt");
OutputStream out = new FileOutputStream("src\\Zhuanti\\target.txt");

int len;//定义一个int类型的变量len,记住每次读取的一个字节
long begintime = System.currentTimeMillis();//拷贝前系统时间
while ((len=in.read())!=-1)
out.write(len);

long endtime = System.currentTimeMillis();
System.out.println("Copy times:"+(endtime-begintime)+"ms");

in.close();
out.close();
}


//字节流的缓冲区
public static void BuffCopyFile() throws IOException {
InputStream in = new FileInputStream("src\\Zhuanti\\source.txt");
OutputStream out = new FileOutputStream("src\\Zhuanti\\target.txt");

//用缓冲区读写文件
byte[] buff = new byte[1024]; //定义一个字节数组,作为缓冲区

int len; //定义len,记住读取 读入缓冲区的字节数

long begintime = System.currentTimeMillis();

while ((len = in.read(buff))!=-1)
out.write(buff,0,len);//从第一个字节开始,向文件写入len个字节

long endtime = System.currentTimeMillis();
System.out.println("Copy times:"+(endtime-begintime)+"ms");

in.close();
out.close();
}


//IO包中提供两个待缓冲的字节流
public static void BufferedCopyFile() throws IOException {
//创建一个带缓冲区的输入流
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("src\\Zhuanti\\source.txt"));
//创建一个带缓冲区的输出流
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("src\\Zhuanti\\target.txt"));
int len;
while ((len=bis.read())!=-1)
bos.write(len);
bis.close();
bos.close();
}


字符流:

Java在操作文件的时候使用的是字节流,但是在操作字符的时候就不太方便了,为此JDK提供了字符流。同字节流一样,字符流也有两个顶级父类,分别是Reader(字符输入流)和Writer(字符输出流)。





字符流Demo:

//从文件中直接读取字符
public static void ReadFile() throws IOException {
//创建一个FileReader对象用来读取文件中的字符
FileReader reader = new FileReader("src\\Zhuanti\\text.txt");
//定义一个变量用于记录读取的字符
int ch;
//循环判断是否读取到文件的末尾
while ((ch=reader.read())!=-1)
System.out.println((char)ch);
reader.close();//关闭文件读取流,释放资源
}


public static void WriteFile() throws IOException {
FileWriter writer=new FileWriter("out.txt");
// FileWriter writer=new FileWriter("out.txt",true);//追加方式写入
String str = "hello world!";
writer.write(str);
writer.close();
}


public static void ReadlineFile() throws IOException {
FileReader reader = new FileReader("src\\Zhuanti\\source.txt");
//创建一个bufferedReader缓冲对象
BufferedReader br = new BufferedReader(reader);

FileWriter writer = new FileWriter("src\\Zhuanti\\target.txt");
//创建一个bufferedWriter缓冲对象
BufferedWriter bw = new BufferedWriter(writer);
String str;

//每次读入
while ((str=br.readLine())!=null)
{
bw.write(str);
bw.newLine();//写入换行符
}
br.close();
bw.close();
}


转换流:

前面提到IO流分为字符流和字节流,有时二种流直接也需要进行转换。

//字节流转换成字符流(只能操作文本文件)
public static void Stream2ZF() throws IOException {
//创建字节输入流
FileInputStream in = new FileInputStream("src\\Zhuanti\\source.txt");
//将字节流输入转换成字符输入流
InputStreamReader isr = new InputStreamReader(in);
//对字符流对象进行包装
BufferedReader br = new BufferedReader(isr);

FileOutputStream out = new FileOutputStream("src\\Zhuanti\\target.txt");
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(osw);

String line;
while ((line = br.readLine())!=null)
bw.write(line);

br.close();
bw.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: