流的分类
File对象可以表示存在的文件或文件夹,也可以表示不存在的。我们想要得到文件的内容怎么办,File只是操作文件,文件的内容如何处理就需要使用io流技术了。
I/O类库中使用“流”这个抽象概念。Java对设备中数据的操作是通过流的方式。“流”屏蔽了实际的I/O设备中处理数据的细节。IO流用来处理设备之间的数据传输。设备是指硬盘、内存、键盘录入、网络等。
Java用于操作流的对象都在IO包中。IO流技术主要用来处理设备之间的数据传输。 由于Java用于操作流的对象都在IO包中。所以使用IO流需要导包如:import java.io.*;
- I/O流=》输入(input)/输出(out)流
- 字节流 =>以字节的形式读写文件
- 字符流=>以字符的形式读写文件
- 字节型文件:音频 视频 word文档 ->由字节组成
- 字符型文件:txt文档 ->由字符组成
- 字节流:可以读取字节型文件,也可以读取字符型文件
- 字符流:只能正常读写字符型文件
四大基类:
字节输入流:InputStream
字节输出流:OutputStream
字符输入流:Reader
字符输出流:Writer
输入就是将文件中的内容读取到程序中
输出就是将程序中的内容写入到文件中
字节流
计算机中都是二进制数据,一个字节是8个2进制位.字节可以表示所有的数据,比如文本,音频,视频.图片,都是作为字节存在的.也就是说字节流处理的数据非常多。
按照数据流的方向可以分为输入流和输出流,输入流是指从数据存储地读取数据传输到程序中,输出流是指将程序中的数据写入数据存储地。
我们已经知道File对象封装的是文件或者路径属性,但是不包含向(从)文件读(写)数据的方法。为了实现对文件的读和写操作需要学会正确的使用Java的IO创建对象。
字节流的抽象基类:
输入流:InputStream
输出流:OutputStream
特点:
字节流的抽象基类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:FileInputStream, ByteArrayInputStream等。
字节输入流
package com.woniuxy.io1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class FileInputStreamTest1 {
public static void main(String[] args){
//1.定位文件位置
String path = "D:\\woniu\\hello.txt";
File file = new File(path);
FileInputStream fileInputStream = null;
try {
//2.建立io流管道
fileInputStream = new FileInputStream(file);
//3.读
byte[] arr = new byte[(int) file.length()];
//把文件中的数据以字节的形式读取到字节数组中
fileInputStream.read(arr);
//4.把字节数组转字符串--指定以GBK的编码格式解码字节数组
String string = new String(arr,"GBK");
System.out.println(string);
}catch (Exception e) {
e.printStackTrace();
}finally {
//5.关流释放资源
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java7的新特性
try(创建管道){
可能会出现异常的代码
}catch(Exception){
}
IO流创建后,会被try自动管理,在程序执行完后,自动关流释放资源,不需要人为的关流
public class FileInputStreamTest2 {
public static void main(String[] args){
//1.定位文件位置
String path = "D:\\woniu\\hello.txt";
File file = new File(path);
//2.建立io流管道--java7新特性-把io流管道交给try-catch管理,自动帮我们关流
try(FileInputStream fileInputStream =new FileInputStream(file)) {
//3.读
byte[] arr = new byte[(int) file.length()];
//把文件中的数据以字节的形式读取到字节数组中
fileInputStream.read(arr);
//4.把字节数组转字符串--指定以GBK的编码格式解码字节数组
String string = new String(arr,"GBK");
System.out.println(string);
}catch (Exception e) {
e.printStackTrace();
}
}
}
字节输出流
package com.woniuxy.io1;
import java.io.File;
import java.io.FileOutputStream;
/**
* 字节输出流
*/
public class FileOutputStreamTest1 {
public static void main(String[] args) {
// 1.定位文件位置
String path = "D:\\woniu\\hello.txt";
File file = new File(path);
//2建立输出流管道 -- true表示再文件已有内容的基础上进行追加
try(FileOutputStream fileOutputStream = new FileOutputStream(file,true)) {
//3.写
String content = "hello world";
//把字符串转字节数组
byte[] bytes = content.getBytes();
fileOutputStream.write(bytes);
} catch (Exception e) {
// TODO: handle exception
}
}
}
案例-文件复制
package com.woniuxy.io1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 文件复制
*
*/
public class CopyFile {
public static void main(String[] args) {
//被复制的文件
String targetPath = "D:\\woniu\\1.png";
File targetFile = new File(targetPath);
//复制后的文件
String copyPath = "D:\\woniu\\2.png";
File copyFile = new File(copyPath);
try(FileInputStream fileInputStream = new FileInputStream(targetFile);
FileOutputStream fileOutputStream = new FileOutputStream(copyFile)
){
//读文件
byte[] by = new byte[(int) targetFile.length()];
fileInputStream.read(by);
//写文件
fileOutputStream.write(by);
}catch (Exception e) {
// TODO: handle exception
}
}
}
字符流
字符流是按照文本的那种字符来读取和输出,就是直接读取数字、字母或是中文字等这些我们能够直接识别的字符。
字符输入流Reader
package com.woniuxy.io2;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
/**
* 字符输入流
*/
public class FileReaderTes1 {
public static void main(String[] args) {
//1.定位文件位置
String path = "D:\\woniu\\hello.txt";
File file = new File(path);
//2.建立io流管道--java7新特性-把io流管道交给try-catch管理,自动帮我们关流
try(FileReader fileReader = new FileReader(file)) {
//3.读
//创建字符数组
char[] ch = new char[(int) file.length()];
fileReader.read(ch);
// System.out.println(Arrays.toString(ch));
//4.把字符数组转字符串
String string = new String(ch);
System.out.println(string);
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符输出流Writer
字体符输出流使用FileWriter对象及其writer()方法将程序中的数据输出到文件中。
package com.woniuxy.io2;
import java.io.File;
import java.io.FileWriter;
/**
* 字符输出流
*/
public class FileWriterTest2 {
public static void main(String[] args) {
// 1.定位文件位置
String path = "D:\\woniu\\hello.txt";
File file = new File(path);
//2建立输出流管道--true表示再文件已有内容的基础上进行追加
try(FileWriter fileWriter = new FileWriter(file,true)) {
//3.写
String content = "hello java";
fileWriter.write(content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
总结:
使用字节输出流或字符输出流向文件中输出内容时,如果没有创建这个文件,会先创建文件,再向文件中输出内容。
换行
我们敲回车实际上有2个动作,回车+换行
回车 \r
换行\n