文件操作

*  Input  /  Out    输入/ 输出

*  java.io  

File

RandomAccessFile

InputStream / OutputStream                 字节流抽象父类

     FileInputStream / FileOutputStream           接文件

     BufferedInputStream / BufferedOutputStream   缓冲,提高单字节读写效率

      DataInputStream / DataOutputStream固定字节格式,writeInt(),writeDouble,writeUTF()

     PrintStream                              任何类型转成字符串 print(),println()

     ByteArrayInputStream / ByteArrayOutputStream 接byte[] 数组 toByteArray()

      ObjectInputStream / ObjectOutputStream       序列化 writeObject()  readObject()

    

     Reader / Writer                            字符流抽象父类

     InputStreamReader / OutputStreamWriter       编码转换流

     FileReader / FileWriter                      “转换流”接“文件字节流”

     BufferedReader / BufferedWriter              字符缓冲,readLine()

     PrintWriter                                与PrintStream相同

 

File

java.io.File

========================================

* 封装磁盘路径的对象

* 可以封装不存在的路径

* 可以是文件,也可以是目录

* 可以通过File类在程序中操作硬盘上的文件和目录

* File类只用于表示文件(目录)的信息(名称、大小等),不能访问文件的内容

创建对象

------------------------------------------------------------------------------------

1. File f= new File(“d:/a/b.txt”);

2. File f =new File("d:/a/", "b.txt");

方法

---------------------------------------------------------------------------------

*)  文件或目录的属性

boolean    canRead()            是否可以读取

boolean    canWrite()            是否可以写入

boolean    canExecute()          是否可执行

boolean    isHidden()            是否是隐藏文件

boolean    exists()               是否存在

String     getAbsolutePath()      完整路径

String     getName()            获取文件名

String     getParent()            获取父目录

long      lastModified()         最后修改时间,毫秒值

long      length()              获取文件长度

boolean    isDirectory()          是否是文件夹

boolean    isFile()               是否是文件

long      getTotalSpace()        空间总大小

long      getFreeSpace()        可用空间

*)  文件或目录操作

boolean    createNewFile()      创建文件

boolean    delete()             删除

boolean    mkdir()             创建此抽象路径名指定的目录(一层)

boolean    mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录(多层)

boolean    renameTo(File)   重新命名此抽象路径名表示的文件,  移动文件

File       File .createTempFile(前缀,后缀)   在系统临时目录创建临时文件

    *) 创建删除重命名方法返回 boolean,表示操作是否成功

*)  文件列表

String[]  list()        返回 String[],包含子文件、子目录名称

File[]    listFiles()    返回File[],包含子文件、子目录的File对象

File[]    listFiles(FilenameFilter)     

File[]    listFiles(Filefilter)    只列出符合顾虑条件的文件目录    

参数:外接的过滤器

File dir= new File("c:/windows");

String[] names=dir.list(new FilenameFilter() {

@Override

public boolean accept(File dir, String name) {

return name.toLowerCase().endsWith(".exe");

}

});

File[] files=dir.listFiles(new FileFilter() {

@Override

public boolean accept(File f) {

if(f.isDirectory()) return false;

return f.length()>=1024*1024;

}

});

 

RandomAccessFile

Java.io.RandomAccessFile

= = = == = = == = = == = = == = = == = = == = = == = = == = = == = = == = = == = = == = = =

*)用来读写文件 (读-输入,写-输出)

*)将文件中的字节数据,当做数组,用下标值访问字节数据

创建对象

----------------------------------------------------------------------------------------------------------

1. RandomAccessFile raf = new RandomAccessFile(文件,”r”).

2. RandomAccessFile raf = new RandomAccessFile(文件,”rw”).

方法

--------------------------------------------------------------------------------------------------------------

返回

方法名

描述

void

write(int b)

输出 int 四个字节中的末尾一个字节

void

write(byte[] a)

输出(写入)数组中全部字节

void

write(byte[],int from,int n)

输出数组中从from开始的n个字节值

int

read()

读取一个字节,然后补30字节,变成int类型

[4]-->[1][2][3][4]  读完之后再读取,会返回-1

int

read(byte[] buff)

根据数组的长度,读取一批字节值,并放入数组,返回读取并放入数组的字节数量,读完之后再读取,返回-1

void

seek(long position)

将下标定位到指定位置

long

getFilePointer()

获得当前下标所在的位置

void

writeInt(int i)

完整输出 int 的四个字节

void

writeDouble(double d)

完整输出 double 的八个字节

void

writeUTF(String s)

先输出两个字节,表示字符串的字节长度;在输出字符串的这些字节值

int

readInt()

读取四个字节转成 int

double

readDouble()

读取八个字节转成 double

String

readUTF()

先读取两个字节,来确定字符串的字节长度;再读取这些字节值,转成String

*) 读取结束再读取,会出现 EOFException  EOFEnd Of File

1单字节读取与批量读取标准格式  代码

 

raf.seek(0); //将下标定位到0位置

//单字节读取标准格式

int b;//保存每一个字节

while((b=raf.read())!=-1){

System.out.println(b);

}

System.out.println("= = = = = = = = = = = = = = = = = ");

//批量读取标准格式

raf.seek(0);

byte[] buff =new byte[6];

int n;//每一批的字节数量

while((n= raf.read(buff))!=-1){

//处理数组中从0位置开始的n

for(int i=0;i<n;i++){

System.out.println(buff[i]);

}

}

raf.close();//释放系统资源

2文件加解密  代码

private static void encript(File path, int key) throws Exception {

/*

 * 1.新建RandomAccessFile d对象赋给raf

 * 2.单字节循环读取,读取的字节值赋给b

 * 3.b key 求异或,结果重新赋给变量b

 * 4.下标定位回到上一个位置  

 * raf.seek(raf.getFilePointer()-1)

 * 5.输出b的值

 * 6.关闭raf

 */

RandomAccessFile raf= new RandomAccessFile(path, "rw");

//1k 2k 3k 4k

//1024 2048 4096 8192

byte[] buff=new byte[8192];

int n;

while((n=raf.read(buff))!=-1){

for(int i=0;i<n;i++){

buff[i] ^= key;

}

raf.seek(raf.getFilePointer()-n);

raf.write(buff,0,n);

}

raf.close();

}

 

Stream

* 将数据读写,抽象成“字节数据在管道中流动”

* 流有方向,分为输入流和输出流

* 只能顺序读写数据,而不能任意跳转位置读写

 

InputStreamOutputStream

* 字节流的抽象父类

* InputStream是所有字节输入流的父类,它是一个抽象类;

*InputStream用于读取输入数据,数据源多种多样:

字节数组、String字符串、文件、网络

* OutputStream是所有字节输出流的父类,它也是一个抽象类

*) 同样,OutputStream也需要向不同的目标输出字节数据

方法

-----------------------------------------------------------------------

OutputStream

返回

方法名

描述

void

write(int b)

输出末尾一个字节

void

write(byte[] buff)

输出(写入)数组中全部字节

void

write(byte[] buff,int from,int n)

输出数组中从from开始的n个字节值

void

flush()

刷出缓存数据

void

close()

关闭此输出流并释放与此流有关的所有系统资源

 

InputStream

返回

方法名

描述

int

read()

读一个字节补三个0字节变成int,读完再读返回-1

int

read(byte[] buff)

读取一批字节放入数组,返回字节数量

int

available()

获得剩余可读取的字节量

void

Close()

关闭此输入流并释放与此流有关的所有系统资源

FileInputStream  FileOutputStream

java.io.FileInputStream / FileOutputStream

= = = = = = = = = = = == = = = = = = = = = = = = = = = = == = = = = == = = = = =

* 直接与文件相接,读写文件数据的流  

创建对象

  ---------------------------------------

    1. FileOutputStream out =new FileOutputStream(文件);

          不论文件是否存在,都会创建一个新的空文件 

    2. FileOutputStream out =new FileOutputStream(文件, true);

          文件不存在会新建文件;文件存在,回向文件末尾追加数据    

*如果目录不存在,会出现异常

附:复制文件  代码

private static void copy(File from, File to)throws Exception {

/*

 * 1. 新建FileInputStream 赋给 in 接文件 from

 * 2. 新建FileOutputStream 赋给out 接文件 to

 * 3. 单字节循环读取,读取的字节赋值给 b

 * 4. 向输出流 out 输出字节值b

 * 5. in.close()  out.close()

 */

FileInputStream in=new FileInputStream(from);

FileOutputStream out = new FileOutputStream(to);

//int b;

//while((b=in.read())!=-1){

//out.write(b);

//}

byte[] buff=new byte[8192];

int n;

while((n=in.read(buff))!=-1){

out.write(buff,0,n);

}

in.close();

out.close();

}

 

高级流、操作流

= = = = = = = = = = = == = = = = = = = = = = = = = = = =

  * 与其他流相接,对数据提供特定的处理功能

  * 可以多个流相接,提供组合功能

  * 操作其他流的流

BufferedInputStream 和BufferedOutputStream

java.io.BufferedInputStream

java.io.BufferedOutputStream

= = = = = = = = = = = == = = = = = = = = = = = = = = =

  * 提供一个内存缓冲区,提高单字节读写效率  

  * 使用缓冲流,读写单个字节,在它内部,做批量读写

  创建对象

  --------------------------------------

    1. BufferedOutputStream out = new BufferedOutputStream(相接的流);

        内部缓冲数组大小: 8192

    2. BufferedOutputStream out = new BufferedOutputStream(相接的流, 缓冲区大小);

        内部缓冲数组大小: 指定的大小

附:复制文件  代码

private static void copy(File from, File to)throws Exception {

BufferedInputStream in=new BufferedInputStream(new FileInputStream(from));

BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(to));

int b;

while((b=in.read())!=-1){

out.write(b);

}

in.close();

out.close();

}

 

DataInputStream 和DataOutputStream

java.io.DataInputStream / DataOutputStream

= = = = = = = = = = = = = = = = = = = = = = = = == = = = = = = = = = = = = == = = = =

* 读写固定字节格式数据

创建对象

-----------------------------------------------------------------------------------------------------

DataOutputStream out = new DataOutputStream(相接的流);

方法

----------------------------------------------------------------------------------------------------

DataOutputStream

---------------------------------------------------------------------------------

writeInt(int i)         输出4个字节

writeDouble(double d)  输出八个字节

. .........

writeUTF(String s)     先输出两个表示字节长度,再输出字符串的字节值

DataInputStream

----------------------------------------------------------------------

readInt()              读四个字节

readDouble()          读八个字节

.....

readUTF()        先读取两个字节确定字节长度,再读取这些字节值转成 String

*)读完再读取,会出现EOFException                   EOF  ---End Of File

 

PrintStream

java.io.PrintStream

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

* 将任何类型数据,专程字符串输出

97         --> "97"

        3.14       --> "3.14"

        true       --> "true"

        Date对象  --> "Tue Aug 25 14:24:06 CST 2015"

创建对象

------------------------------------------------------------------------------------

1. PrintStream out = new PrintStream(相接的流);

2. PrintStream out =new PrintStream(文件);

3. PrintStream out=new PrintStream(文件,字符编码);

方法

---------------------------------------------------------------------------------------

print()

println()      末尾补换行,windows 中换行符:\r\n,其他系统换行符\n

 

 

字符编码 encoding

字符集    charset

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

ASC-II            0127  

ISO-8859-1

   LATIN-1          159255  

   GB2312          7k+  (喆 镕 等汉字无法显示)  

   GBK             20902     \u4e00 \u9fa5  

   BIG5

   KRxxx           以下都表示外国一些字符编码

   JPxxx

   ALxxx  

  Unicode 统一码、万国码

  -----------------------------------------------

    * 包含 70+ 字符

    * 常用字符表,双字节字符

    * 不常用字符,三字节或以上    

    * java char 类型,

      使用的是 Unicode 常用字符表  

    * Unicode 传输码        

        UTF-8           英文,单字节 ,某些字符,双字节,中文,三字节        

        UTF-16Be

        UTF-16Le

        UTF-32

  java 字符编码转换

  --------------------------------------------

    * Unicode --> 其他编码        

        String s = "abc中文";        

        //转成默认编码

        byte[] a = s.getBytes();        

        //转成指定的编码

        byte[] a = s.getBytes("GBK");    

    * 其他编码 --> Unicode        

        //从默认编码转成 Unicode

        String s = new String(a);        

        //从指定编码转成 Unicode

        String s = new String(a, "GBK");

 

Reader Write

java.io.Reader / Writer

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

* 字符流的抽象父类

方法

------------------------------------------------------------------------------

Writer

--------------------------------------------------------------------------------

write(int c)   四个字节中,末尾两个字节是char 字符

write(char[] buff)

write(char[] buff,int from,int n)

write(String s)

Reader

-------------------------------------------------------------------------------------

read()

read(char[] buff)

 

 

InputStreamReader OutpputStreamWriter

java.io.InputStreamReader / OutputStreamWriter

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

* 字符编码转换流

* InputStreamReader    读取其他编码字节值,转成Unicode

* OutputStreamWriter    将内存中 Unicode 编码字符,转成其他编码字节数据输出

创建对象

----------------------------------------------------------------------------------------------------

1. OutputStreamWriter out = new OutputStreamWriter(相接的字节流);//默认编码

2. OutputStreamWriter out = new OutputStreamWriter(相接的字节流,字节编码);

//指定的编码

FileReader 和FileWriter

java.io.FileReader / FileWriter

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

  * 内部是“转换流”接“文件字节流”

  * 不能指定字符编码

  * 用不用都行

  

  创建对象

  --------------------------------

    FileWriter out = new FileWriter(文件);

BufferedReader 和BufferedWriter

java.io.BufferedReader / BufferedWriter

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

  * 字符缓冲,提高单个字符读写效率

  

  创建对象

  ----------------------------------

    BufferedWriter out = new BufferedWriter(字符流);

  

  方法

  ----------------------------------

    BufferedReader

    ------------------------------

      readLine()    读取一行字符串,不包含末尾换行符,        

        读取结束,再读取,返回 null

 

PrintWriter

java.io.PrintWriter

=========================================

  * PrintStream 相同  

  * PrintWriter    可以与“字节流”、"字符流"相接  

  * PrintStream   只能与“字节流”相接

 

 

ObjectInputStream 和ObjectOutputStream

java.io.ObjectInputStream / ObjectOutputStream

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

* 对象序列化、反序列华

* 序列化头部数据

*) 新建 ObjectOutputStream 对象时,会自动输出 4个头部字节值

*) 新建ObjectInputStream 对象时,会自动读取4个头部字节值,并检查是否正确,来确认后面的数据是否序列化数据

* 不序列化的成员变量

*static       属于类,不随对象序列化输出

*transient    临时,不随对象序列化数据持久保存

* 序列化版本号

*) 旧版本数据,不能恢复成新版本的类型

定义格式:

任意访问范围 static final long serialVersionUID = xxxL;

  创建对象

   -----------------------------------

ObjectOutputStream out =  new ObjectOutputStream(相接的流);

  

   方法

   -----------------------------------

     ObjectOutputStream

     --------------------------------

       writeObject(Object obj)        序列化

    

     ObjectInputStream

     --------------------------------

       readObject()               反序列化

 

ByteArrayInputStream 和ByteArrayOutputStream

java.io.ByteArrayInputStream / ByteArrayOutputStream

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

   * byte[] 数组相接,读写数组中的数据  

   创建对象

   -----------------------------------------------------------------------------

     ByteArrayInputStream in = new ByteArrayInputStream(byte[]数组);     

     ByteArrayOutputStream out = new ByteArrayOutputStream();  

   方法

   ---------------------------------------------------------------------------------------

     ByteArrayOutputStream

     -----------------------------------

       toByteArray()        获取内部 byte[] 数组

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值