手机网站建设林肖,云南高端网站建设,网络开发部是做什么的,建设银行网站号IO
把电脑硬盘中的数据读到程序中,称为输入,进行数据的read操作
把程序往外部设备写数据,称为输出,进行数据的write操作
File类
一个File对象可以表示计算机硬盘上的一个文件或目录(文件夹)
可以获取文件信息,创建文件,删除文件
但是不能对文件中的数据进行读写操作
一些…IO
把电脑硬盘中的数据读到程序中,称为输入,进行数据的read操作
把程序往外部设备写数据,称为输出,进行数据的write操作
File类
一个File对象可以表示计算机硬盘上的一个文件或目录(文件夹)
可以获取文件信息,创建文件,删除文件
但是不能对文件中的数据进行读写操作
一些File类的方法
public class FileDemo {public static void main(String[] args) {
/*File类中构造方法*//*String p E:/;File f1 new File(p,demo.txt);File f2 new File(p,demo.txt);*/
/*File pf new File(E:);File f2 new File(pf,demo.txt);*/
File f new File(E:/demo.txt);System.out.println(f.canExecute());// 文件是否可以被执行System.out.println(f.canRead());// 是否可以读取System.out.println(f.canWrite());// 是否可以写
System.out.println(---------------------------);System.out.println(f.exists());// 文件是否存在System.out.println(f.getAbsolutePath());//获得文件绝对地址
System.out.println(f.getName());// 获取文件名字System.out.println(f.getParent());// 获取父级地址
System.out.println(f.isAbsolute());//是否为绝对路径System.out.println(---------------------------);System.out.println(f.isDirectory());// 是否是文件夹System.out.println(f.isFile());// 是否是文件System.out.println(f.isHidden());// 是否隐藏文件
System.out.println(new Date(f.lastModified()));//文件最后一次修改的时间System.out.println(f.length());//文件内容长度 以字节为单位
}
}
流的分类
按照流(java提供的读写文件的类)的读写单位划分为
字节流每次读写是以字节为单位(计算机中的所有数据存储都是字节为单位) 可以读取任意文件(视频,音频...)
字符流每次都写是以字符为单位 只能读取纯文本文件(txt,java,html)
字节流
输入字节流InputStream
FileInputStream就是节点流 直接负责对接文件,对文件进行读写操作 输出字节流OutputStream
FileOutputStream
public class StreamDemo1 {
public static void main(String[] args) throws IOException {
//创建一个输入字节流对象 并为其指定要读的文件FileInputStream in new FileInputStream(E:/demo.txt);//输入的源文件不存在,会报错FileOutputStream out new FileOutputStream(D:/demo.txt);//输出时文件不存在是可以自动创建//in.read() 每次读到一个字节数据 并返回, 直到读完后返回-1.int b 0;while ((b in.read()) ! -1) {out.write(b);}in.close();out.close();//关闭流通道 释放文件}
}
public class StreamDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream in new FileInputStream(E:/feige.exe);FileOutputStream out new FileOutputStream(E:/demo.exe);//read() write(int b) 每次只能读入 写出一个字节 效率低, 读写次数多//in.read(b); 每次读一个byte数组个字节内容,返回实际向数组装入的字节数量 读完也是返回-1byte[] b new byte[1024];int size0;// 每次最多读取b.length个字节数据为字节数组while((sizein.read(b))!-1){System.out.println(size);//每次向外写出一个byte数组个字节,从第0个开始,写size个,效率高out.write(b,0,size);}in.close();out.close();}
}
根据封装类型不同流分为
节点流 封装的是某种特定的数据源,如文件、字符串、字符串数组等
FileInputStream就是节点流 直接负责对接文件,对文件进行读写操作
处理流封装的是其他流对象,处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法
public class StreamDemo4 {
public static void main(String[] args) throws IOException {
//节点流 直接负责数据读和写FileInputStream in new FileInputStream(E:/feige.exe);FileOutputStream out new FileOutputStream(E:/demo.exe);
//处理流/包装流/缓存流(带缓冲区)BufferedInputStream bin new BufferedInputStream(in);BufferedOutputStream bout new BufferedOutputStream(out);
byte [] b new byte[1024];int size 0;while((sizebin.read())!-1){bout.write(b,0,size);}
bin.close();bout.flush();//刷新缓存区bout.close();
}
}
字符流
字符流只能读纯文本文件
输入字符流
Reader
InputStreamReader
输入转换流,可以把原始字节结合 编码转为字符
FileReader 可以读入一个字符
输出字符流
Writer
OutputStreamWriter
将字符转为字节
FileWriter 可以写出一个字符
FileReader reader new FileReader(E:/demo.txt);
FileWriter writer new FileWriter(E:/demo1.txt);
char[] cs new char[10];
int size 0;
// reader.read();// 一次读到一个字符编码
while((sizereader.read(cs))!-1){System.out.println(size);writer.write(cs,0,size);
}
reader.close();
writer.close(); //节点流 直接包含数据
FileReader reader new FileReader(E:/demo.txt);
FileWriter writer new FileWriter(E:/demo2.txt,true);//后面输出不会覆盖前面的,会保留前面的内容
//字符处理流BufferedReader breader new BufferedReader(reader);BufferedWriter bwriter new BufferedWriter(writer);//readLine() 一次读一行数据 一次缓冲一行数据String line null;while((linebreader.readLine())!null){bwriter.write(line);//一次写出一行数据bwriter.newLine();//插入一个换行符}
breader.close();bwriter.flush();//缓冲流关闭前需要刷新bwriter.close();
字符处理流这里我们可以一行一行的读和写,这样加大了读写操作的效率
对象输入流和对象输出流
对象---指的是java程序运行时产生的对象
将程序运行时,创建的对象,输出到一个文件中
为什么要将运行时的对象输出到文中
为了实现数据 持久保存 有时,服务器要关闭(程序结束运行),结束运行时,将一些保存下来
对象输出--对象序列化
对象输入流 将文件中存储的对象信息 在输入到程序中--对象反序列化(java中创建对象的方式之一)
java中创建对象 不仅仅只有new一种 对象反序列化 反射 创建一个User类 并且实现Serializable接口,如果不实现的话,那么此类对象信息将无法被输出到文件中
//需要被序列化的类,必须实现Serializable接口,会为类生成一个序列化id号,是唯一与类对应
public class User implements Serializable {
private Integer id;private String name;
public User(Integer id, String name) {this.id id;this.name name;}
public Integer getId() {return id;}
public void setId(Integer id) {this.id id;}
public String getName() {return name;}
public void setName(String name) {this.name name;}
} public class ObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/* Date date new Date();String s new String(abc);User user new User(1, jim);
// 讲对象信息输出到文件中 称为对象序列化 对象的类必须实现java.io.Serializable接口FileOutputStream out new FileOutputStream(E:/obj.txt);ObjectOutputStream objectOut new ObjectOutputStream(out);objectOut.writeObject(date);objectOut.writeObject(s);objectOut.writeObject(user);objectOut.flush();objectOut.close();*/
// 对象反序列化 将文件中的信息输入到程序 再创建一个对象FileInputStream in new FileInputStream(E:/obj.txt);ObjectInputStream objIn new ObjectInputStream(in);Date date (Date) objIn.readObject();String s (String) objIn.readObject();User user (User) objIn.readObject();
System.out.println(date);System.out.println(s);System.out.println(user.getId():user.getName());}
}