JavaSE 10 —IO流
1.IO流简介
Java的输入输出功能来自java.io包中的InputStream类,OutputStream类,Reader类和Writer类以及继承它们的各种子类。每一个数据流都是一个对象,它们提供了各种支持“读入”与写出操作的流类。
File类用于封装系统的文件和目录的相关信息。在该类中定义了一些与平台无关的方法来操作文件。
创建File对象可以通过3种方式:
new File(String pathName)
new File(String parent,String child)
new File(File parent,String child)
2.字节输入输出流
1)字节输入流抽象类InputSream
该类是输入留的抽象类,定义了操作输入留的各种方法。
实例:创建InputStream实例is 并将其赋值为System类的in属性,定义为控制台输入流,从is输入留中获取字节信息,用这些字节创建字符串,并打印到控制台输出。
import java.io.*;
public class InputMessage
{
public static void main(String[] args){
InputStream is = System.in;
try{
byte[] bs = new byte[1024];
while(is.read(bs)!=-1){
String str = new String(bs).trim();//去掉字符串开头和结尾的空格
System.out.println(str);
}
is.close()
}catch(IOException e){
e.printStackTrace();
}
}
}
虽然Java在程序结束时会自动关闭所有打开的流,但是当使用完流后,显示地关闭任何打开的流仍时一个好习惯。
2)字节输出留抽象类OutputStream
该类是字节输出流的抽象类,定义了输出流的各种操作方法。
实例:创建OutputStream实例out,并将其赋值为System.out标准输出流。通过write()方法向流中写入数据。
import java.io.*;
public class OutPutData
{
public static void main(String[] args){
OutStream out = new System.out;
try{
byte[] bte = "Hello world\n".getBytes();
out.write(bte);
bte = "oh fuck you\n".getBytes();
out.write(bte);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
3)文件字节输入流类FileInputStream
文件字节输入流可以从指定路径的文件中读取字节数据。文件字节输入流类继承自InputStream类,并实现了读取输入流的各种方法。
语法如下:
new FileInputStream(File file);
例如:
try{
FileInputStream fileInputStream = new FileInputStream(file);
}catch(FileNotFoundException e){
e.printStackTrace();
}
4)文件字节输出流类FileOutputStream
文件字节输出流关联指定路径的文件,数据通过文件字节输出流以字节单位输出并保存到文件中。
语法:
new FileOutputStream(file);
new FileOutputStream(filePath);
实例:创建OutputStream实例out,并将其赋值为System.out标准输出流。通过write方法向流中写入数据。
import java.io.*;
public class FileCreate
{
public static void main(String[] args){
FIle file = new FIle("C:/","word.txt");
try{
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
byte buy[] = "Hello world,Hello China".getBytes();
out.write(buy);
out.close();
}catch(Exception e){
e.printStackTrace();
}
try{
FileInputStream in = new FileInputStream(file);
byte byt[] = new byte[1024];
int len = in.read(byt);
System.out.println(new String(byt,0,len));
in.close();
}catch(Exception e){
e.printStrackTrace();
}
}
}
3.字符输入输出流
1)字符输入流抽象类Reader
该类是字符输入流的抽象类,定义了操作字符输入流的各种方法。
2)字符输出流抽象类Writer
该类是字符输出流的抽象类,定义了操作字符输入流的各种方法。
3)文件字符输入L流FileReader
文件字符输出流与文件字节输入流的功能相似,但是传送数据的方式不一样,字节流以字节为单位传送数据,可以是文本,视频,音频,图片。字符流以字符为单位传送数据,只能传送文本类型。
语法:
new File(File file);
例如:
File file = new File(“C://word.txt”);
try{
FileReader FileReader = new FileReader(file);
}catch(FileNotFoundException e1){
e1.printStackTracer();
}
实例:创建类FileInAndOutChar,读取C盘中的word.txt文件,并输出在控制台。
public class FileInAndOutChar
{
public static void main(String[] args){
File file = new File("C://word.txt");
try{
if(!file.exists())
System.out.println("Cannot open it!");
else{
FileReader fr = new FileReader(file);
char[] data = new char[256];
int length = 0;
while((length = fr.read(data))>0){
String str = new String(data,0,length);
System.out.println(str);
}
fr.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
4)文件字符输出流FileWriter
文件字符输出流类(FileWriter)继承自Writer类,提供了向文件输出数据的各种方法,数据通过文件字符输出流以字符为单位输出并保存在文件中。
语法:
new FileWriter(file);
综合实例:
1. 创建类CopyFile,在该类中创建copy()方法,改方法接受文件数组和目标文件夹两个参数,如果目标文件夹不存在,则调用mkdir()方法创建文件夹,然后在循环中将文件数组中的每个文件对象写到文件夹中。
package qijia.si;
import java.io.*;
public class JavaTest {
public static void copy(File[] fl,File file){
if(!file.exists());
file.mkdir();
for(int i=0;i<fl.length;i++){
if(fl[i].isFile()){
try{
FileInputStream fis = new FileInputStream(fl[i]);
FileOutputStream out = new FileOutputStream(new File(file.getPath()+File.separator+fl[i].getName()));
int count = fis.available();
byte[] data = new byte[count];
if((fis.read(data))!=-1){
out.write(data); //复制内容
}
out.close();
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(fl[i].isDirectory()){
File des = new File(file.getPath()+File.separator+fl[i].getName());
des.mkdir();
copy(fl[i].listFiles(),des); //递归调用方法本身
}
}
}
}
2. 创建类BranchWriter,在主方法中定义文件对象,将该对象作为参数创建BufferedWriter类实例,调用改实例的wirter()方法将数据写入文件中,然后调用newLine()方法写入换行符,实现分行向文件中写入数据。
package qijia.si;
import java.io.*;
public class JavaTest {
public static void main(String[] args){
String filePath = "C://BranchWriter.txt";
File file = new File(filePath);
try{
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hello world".toCharArray());
bw.newLine();
bw.write("China!");
bw.newLine();
bw.flush(); //刷新缓冲区
System.out.println("成功!");
}catch(Exception e){
e.printStackTrace();
}
}
}