ioFile
来源:互联网 发布:网络电影演员火不了 编辑:程序博客网 时间:2024/06/07 08:39
一、 FileInputStream
(一) API 1.6.0说明
java.io
类 FileInputStream
java.lang.Object
java.io.InputStream
java.io.FileInputStream
所有已实现的接口: Closeable
public class FileInputStream
extends InputStream
FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
从以下版本开始: JDK1.0
另请参见: File, FileDescriptor, FileOutputStream
(二) Constructor
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(FileDescriptor fdObj)
通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统中某个实际文件的现有连接。
FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
public staticvoidis1() throwsException{
FileInputStream fis=new FileInputStream("fis.txt");
int b;
while((b=fis.read())!=-1){
System.out.println(b);
}
fis.close();
}
public staticvoidis2() throwsException{
FileInputStream fis=new FileInputStream(new File("e:\\fileTest\\1.txt"));
int b;
while((b=fis.read())!=-1){
System.out.println(b);
}
fis.close();
}
(三) Method
1. 读单个字节
int
read()
从此输入流中读取一个数据字节。
public staticvoidis3() throwsException{
FileInputStream fis=new FileInputStream(new File("e:\\fileTest\\1.txt"));
FileOutputStream fos=new FileOutputStream(new File("e:\\fileTest\\2.txt"));
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
2. 读多个字节
int
read(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
public staticvoidis5() throwsException{
FileInputStream fis=new FileInputStream(new File("e:\\fileTest\\1.txt"));
int b;
byte[] bytes=newbyte[1024];
while((b=fis.read(bytes))!=-1){
System.out.print(new String(bytes,0,b));
}
fis.close();
}
二、 FileOutputStream
(一) API1.6.0 说明
java.io
类 FileOutputStream
java.lang.Object
java.io.OutputStreamjava.io.FileOutputStream
所有已实现的接口: Closeable, Flushable
public class FileOutputStream
extends OutputStream
文件输出流是用于将数据写入 File 或FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。
FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。
从以下版本开始: JDK1.0
另请参见: File, FileDescriptor, FileInputStream
(二) Constructor
FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(FileDescriptor fdObj)
创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
public staticvoidos1()throws Exception{
FileOutputStream fos=new FileOutputStream(new File("e:\\fileTest\\os.txt"));
fos.close();
}
常用这种方式
public staticvoidos2()throws Exception{
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\os2.txt");
fos.close();
}
(三) Method
void
write(byte[] b)
将 b.length 个字节从指定 byte 数组写入此文件输出流中。
void
write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
void
write(int b)
将指定字节写入此文件输出流。
1. 非追加,总是重新输出数据。
public staticvoidos3() throwsException{
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\os2.txt");
byte[] bytes=newbyte[]{110,111,112,113,114};
fos.write(bytes);
//fos.write(bytes,2,3);
fos.write(98);
fos.close();
}
2. 构造输出流时,append 参数设为true,在文件尾追加内容。
public staticvoidos3() throwsException{
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\os2.txt",true);
byte[] bytes=newbyte[]{110,111,112,113,114};
fos.write(bytes);
//fos.write(bytes,2,3);
fos.write(98);
fos.close();
}
3. 设置换行
Windows系统:\r\n linux系统:\n 跨系统平台,从系统中获取换行符。
\r\n
public staticvoidos5()throws Exception{
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\os2.txt",true);
byte[] bytes=newbyte[]{110,111,112,113,114};
fos.write(bytes);
//fos.write(bytes,2,3);
fos.write(("\r\n".getBytes()));
fos.close();
}
系统的换行符
public staticvoidos6()throws Exception{
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\os2.txt",true);
byte[] bytes=newbyte[]{110,111,112,113,114};
fos.write(bytes);
//fos.write(bytes,2,3);
fos.write((System.getProperty("line.separator").getBytes()));
fos.close();
}
(四) 流的异常处理模版
问题分析:
1. 如果流创建后,在流的执行操作中,关闭流之前出现异常,那么流的关闭动作将缺失执行,造成资源的浪费。
2. 如果在流还没有创建时,就出现异常,那么在finally关闭流时,将出现nullPointException,所以finally里面要判断。
public staticvoidos7() throwsException{
FileOutputStream fos=null;
try{
fos=new FileOutputStream("e:\\fileTest\\os2.txt",true);
byte[]bytes=newbyte[]{110,111,112,113,114};
fos.write(bytes);
fos.write((System.getProperty("line.separator").getBytes()));
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(fos!=null){
fos.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
三、 BufferedOutputStream
(一) API1.6.0 说明
java.io
类 BufferedOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.BufferedOutputStream
所有已实现的接口: Closeable, Flushable
public class BufferedOutputStream
extends FilterOutputStream
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
从以下版本开始:JDK1.0
(二) ConstructorAndMethod
BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
BufferedOutputStream(OutputStream out, int size)
创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
void
write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。
void
write(int b)
将指定的字节写入此缓冲的输出流。
public staticvoidos8() throwsException{
BufferedOutputStream bos=null;
try{
bos=new BufferedOutputStream(new FileOutputStream("e:\\fileTest\\os.txt"));
bos.write(newbyte[]{110,111,112,113});
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(bos!=null){
bos.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
四、 BufferedInputStream
(一) AIP1.6.0 说明
java.io
类 BufferedInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.io.BufferedInputStream
所有已实现的接口: Closeable
public class BufferedInputStream
extends FilterInputStream
BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。
从以下版本开始: JDK1.0
(二) ConstructorAndMethod
BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
BufferedInputStream(InputStream in, int size)
创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
int
read()
参见 InputStream 的 read 方法的常规协定。
int
read(byte[] b, int off, int len)
从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。
public staticvoidis6()throws Exception{
BufferedInputStream bis=null;
try{
bis=new BufferedInputStream(new FileInputStream("e:\\fileTest\\os.txt"));
byte[]bytes=newbyte[1024];
int b;
while((b=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,b));
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(bis!=null){
bis.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
小结:4种流的读写方式的效率排序,从高到低依次为:
缓存字节流的按数组存取>>普通字节流的按数组存取>缓存字节流的按单个字节存取>普通字节流的按单个字节存取
五、 应用实例,将文件分割成若干个指定字节大小的文件
public staticvoidcutFile() throwsException{
FileInputStream fis=new FileInputStream("e:\\fileTest\\1.mp4");
byte[] bytes=newbyte[1024*1024*2];
int b;
while((b=fis.read(bytes))!=-1){
FileOutputStream fos=new FileOutputStream("e:\\fileTest\\"+UUID.randomUUID().toString()+".mp4");
fos.write(bytes,0,b);
fos.close();
}
fis.close();
}
六、 流的编码与解码
String-编码
byte[]
getBytes()
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[]
getBytes(Charset charset)
使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
String-解码
String(byte[] bytes)
通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
String(byte[] bytes,Charset charset)
通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
Arrays
static String
toString(byte[] a)
返回指定数组内容的字符串表示形式。
(一) 解码
public staticvoiddecode()throws Exception{
//你你哈哈 utf-8编码 [-28, -67, -96, -28, -67, -96, -27, -109, -120, -27,-109, -120]
//你你哈哈gbk编码 [-60, -29, -60, -29,-71, -2, -71, -2]
byte[] utf_8={-28, -67, -96, -28, -67, -96, -27, -109, -120, -27, -109, -120};
System.out.println(new String(utf_8));
System.out.println(new String(utf_8,"utf-8"));
byte[] gbk={-60, -29, -60, -29, -71, -2, -71, -2};
System.out.println(new String(gbk));
}
(二) 编码
public staticvoidencode() throwsException{
String str="你你哈";
byte[] bytes=str.getBytes();
System.out.println(Arrays.toString(bytes));
byte[] bytesUtf_8=str.getBytes("utf-8");
System.out.println(Arrays.toString(bytesUtf_8));
byte[] bytesGbk=str.getBytes("gbk");
System.out.println(Arrays.toString(bytesGbk));
}
(三) 乱码
编码方式与解码方式不一致
public staticvoidmessyCode() throwsException{
String str="你你哈哈";
byte[] bytesUtf_8=str.getBytes("UTF-8");
String gbk=new String(bytesUtf_8,"gbk");
System.out.println(gbk);
}
反乱码
public staticvoidencode() throwsException{
String str="你你哈哈";
byte[] bytesUtf_8=str.getBytes("UTF-8");
String gbk=new String(bytesUtf_8,"gbk");
byte[] bytesGbk=gbk.getBytes("gbk");
String getBgkStr=new String(bytesGbk,"utf-8");
System.out.println(getBgkStr);
}
上面反乱码的方式,不能解决未知字符介入乱码的问题。如❤等。