JavaIO
-BIO(阻塞式IO)、基于抽象类的
Java.IO包下
NIO:非阻塞式IO
核心掌握五个类(File、OutPutStream、InPutStream、Reader、Writer)+一个接口(Serializable)
1、File文件操作
既可以描述具体文件也可以描述文件夹
File是唯一一个与文件本身操作(创建、删除、取得信息)有关的程序类。
产生File对象:
public File(String pathname):根据文件的绝对路径来产生file对象
public File(URI uri):根据网络产生File对象
1.1常用操作方法
创建新文件:
public boolean createNewFile() throws IOException(强制进行异常处理)
判断文件是否存在:
public boolean exists()
删除文件
public boolean delete()
文件 分隔符File.separator
例:
public class FileTest {
public static void main(String[] args) {
//1.取得File对象
File file = new File("F:" + File.separator + "TestIO.java");
if (file.exists()) {
file.delete();
} else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2目录操作
取得父路径的File对象
public File getParentFile()
取得父路径的目录
public String getParent()
创建多级父路径(一次性创建多级不存在的父路径)
public boolean mkdirs()
例:
public class FileTest {
public static void main(String[] args) {
//1.取得File对象
File file = new File("F:" + File.separator + "TestIO"+File.separator+"com"+File.separator+"java"+File.separator+"Test.java");
//判断父路径是否存在,不存在创建多级父路径
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
//3.判断文件是否存在,不存在则创建文件
if(file.exists()){
System.out.println("文件已存在");
}else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.3取得文件信息
判断File对象是否是文件
public boolean isFile()
if(file.exists()&&file.isFile()){
//两者都满足才说明文件缺失存在
判断File对象是否是文件夹
public boolean isDirectory()
取得文件大小
public long length()
取得上次修改时间
public long lastModified()
public class FileTest2 {
public static void main(String[] args) {
//1.取得File对象
File file = new File("F:" + File.separator + "TestIO"+File.separator+"com"+File.separator+"java"+File.separator+"Test.java");
//判断父路径是否存在,不存在创建多级父路径
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
//3.判断文件是否存在,不存在则创建文件
if(file.exists()&&file.isFile()){
System.out.println(file.length());
Date date = new Date(file.lastModified());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-mm-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(date));
}else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
列出一个目录的全部组成:
public File[] listFiles()
public class FileTest3 {
public static void main(String[] args) {
File file = new File("F:"+File.separator+"work");
//1.判断file对象确实存在且是一个文件夹
//将IO操作放在子线程中进行,IO操作不影响主线程操作
new Thread(() ->{
long start = System.currentTimeMillis();
listAllFiles(file);
long end = System.currentTimeMillis();
System.out.println("文件遍历结束,共耗时"+(end-start)+"ms");
}).start();
}
public static void listAllFiles(File file) {
if ( file.isFile()) {
System.out.println(file);
} else {
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
listAllFiles(file1);
}
}
}
}
}
2.字节流与字符流
java.io包分为两类:输入流与输出流
1、字节(byte)流:原生操作,无需转换,可以处理文本文件、图像、音乐、视频等资源
InputStream、OutputStream
2、字符(char)流:经过处理后的操作,而字符只用于处理中文文本。
Reader、Writer
流媒体文件:视频和音频
3、流模型的操作流程
1)取得终端对象
2)根据终端对象取得输入输出流
3)根据输入输出流进行数据的读取与写入
4)关闭流
IO操作属于资源处理,所有资源处理(IO操作,数据库操作、网络操作)在使用后一定要关闭。
4、字节输出流OutputStream
public abstract class OutputStream implements Closeable, Flushable
OutputStream类实现了Closeable,Flushable两个接口,这两个接口中的方法:
- Closeable: public void close() throws IOException; //关闭流方法
- Flushable: public void flflush() throws IOException; //刷新缓冲区
4.1OutputStream三个核心方法:
**void write(byte b[]) throws IOException:将给定的字节数组内容全部输出:
void write(byte b[], int off, int len) throws IOException:将给定的字节数组以off位置开始输出len长度后停止输出
abstract void write(int b) throws IOException:输出单个字节
例:
public class OutputStreamText1 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\TestIO.txt");
//2、取得指定文件的输出流
OutputStream out = new FileOutputStream(file);
//1.进行数据的输出
String str = "hello";
out.write(str.getBytes());
//4.关闭流
out.close();
}
}
注意:
1)使用OutPutStream输出数据时,若指定的文件不存在,FileOutputStream会自动创建文件(不包含创建目录,目录必须手工创建)。
2)使用FileOutputStream输出内容时,默认是文件内容的覆盖操作。
3)若要进行文件内容的追加,使用如下构造方法:
public FileOutputStream(File file ,boolean append)
4)JDK1.7追加AutoCloseable自动关闭接口,要使用此接口,必须使用try-catch块(不推荐使用该方法,推荐close()显示关闭)
class CloseTest implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("自动调用close方法");
}
public void fun(){
System.out.println("普通方法");
}
}
public class CloseableTest {
public static void main(String[] args) {
try(CloseTest closeTest = new CloseTest()){
closeTest.fun();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.2 字节输入流InputStream
public abstract class InputStream implements Closeable
public int read(byte b[]) throws IOException.最常用方法
将读取的内容放入字节数组中
返回值有如下三种情况:
I.返回b.length
未读取的数据 > 存放的缓存区大小,返回字节数组大小
II.返回大于0的整数,此整数小于b.length
未读取的数据 < 存放的缓冲区大小,返回剩余数据大小
III.返回-1 --终止标记
此时数据已经读取完毕,返回-1
/**
* Created with IntelliJ IDEA
*
* @Description: 输入流练习
* @Author: zhen
* @Date: 2019/8/6
* @Time: 11:52
*/
public class ReadTest {
public static void main(String[] args) throws IOException {
//1.取得终端对象
File file = new File("F:\\TestIO.txt");
//2.取得相应输入流
InputStream inputStream = new FileInputStream(file);
//3.进行数据读取
byte[] data = new byte[1024]; //用于存放返回值
int len = inputStream.read(data);
System.out.println(new String(data,0,len));
inputStream.close();
}
}
2.3字符输出流Writer --适用于处理中文文本
public abstract class Writer implements Appendable, Closeable, Flushable
字符流可以直接支持字符串的输出
public class OutputStreamText1 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\TestIO.txt");
//2、取得指定文件的输出流
Writer writer = new FileWriter(file);
//1.进行数据的输出
Writetr.write("hello");
//4.关闭流
out.close();
}
}
字符流若未关闭,数据存放在缓冲区存放,不会输出到目标终端。要想将数据输出,要么将输出流关闭,要么使用flush()强制刷新缓冲区。而字节流不存在缓冲区,数据会直接被存放到文件中。
2.4字符输入流 Reader
public int read(char cbuf[]) throws IOException
3.转换流(可以将字节流-> 字符流)
OutoutStreamWriter(字节输出流 -> 字符输出流)
InputStreamReader(字节输入流 -> 字符输入流)
字符流的具体子类大都是通过转换流将字节流转为字符流(FileWriter继承转换流)
public class OutputStreamText1 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\TestIO.txt");
//2、取得指定文件的输出流
Writer out = new OutputStreamWriter(new FileOutputStream(file));
//1.进行数据的输出
out.write("hello");
//4.关闭流
out.close();
}
}
4.(重要练习!!)文件拷贝
public class FileCopy {
public static void main(String[] args) throws IOException {
String sourceFilePath = "C:"+File.separator+"Users"+File.separator+"LENOVO\\Desktop\\Redis-like.pdf";
String destFilePath = "F:\\Redis.pdf";
copyFile(sourceFilePath,destFilePath);
}
public static void copyFile(String sourceFilepath,String destFilepath) throws IOException {
//1.取得源文件与目标文件的File对象
File sourceFile = new File(sourceFilepath);
File destFile = new File(destFilepath);
//2.取得输入输出流
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
//3.数据输入输出
int len = 0;
//数据还未读取完毕
long start = System.currentTimeMillis();
//开一个1024的缓冲区,先将数据存放到缓冲区中再一次性读取,增加读取效率
byte[] data = new byte[1024];
while((len = in.read(data))!= -1){
out.write(data);
}
long end = System.currentTimeMillis();
System.out.println("耗时"+(end - start)+"ms");
}
}
5.字符编码—就用UTF-8
1)GBK、GB2312:
GBL包含简体与繁体中文,GB2312只包含简体中文
2)UNICODE:
Java提供的16进制编码,可以描述世界上任意语言,但是编码进制太高,编码体积较大。
3)ISO-8859-1:国际通用编码,不支持中文,浏览器默认编码
4)UTF编码:结合UNICODE与ISO-88559-1,最常采用的是UTF-8编码。
乱码产生原因:
(95%)编解码不一致
(5%)由于数据丢失造成的乱码,因为我们用的是TCP可靠网络传输,所以一般不会出现这种情况。
6.内存流(以内存为终端的输入输出流)
字节内存流
ByteArrayInputStream、ByteArrayOutputStream
//将制定的字节数组内容存放到内存中。
public ByteArrayInputStream(byte buf[])
//内存输出流的构造方法,因为只存放到内存中,所以没有参数
public ByteArrayOutputStream()
字符内存流
CharArrayReader、CharArrayWriter
public class ArrayStreamTest {
public static void main(String[] args) throws IOException {
String str = "hello world";
//1.取得终端对象以及取得输入输出流
ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
//2.数据的读取与写入
int len = 0;
while((len = byteArrayInputStream.read()) != -1){
byteArrayOutputStream.write(Character.toUpperCase(len));
}
//3.直接输出内存内容
System.out.println(byteArrayOutputStream);
//4.关闭
byteArrayInputStream.close();
byteArrayOutputStream.close();
}
}
6.打印流(输出流的强化版本)
字节打印流:printStream
字符打印流:printWrite
打印流的设计属于装饰设计模式 – 基于抽象类
特点:核心依然是某个类(OutputStream提供的Write())的功能,但是为了得到更好的操作效果,让其支持的功能更多一些,使用装饰类(printStream)
优点:很容易更换装饰类来达到不同的操作效果。
缺点:造成类结构复杂
class PrintUtil {
private OutputStream out ;
// 由外部传入要输出的目标终端
public PrintUtil(OutputStream out) {
this.out = out ;
}
// 核心功能就一个
public void print(String str) {
try {
this.out.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void println(String str) {
this.print(str+"\n");
}
public void print(int data) {
this.print(String.valueOf(data));
}
public void println(int data) {
this.println(String.valueOf(data));
}
public void print(double data) {
this.print(String.valueOf(data));
}
public void println(double data) {
this.println(String.valueOf(data));
}
}
public class TestPrint {
public static void main(String[] args) throws Exception {
PrintUtil printUtil = new PrintUtil(new FileOutputStream(new
File("/Users/yuisama/Desktop/test.txt"))) ;
printUtil.print("姓名:") ;
printUtil.println("yuisama") ;
printUtil.print("年龄:") ;
printUtil.println(27) ;
printUtil.print("工资: ") ;
printUtil.println(0.000000000000001);
}
}
7.System类对于IO的支持
标准输出(显示屏):System.out
标准输入(键盘):System.in
错误输出:System.err
public class TestPrint {
public static void main(String[] args) throws Exception {
try {
Integer.parseInt("abc") ;
} catch (Exception e) {
System.out.println(e) ;
System.err.println(e) ;
}
}
}
7.1系统输出
系统提供的out(输出到显示器,颜色为黑色)与err(输出到显示器,颜色为红色)对象,均是printStream的对象
以后输出采用日志(Log)–格式化输出
7.2系统输入
8.两种输入流
8.1BufferedReader
readline()直接读取一行输入,默认以回车换行
public class TestPrint {
public static void main(String[] args) throws Exception {
//通过转换流将字节流对象转换为字符流传给BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;
System.out.println(“请输入信息 :”) ;
// 默认的换行模式是BufferedReader的最大缺点
String str = buf.readLine() ; // 只能使用回车换行
System.out.println(“输入信息为:” + str );
}
}
8.2java.util.Scanner
hasNextXX():判断是否有指定类型数据输入
nextXX():获取指定类型数据
useDelimiter(”指定分隔符“)
支持正则表达式
File、prinStream、Scanner