io中的超类概述:
通过API1.8我们可以看到io包中流存在5大超类(还有其他类和接口),即以下5大类:
File (文件及目录路径名抽象类)
InputStream (字节输入流抽象类)
OutputStream (字节输出流抽象类)
Reader (读取字符流的抽象类)
Write (写入字符流的抽象类)
下面我们逐个玩玩。
File:
玩File就不得不提他们两个了,虽然是两个被final修饰的常量(IoTest中被注释掉的两行代码),但是很重要,因为他两个家伙的存在才可以使你的代码跨平台而不出问题。
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class IoTest {
public static void main(String args[])throws IOException{
//public static final String separator;
//public static final String pathSeparatorChar;
System.out.println(File.separator);//结果是“\"
System.out.println(File.pathSeparatorChar);//结果是";"
//文件超类演示----创建文件
File file = new File("D:"+File.separator+"study.txt");
try {
file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
//文件超类演示----列出文件
String fileName="E:"+File.separator;
File file1=new File(fileName);
String[] str=file1 .list();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
//文件超类演示----使用RandomAccessFile写入文件
RandomAccessFile rda=new RandomAccessFile(file,"rw");
rda.writeBytes("study java");
rda.writeInt(520);
rda.writeBoolean(true);
rda.writeChar('A');
rda.writeFloat(13.14f);
rda.writeDouble(520.1314);
rda.close();
//结果:study java AAR=q@€Aqu?
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class IoTest {
public static void main(String args[])throws IOException{
//public static final String separator;
//public static final String pathSeparatorChar;
System.out.println(File.separator);//结果是“\"
System.out.println(File.pathSeparatorChar);//结果是";"
//文件超类演示----创建文件
File file = new File("D:"+File.separator+"study.txt");
try {
file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
//文件超类演示----列出文件
String fileName="E:"+File.separator;
File file1=new File(fileName);
String[] str=file1 .list();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
//文件超类演示----使用RandomAccessFile写入文件
RandomAccessFile rda=new RandomAccessFile(file,"rw");
rda.writeBytes("study java");
}
结果:
File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。File的玩法就演示这么多吧,其他的你们自己来
(更多内容请移步微信公众号和微信小程序“每天学Java”)
OutputStream:
public class IoTest {
public static void main(String args[])throws IOException{//字节流写入
String fileName1 = "D:"+File.separator+"study.txt";
File f = new File(fileName1);
OutputStream outputStream = new FileOutputStream(f);
String str1="坚持不一定成功,但放弃一定会失败";
byte [] b = str1.getBytes();
outputStream.write(b);
outputStream.close();
}
}
结果:
OutputStream 是所有的输出字节流的父类,它是一个抽象类。ByteArrayOutputStream、FileOutputStream 是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,ObjectOutputStream 和所有FilterOutputStream 的子类都是装饰流。这些细致东西后边我们仔细探讨。
InputStream:
public class IoTest {
public static void main(String args[])throws IOException{
//字节流写入
String fileName1 = "D:"+File.separator+"study.txt";
File f = new File(fileName1);
//字节流读取
InputStream in=new FileInputStream(f);
byte[] b3=new byte[(int)f.length()];
in.read(b3);
in.close();
System.out.println(new String(b3));
}
}
结果:
InputStream 是所有的输入字节流的父类,它是一个抽象类。ByteArrayInputStream、StringBufferInputStream、FileInputStream是三种基本的介流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。
Writer:
public class IoTest {
public static void main(String args[])throws IOException{
//字符流写入
File f3 = new File(fileName1);
Writer writer= new FileWriter(f3,true);
str1 ="你的未来你做主";
writer.write(str1);
writer.close();
}
}
结果:
Writer是所有的输出字符流的父类,它是一个抽象类。CharArrayWriterStringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。PipedWriter 是向与其它线程共用的管道中写入数据,BufferedWriter 是一个装饰器为Writer 提供缓冲功能。PrintWriter 和PrintStream 极其类似,功能和使用也非常相似。OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类。功能和使用和OutputStream 极其类似。
Reader:
public class IoTest {
public static void main(String args[])throws IOException{
String fileName1 = "D:"+File.separator+"study.txt";
//字符流读取
File f4=new File(fileName1);
char[] ch=new char[100];
Reader read=new FileReader(f4);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为 :"+new String(ch,0,count));
}
}
结果:
Reader 是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader 是两种基本的介质流,它们分别将Char 数组、String中读取数据。
PipedReader 是从与其它线程共用的管道中读取数据。BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。
FilterReader 是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增加一个行号。
InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader 可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。
缓冲流演示:
public class IoTest {
public static void main(String args[])throws IOException{
//缓冲流演示
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str5 = null;
System.out.println("请输入内容");
try{
str5 = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你输入的内容是:" + str5);
}
}
结果:
Scanner类的使用
public class IoTest {
public static void main(String args[])throws IOException{
//Scanner类使用
File file6 = new File("D:" + File.separator + "study.txt");
Scanner sca = null;
try{
sca = new Scanner(file6);
}catch(FileNotFoundException e){
e.printStackTrace();
}
String str6 = sca.next();
System.out.println("从文件中读取的内容是:" + str6);
}
}
结果:
文件压缩演示:
public class IoTest {
public static void main(String args[])throws IOException{
//文件压缩
File file7 = new File("D:" + File.separator + "study.txt");
File zipFile = new File("D:" + File.separator + "study.zip");
InputStream input7 = new FileInputStream(file7);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
zipOut.setComment("study");
int temp7 = 0;
while((temp7 = input7.read()) != -1){
zipOut.write(temp7);
}
input7.close();
zipOut.close();
}
}
结果:
总结:
1、字节流和字符流的区别
实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的时候是会用到缓冲区的,是通过缓冲区来操作文件的。
如果大家有兴趣想知道字符流和字节流在缓冲区这一概念的使用上到底有啥区别的话,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是不妨在使用完流之后不去关闭流,这时候你就会发现字节流和字符流的区别了。使用字符流是需要刷新缓冲区的。