java基础知识总结5

四、I/O流、File
File——–java.io.File——–文件和目录路径名的抽象表示形式

常用方法:
createNewFile(” “)———创建文件
delete(” “)
exists()———–测试是否存在
getName()———获得文件名

例:
String path = File.separator;//与系统有关的默认名称分隔符
File f = new File(“c:”+path+”c.txt”);
ystem.out.println(f.createNewFile());

例:import java.io.*;
public class FileDemo {
public static void main(String[] args) throws IOException{
/*
File f = new File(“c:\Users\Rainlate\Desktop\123.txt”);//注意\的转义字符
//构建实例不代表创建文件
boolean b =f.createNewFile();//创建文件
f.mkdirs();//创建前不必有父目录
//f.createTempFile(sd,jsp);//使用给定前缀和后缀生成其名称创建空文件
//f.createTempFile(23,txt, null);//使用给定前缀和后缀生成其名称
//boolean d1 = f.delete();//删除此抽象路径名表示的文件或目录。如果此路径名表示一个目录,则该目录必须为空才能删除
System.out.println(f.exists());//是否存在文件
System.out.println(f.getName());//123.txt
String path = File.separator;
File f = new File(“c:”+path+”c.txt”);
System.out.println(f.createNewFile());
String a = File.separator;
String path=”c:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”1”;
File f = new File(path);
File[] files = new listFiles(f);
for(String file:files){
System.out.println(file);
}
*/
File x= new File(“c:\Users\Rainlate\Desktop\1”);
show(x);
}

public static void show(File f){//递归输出源目录下的所有文件
if(f.isDirectory()){//判断是否为目录
File[] files =f.listFiles();
for(int i=0;i
IO流:

第一,确定方向———–输入输出是以程序为中心
第二,确定是字节还是字符【若是字符型文本使用字符流,其他使用字节流】
第三,确定是否需要缓冲【autoFlush、Flush】———-缓冲流是用空间换时间
第四,所有操作必须关闭【在finally关闭】

例1;
import java.io.*;
public class FileInputStreamDemo{//文本复制
public static void main(String[] args) {
String a = File.separator;//与系统有关的默认名称分隔符
FileInputStream fin = null;
//FileOutputStream fo = null;
BufferedOutputStream fo = null;
try{
fin = new FileInputStream(“C:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”123.docx”);
byte[] b = new byte[512];//存储读取数据的缓冲区
long start = System.currentTimeMillis();
int size = fin.read(b,0,b.length);
//fo = new FileOutputStream(“C:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”12.java”);
fo = new BufferedOutputStream (new FileOutputStream(“C:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”12.docx”));
while(size != -1){
fo.write(b,0,size);//文件的实际大小
fo.flush();
size = fin.read(b,0,b.length);
}
long late = System.currentTimeMillis();
System.out.println(late-start);
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
try{
fin.close();//关闭输入流
fo.close();//关闭输出流
}catch (IOException e){
System.exit(1);//异常退出
}
}
}
}

例2:
import java.io.*;
public class ObjectSerializable {
public static void main(String[] args) {
Person p = new Person(32,”huhu”);
String a = File.separator;
ObjectOutputStream ou =null;//序列化
ObjectInputStream oin = null;//反序列化
try{
ou = new ObjectOutputStream(new FileOutputStream(“C:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”1.txt”));
ou.writeObject(p);//对象序列化
ou.flush();

//反序列化
oin = new ObjectInputStream(new FileInputStream(“C:”+a+”Users”+a+”Rainlate”+a+”Desktop”+a+”1.txt”));
Object obj = oin.readObject();
Person as = (Person)obj;
System.out.println(as);
out.flush();
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch(IOException e){
System.out.println(e.getMessage());
}catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}finally{
try{
out.close();
}catch (IOException e){
System.exit(1);
}
}
}
}

class Person implements Serializable {
private int age;
private String name;
public Person(int age,String name){
this.age=age;
this.name=name;
}
public String toString(){
return “name=”+this.name+”,age=”+this.age;
}
}

序列化的版本号(serialVersionUID=1L)?
作用:就是确保了不同版本之间的兼容性,不仅能够向前兼容,还能够向后兼容,即在版本升级时反序列化仍保持对象的唯一性。
它有两种生成方式:
一个是默认的1L,比如:private static final long serialVersionUID = 1L;
一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如: private static final long serialVersionUID = xxxxL
类图结构

┌BufferedReader
├InputStream──FileReader
├StringReader
┌Reader─┤
│ ├PipedReader
│ ├ByteArrayReader
│ └FileReader──PushbackReader
字符流─┤
│ ┌BufferedWriter
│ ├OutputStreamWriter──FileWriter
│ ├PrinterWriter
└Writer─┼StringWriter
├PipedWriter
├CharArrayWriter
└FileWriter

┌FileInputStream
│ ┌BufferedInputStream
├FilterInputStream ──┼DataInputStream
│ └PushbackInputStream
┌InputStream ─┼ObjectInputStream
│ ├PipedInputStream
│ ├SequenceInputStream
│ ├StringBufferInputStream
│ └ByteArrayInoutStream
字节流─┤

│ ┌FileOutputStream
│ │ ┌BufferedOutputStream
│ ├FilterOutputStream ──┼DataOutputStream
└OutputStream ─┤ └PrintStream
├ObjectOutputStream
├PipedOutputStream

猜你喜欢

转载自blog.csdn.net/yao302789/article/details/51004191