您的位置:首页 > 编程语言 > Java开发

欢迎使用CSDN-markdown编辑器

2016-09-01 22:47 260 查看

 JAVA文件传输基础—java IO流

  1.在JAVA中用getBytes()方法转换成字节序列的项目默认的编码gbk,在gbk编码汉字占2个字节,英文占用1给我字节。而utf-8编码中汉字占用3个字节,英文占用1个字节。java是双字节编码,即utf-16be,中文和英文都是2个字节。

  2.当字节序列是某种编码时,需要把字节序列变成字符串,也是需要相对应的编码方式,否则会出现乱码。

  3.文本文件本质上时字节码文件,可以是任意编码的字节序列,但是如果在中文机器上直接编码,那么文本文件只认识ansi编码

  4.java.io.File类主要用于表示文件(目录),其中File类只用于便是文件(目录)的信息,而不能用于文件内容的访问。

  5.遍历目录

String[] filenames=dir.list();

for(String string: filenames)

System.out.println(dir+"\\"+string);


返回的是字符串数组,直接子的名称,不包含子目录下的内容

如果需要遍历子目录下的内容就需要构造File对象做递归操作

File[] files =dir.listFiles();

if(file!=null&&file.length>0){

for(File file:files){

if(file.isDirectory()){

listDirctory

}else{

System.out.println(file);}

}

}


  6.RandomAccessFile 是JAVA提供的对文件内容的访问,可以读文件也可以写问阿金。支持随机访问文件,可以访问文件任意位置。打开文件有两种模式”rw”和”r”
RandomAccessFile raf= new RondomAcceccFile(file,"rw")
打开文件是文件指针在开头pointer=0;

 

写方法raf.write(int) 只能写一个字节(后八位)

要把int i= 0x7ffffff 用write()写入,需要写4次

raf.wtite(i>>>24);

raf.wtite(i>>>16);

raf.wtite(i>>>8);

raf.wtite(i);


如果用
raf.writeInt(i)
,可以方便的直接写入一个int。

也可以写成一个字节数组,然后一次写入

String s="中";

 byte[] gbk =s.getBytes("gbk");

 raf.write(gbk);


读方法int b=raf.read(), 只能读一个字节(读文件时要把指针移到头部raf.seek(0);)

用数组的方式一次性读取文件

byte[]buf=new byte[(int)raf.length()];
raf.read(buf);
System.out.println(Arrays.toString(buf));


文件在读/写完以后一定记得关闭IO raf.close()

创建一个目录File file=new File(demo); demo.mkdir();

创建目录下的一个文件夹 File file =new File(demo,”**.txt”) file.createNewFile();

定义文件的打开方式
RandomAccessFile raf =new RandomAccessFile(file,"rw");


7.字节流 inputSteam和outputStream,其中inputStream抽象了应用数据读取数据的方式,outputStream抽象了应用数据读取数据的方式,EOF=End 读到—1就读到结尾。

int b =in.read(); 读取一个字节无符号填充到int的低八位。in.read(byte[]buf,int start,int size)读取数据到字节数组

out.write(byte b)写出一个byte到流,b的低八位,

8.子类 FileInputStreasm具体实现了在文件上读取数据

//单字节读取,效率低

FileInputStream in = new FileInputStream(fileName);
int b ;
int i = 1;
while((b = in.read())!=-1){
if(b <= 0xf){
//单位数前面补0
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+"  ");
if(i++%10==0){
System.out.println();
}
}
in.close();

//批量读取,适合大文件
FileInputStream in = new FileInputStream(fileName);
byte[] buf = new byte[8 * 1024];
int bytes =0:
int j=1;
while (bytes=in.read(buf,0,buf.length)!=-1){
for(int i=0;i<bytes;i++){
Systeam.ou.println(Integer.toHexString(buf[i]&oxff)+" ");
if(i++%10=0){
Systeam.out.println();
}
}
in.close();
}


9.子类 FileOutputStreasm具体实现了在文件上写入数据

//单个字节写入

FileOutStream out=new FileStream("demo/out.dat");

out.write("a");

int a=10;

out.write(a>>>24);

out.write(a>>>16);

out.write(a>>>8);

out.write(a);

byte[] gbk= "中国".getbyte("gbk");

out.write(gbk);

out.close;


10.子类 FileIntputStreasm和子类 FileOutputStreasm

   //单字节的copy文件,不带缓冲进行文件拷贝

public static void copyFileByByte(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c ;
while((c = in.read())!=-1){
out.write(c);
out.flush();
}
in.close();
out.close();
}

}


//批量copy文件,利用带缓冲的字节流
public static void copybyBuffer(File srcFile,File destFile)throws IOException{
if(!srcFile.is exists()){
throw new IllegalArgumentExcception(srcFile+"文件不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentExcception(srcFile+"不是文件");
}
BufferedInputStream bis=new BufferedInputSteream(
newFileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(
new FileOutputStream(destFile));
int c;
while((c=bis.read())!=-1){
bos.write(c);
bis.flush();
}
bis.close();
bos.close();
}

//文件拷贝,字节批量读取(最快)
public static void copyFile(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8*1024];
int b ;
while((b = in.read(buf,0,buf.length))!=-1){
out.write(buf,0,b);
out.flush();//最好加上
}
in.close();
out.close();

}
//FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
//DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
//BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了


11.字符流 一次处理一个字符,底层仍然是基本的字节序列

InputStreamReader 完成byte流解析为char流,按照编码解析

OutputStreamReader 完成char流解析为byte流,按照编码解析

InputStreamReader isr = new InputStreamReader(
new FileInputStream("e:\\javaio\\imoocutf8.txt"),"utf-8");//默认项目的编码,操作的时候,要写文件本身的编码格式
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("e:\\javaio\\imoocutf81.txt"),"utf-8");
/*int c ;
while((c = isr.read())!=-1){
System.out.print((char)c);
}*/
char[] buffer = new char[8*1024];
int c;
/*批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个
返回的是读到的字符的个数
*/
while(( c = isr.read(buffer,0,buffer.length))!=-1){
String s = new String(buffer,0,c);
System.out.print(s);
osw.write(buffer,0,c);
osw.flush();
8f5c
}
isr.close();
osw.close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文件传输 io流