IO流的概要总结:
一.File:
File类的理解:
a.File类的一个对象,代表文件或文件目录(俗称文件夹)
b.File类声明在java.io包下。
c.Flie类中涉及到关于文件或文件目录的创建,删除,重命名,修改时间,文件大小等方法,并涉及到写入或读取文件内容操作,如果需要读取或写入文件内容,需要IO流来完成。
d.后续File类的对象常会作为参数传递到流的构造器中指明读取或者写入的"终点"。
File的实例化:
File(String filePath)
File(String parentPath,String childPath)
File(File parentPath,String childPath)
File file = newFile("D:\\multithread\\src\\work\\t.txt" );//绝对路径
File file1 = new File( "t.txt" );//绝对路径
File file2 = new File( file1,"t.txt" );
相对路径:相对于某个路径下,指明的路径。
绝对路径:包含盘符在内的文件或者文件目录的路径。
(说明:在Idea中:用单元测试方法测试,相对路径即为当前Module下,使用mian()测试,相对路径在Project下。)
路径分隔符:windows:\ \ linux:/
方法:
canWrite() :检查应用程序是否可以修改文件的抽象路径名记。
createNewFile() :自动创建一个新的空文件命名的抽象路径名的当且仅当该文件不存在。
createTempFile(String prefix, String suffix): 在默认的临时文件目录中创建一个空文件,使用给定的前缀和后缀来生成它的名称
delete() :删除文件或目录的路径名表示的抽象。
exists() :检查文件或目录是否存在这种抽象路径名记。
isAbsolute() :测试是否这个抽象路径名是绝对的。
isDirectory(): 测试文件是否通过这种抽象路径名表示是一个目录。
isFile() :测试文件是否通过这种抽象路径名表示的是一种正常的文件。
二.IO流:
概念:用于设备之间的数据传输。写文件,网络通信…
说明:对于数据的输入、输出操作以"stream"的方式进行。
java.io包下提供了各种流类,用来获取不同种类的数据。
流的分类:
字节流和字符流:字节流(byte),字符流(char一般文本)。
流向不同:输入流,输出流。
流的角色:节点流(不加工),处理流(加工)。
抽象类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
(抽象类不能实例化!)
节点流:
分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
---|---|---|---|---|
抽象类 | InputStream | OutputStream | Reader | Writer |
实现类 | FileInputStream | FileOutputStream | FileReader | FileWriter |
(其余基本是处理流在它们基础上实现。)
概念:
1.输入与输出式相对于参照物而言的
2.以程序或者运行内存为主体,从程序将数据写到磁盘即为:输出流
以程序或者运行内存为主体,从磁盘将数据读入程序即为:输入流。
字符流实例:
//读取(用try-catch此处不用举例)
public class FileT {
public static void main(String[] args) throws IOException {
//实例化File类的对象,指明文件
File file = new File( "hello.txt" );
//指定具体的流
FileReader reader = new FileReader( file );
//数据读取
int read = reader.read();
while (read!=-1){//返回-1,说明到达文件末位
System.out.println((char) read);
read=reader.read();
}
reader.close();//关闭流
}
}
//总结:1.File类的实例化
//2.FileReader流的实例化
//3.读入的操作
//4.资源的关闭
//写出(伪代码)
public class FileT {
public static void main(String[] args) throws IOException {
File file = new File( "hell0.txt" );//该文件不存在会自动创建
FileWriter writer = new FileWriter( file );
writer.write( "I have a dream!" );
writer.close();
}
}
//如果文件存在,new FileWriter( file )默认对原有文件覆盖
//new FileWriter( file,true )追加
字节流实例:(使用字节流处理文本文件可能出现乱码)
输入Input:读取
FileInputStream:
//总结:1.File类的实例化
//2流的实例化
//3.读入的操作
//4.资源的关闭
package workz;
import java.io.*;
/**
将 “Hello World !”写入本地文件 hello.txt 文件中,再将其读出并打印在控制台
*/
public class IoHello {
public static void main(String[] args) {
Output();
Input();
}
static void Input(){
File file = new File("D:"+File.separator+"Hello.txt");//file文件获取路径
if(file.exists()){
FileInputStream Int = null;
try {
Int = new FileInputStream( file );//输入流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
byte[]Put=new byte[(int)file.length()];
Int.read( Put );//读取到数组中
String s = new String( Put );//把数组转化成字符串
System.out.println(s);//打印
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
Int.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出Output:写入
FileOutStream:
static void Output(){
File file = new File("D:"+File.separator+"Hello.txt");//file文件获取路径
if(file.exists()){
String string="Hello World";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream( file);//从程序到磁盘输出流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileOutputStream.write( string.getBytes() );//把字符串写到文本中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//图片拷贝
package text;
import java.io.*;
public class PhotosCopy {
public static void main(String[] args) {
phonsCopy();
}
static void phonsCopy() {
File start = new File( "01.jpg" );
File end = new File( "02.jpg" );
FileInputStream R = null;
try {
R = new FileInputStream( start );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileOutputStream W = null;
try {
W = new FileOutputStream( end );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buf=new byte[(char)start.length()];
try {
R.read( buf );
} catch (IOException e) {
e.printStackTrace();
}
try {
W.write( buf );
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
R.close();
W.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流: | (提高读取,写入速度,原因:提供了一个缓冲区) | ||
---|---|---|---|
BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
步骤:(伪代码)
1.造文件
File srcF=new File("01.jpg");
File destF=new File("01.jpg");
2.造节点流
FileInputStream fis=new FileInputStream(srcF);
FileOutputStream fos=new FileOutputStream(destF);
3.造缓冲流
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
4.赋值读取,写入
byte[]buffer=new byte[srcF.length()];
int len;
while((len= bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
5.资源关闭:先关外部流,再关内部流。
if(bos !=null){
bos.close();
}
if(bis !=null){
bis.close();
}
(关闭外层流后,内层流也会关闭,所以可以不手动关闭内部流)
转换流:
InputStreamReader:输入的字节流转换为字符流
OutPutStreamWriter:输出字节流转换成字节流
package characterset;
import java.io.*;
/*
*/
public class IoText {
public static void main(String[] args) {
OutPrint();
IntPut();
}
static void OutPrint(){
File file = new File( "D:"+File.separator+"Io"+File.separator+"veryGood3.txt ");
if (file.exists()){
FileOutputStream out=null;
OutputStreamWriter Writer=null;
try {
out=new FileOutputStream( file );//字节流
Writer=new OutputStreamWriter( out ,"utf-8");//转字符流
Writer.write( "我是王" );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (Writer!=null){
try {
Writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (Writer!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}else {
try {//不存在则创建
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void IntPut(){
File file = new File( "D:"+File.separator+"Io"+File.separator+"veryGood3.txt ");
FileInputStream Int=null;
InputStreamReader reader=null;
try {
Int=new FileInputStream( file );//字节流
reader=new InputStreamReader( Int );//字符流
char[] chars = new char[(char) file.length()];
reader.read(chars);
System.out.println( new String( chars ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
if (reader!=null){
try {
Int.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
数据流(序列化反序列化):DataInputStream和DataOutputStream(不常用了解即可)
作用:读取或写出基本数据类型的变量或字符串。
package serialize;
import java.io.Serializable;
import java.util.StringJoiner;
/**
* 将一个学生集合以序列化的形式存储在磁盘文件中,存入户将其读出并打印其中学生的信息
*/
public class Student implements Serializable {//序列化对象
private String name;
private int age;
private String sex;
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return new StringJoiner( ", ", " ", " " )
.add( "姓名:" + name + " " )
.add( "年龄:" + age )
.add( "性别:" + sex + " " )
.toString();
}
}
--
package serialize;
import java.util.HashSet;
//将一个学生集合以序列化的形式存储在磁盘文件中,存入户将其读出并打印其中学生的信息
public class Set {//存入集合
HashSet<Student> students = new HashSet<>();
void add(){
students.add( new Student( "王广宇",18,"男" ) );
students.add( new Student( "王大宇",19,"男" ));
}
}
--
package serialize;
import java.io.*;
import java.util.HashSet;
/*
步骤: 1.确定源目标
2.创建Io对象
3.实例化对象
4.具体实现操作
5.关闭流
*/
public class SerializeText {
public static void main(String[] args) {
Set set = new Set();
HashSet<Student> students = set.students;
set.add();
serialize( students );
deserialize();
}
static void serialize(HashSet<Student> student) {//输入 写
FileOutputStream file = null;//源目标
try {
file = new FileOutputStream("D:"+ File.separator+"Student.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectOutputStream OutPrint = null;//创建Io对象
try {
OutPrint = new ObjectOutputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
OutPrint.writeObject( student );//写入
} catch (IOException e) {
e.printStackTrace();
}finally {
if (OutPrint!=null){
try {
OutPrint.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
if (file!=null){
try {
file.close();//关闭流(先打开的先关闭)
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static void deserialize() {
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream( "D:" + File.separator + "Student.txt" );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectInputStream stream = null;
try {
stream = new ObjectInputStream( fileInput );
} catch (IOException e) {
e.printStackTrace();
}
Object readObject = null;
try {
readObject = stream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (stream != null) {
try {
stream.close();//关闭流(先打开的先关闭)
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInput != null) {
try {
fileInput.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println( readObject );
}
}
}
补充: