IO体系图
File类
- 在java程序中,对磁盘文件进行描述的类
- 通常使用File类的构造方法
File类文件属性方法
static String pathseparator:与系统有关的路径分隔符,为了方便,它被表示为一个字符串(;)
static char pathSeparatorChar;
static String Separator:与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串(\)
static char Separator
package waking.test.io;
import java.io.File;
public class Demo01 {
public static void main(String[] args) {
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println(File.separator);
System.out.println(File.separatorChar);
}
}
常用方法
返回值 |
方法名/描述 |
boolean |
canExecute()测试应用程序是否可以执行此抽象路径名表示的文件 |
boolean |
canRead()测试应用程序是否可以读取此抽象路径名表示的文件 |
boolean |
canWrite()测试应用程序是否可以修改此抽象路径名表示的文件 |
int |
compareTo(File)按字母顺序比较两个抽象路径名 |
boolean |
exists()测试此抽象路径名表示的文件或目录是否存在 |
boolean |
createNewFile()当且仅当不存在具有此路径名指定的文件时,创建文件 |
boolean |
delete()删除此抽象路径名表示的文件或目录 |
File |
getAbsoluteFile()返回此抽象路径名的绝对路径名形式 |
String |
getName()返回由此抽象路径名表示的文件或目录的名称 |
String |
getParent()返回此抽象路径名父路径名字符串,如果此路径名没有指定父目录,则返回null |
String |
getPath()将此抽象路径名装换为一个路径名字符串 |
boolean |
isFile()测试此抽象路径名表示的文件是否是一个标准文件 |
boolean |
isDirectory()测试此抽象路径名表示的文件是否是一个目录 |
boolean |
ishidden()测试此抽象路径名指定的文件是否是一个隐藏文件 |
String[] |
list()返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录 |
boolean |
mkdir()创建此抽象路径名指定的目录 |
boolean |
renameTo(File dest)重新命名此抽象路径名表示的文件 |
File[] |
listFiles()返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录的文件 |
package waking.test.io;
import java.io.File;
public class Demo02 {
public static void main(String[] args) {
File file = new File("E:\\diary\\1909java");
isFile(file);
}
public static void isFile(File f) {
if(f.isFile()) {
System.out.println(f.getPath());
}else {
File[] listFiles = f.listFiles();
for (int i = 0; i < listFiles.length; i++) {
isFile(listFiles[i]);
}
}
}
}
IO流
- 在工作中,经常回去操作磁盘上的资源这个过程中实现了数据的输入和输出操作,磁盘上的文件和内存之间进行交互,数据的交互需要有一个媒介或者管道,把这个媒介或者管道就称为IO流,也被称为输入输出流
流的作用和原理
- 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
- 即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作
IO流的种类
输入流:表示将数据读取到java程序(内存)中使用的流
输出流:表示从java程序(内存)向外传输使用的流
字节流:一次性传输一个字节数据,将数据已字节的形式传输
字符流:一次性传输一个字符数据,将数据以字符的形式传输
节点流:可以从或向一个特定的地方(节点)读写数据
处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写
字节输入流
方法名 |
描述 |
void close() |
关闭此输入流并释放与该流关联的所有系统资源 |
int read() |
从输入流中读取数据的下一个字节 |
int read(byte[] b) |
从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中 |
int read(byte[] b,int off,int len) |
将输入流中最多len个数据字节读入byte数组 |
package waking.test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Demo03 {
public static void main(String[] args) {
InputStream is =null;
try {
is = new FileInputStream(new File("E:\\diary\\1909java\\预习\\javaEE\\Java8.pdf"));
byte[] b = new byte[1024];
int len = -1;
while((len=is.read(b))!=-1) {
System.out.println(new String(b, 0, len));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节输出流
- OutputStream
- 是个抽象类,不能实例化对象
方法名 |
描述 |
void close() |
关闭此输出流并释放与此流有关的所有系统资源 |
void flush() |
刷新此输出流并强制写出所有缓冲的输出字节 |
void write(byte[] b) |
将b.length个字节从指定的byte数组写入此输出流 |
package waking.test.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo04 {
public static void main(String[] args) {
OutputStream os=null;
try {
os = new FileOutputStream(new File("E://a.txt"));
String s = "waking";
os.write(s.getBytes(), 0,s.length());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(os!=null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package waking.test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo05 {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
try {
is=new FileInputStream(new File("E:\\\\diary\\\\1909java\\\\预习\\\\javaEE\\\\Java8.pdf"));
os = new FileOutputStream(new File("E://wakingjava8.pdf"));
byte[] b = new byte[1024*8];
int len = -1;
while((len=is.read(b))!=-1) {
os.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is!=null) {
is.close();
}
if(os!=null) {
os.flush();
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符输入流 Reader
- 是所有字符输入流的父类,为一个抽象类,不能实例化对象,使用它的子类FileReader类
- 常用方法和以上方法类似
- 示例
package waking.test.io;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo06 {
public static void main(String[] args) {
Reader r=null;
try {
r = new FileReader(new File("E://a.txt"));
char[] c = new char[1];
int len = -1;
while((len=r.read(c))!=-1) {
System.out.println(new String(c, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(r!=null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字符输出流Writer
- Writer:是所有字符输出流的父类,为一个抽象类,不能实例化对象,使用它的子类FileWriter
- 常用方法和以上类似就不一一介绍
- 示例
package waking.test.io;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo07 {
public static void main(String[] args) {
Writer w = null;
try {
w= new FileWriter(new File("E://aa.txt"));
String c = "waking to love";
w.write(c.toCharArray(), 0, c.length());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(w!=null) {
w.flush();
w.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节字符转换输入流InputstreamReader
package waking.test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Demo08 {
public static void main(String[] args) {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(new File("E://a.txt")),"utf-8");
char[] c =new char[1024];
int len = -1;
while((len=isr.read(c))!=-1) {
System.out.println(new String(c, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(isr!=null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节字符输出流OutputStreamWriter
package waking.test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Demo09 {
public static void main(String[] args) {
OutputStreamWriter osw = null;
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(new File("E://a.txt")),"utf-8");
osw = new OutputStreamWriter(new FileOutputStream(new File("E://b.txt")),"utf-8");
char[] c =new char[1024*8];
int len = -1;
while((len=isr.read(c))!=-1) {
osw.write(c, 0, len);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(isr!=null) {
isr.close();
}
if(osw!=null) {
osw.flush();
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲输入流BufferedInputStream
- 作用:主要是为了增强基础流的功能而存在,提高了流的工作效率(读写效率)
- 示例
package waking.test.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo10 {
public static void main(String[] args) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("E://a.txt")));
byte[] b =new byte[1024];
int len = -1;
while((len=bis.read(b))!=-1) {
System.out.println(new String(b, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
缓冲输出流BufferedOutputStream
package waking.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo11 {
public static void main(String[] args) {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("E://a.txt")));
bos = new BufferedOutputStream(new FileOutputStream(new File("E://c.txt")));
byte[] b = new byte[1024];
int len =-1;
while((len=bis.read(b))!=-1) {
bos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bos!=null) {
bos.flush();
bos.close();
}
if(bis!=null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 缓冲字符输入输出流和缓冲字节输入输出流类似,区别在于对字符的操作
内存流ByteArrayInputStream
- 将字节写到内存中,是InputStream的子类
- 示例
package waking.test.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Demo12 {
public static void main(String[] args) throws IOException {
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream();
bais = new ByteArrayInputStream("waking".getBytes());
int len = -1;
while((len = bais.read())!=-1) {
char c = (char)len;
baos.write(Character.toUpperCase(c));
}
String bao = baos.toString();
baos.close();
bais.close();
System.out.println(bao);
}
}
标准输入输出流
package waking.test.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Demo13 {
public static void main(String[] args) {
PrintStream ps = null;
try {
ps = new PrintStream(new File("E:/d.txt"));
System.setOut(ps);
System.out.println("waking");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(ps!=null) {
ps.close();
}
}
}
}
package waking.test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream(new File("E://a.txt"));
System.setIn(fis);
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.println(line);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
IO流就介绍到这里,后面还用序列化和反序列化,对象流,RandomAccessFile等,在下一个博客介绍