IO流

来源:互联网 发布:淘宝论坛帐号 编辑:程序博客网 时间:2024/06/05 10:11

//文件拷贝:

package demo;
import java.io.*;
public class Demo {
public static void main(String[] args)
{
FileWriter fw=null;
FileReader fr=null;
try{
fw=new FileWriter("abc.txt");
fr=new FileReader("demo.txt");
char[] buf=new char[1024];
int len=0;
while((len=fr.read(buf))!=-1){
fw.write(buf,0,len);
}
}catch(Exception e){
throw new RuntimeException("读写失败");
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {

e.printStackTrace();
}
}if(fr!=null){
try {
fr.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}
}
}

//缓冲字符流实现文件拷贝

package demo;
import java.io.*;
class Demo{
public static void main(String [] args)
{
BufferedReader bufr=null;
BufferedWriter bufw=null;
try{
bufr=new BufferedReader(new FileReader("abc.txt"));
bufw=new BufferedWriter(new FileWriter("abc1.txt"));
String line=null;
while((line=bufr.readLine())!=null){
bufw.write(line);
bufw.newLine();//读一行时,没有包含行终止符(换行符),所以要写一个换行语句
bufw.flush();
}
}catch(Exception e){
throw new RuntimeException("文件读写失败");
}finally{
if(bufr!=null){
try {
bufr.close();
} catch (IOException e) {

e.printStackTrace();
}
}
if(bufw!=null){
try {
bufw.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}
}
}

//图片或视频拷贝(一)

package demo;
/*目的:拷贝图片
 * 思路:1.用字节读取流对象和图片关联
 * 2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
 * 3.通过循环读写,完成数据的存储
 * 4。关闭流资源*/
import java.io.*;


class Demo {
public static void main(String[] args) throws IOException {
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream("1.avi");
fos=new FileOutputStream("2.avi");
byte buf[]=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
fos.write(buf);
}
}catch( IOException e){
throw new RuntimeException("拷贝文件失败");
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException e){
throw new RuntimeException("读取关闭失败");
}
}
if(fos!=null){
try{
fos.close();
}catch(IOException e){
throw new RuntimeException("写入关闭失败");
}
}
}
}
}

//图片或视频拷贝(二)注:带缓冲

import java.io.*;


class Demo {
public static void main(String[] args){
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.println((end-start));
}
public static void copy(){
BufferedInputStream bufis=null;
BufferedOutputStream bufos=null;
try{
bufis=new BufferedInputStream(new FileInputStream("1.avi"));
bufos=new BufferedOutputStream(new FileOutputStream("2.avi"));
byte buf[] = new byte[1024];
int len = 0;
while ((len = bufis.read(buf)) != -1) {
bufos.write(buf);
}
}catch(IOException e){
throw new RuntimeException("拷贝文件失败");
}finally{
if (bufis != null) {
try {
bufis.close();
} catch (IOException e) {
throw new RuntimeException("读取关闭失败");
}
}
if (bufos != null) {
try {
bufos.close();
} catch (IOException e) {
throw new RuntimeException("写入关闭失败");
}
}
}
}
}


缓冲技术个人总结:(一)中的拷贝虽然用到了buf[1024]:这里是一次从硬盘读1024字节,然后在一次写1024字节到硬盘的另一处,比不用buf时(一次只读一个字节)减少了操作底层的时间,每次去硬盘读写开销是很大的。(二)这里用了两个缓冲流BufferedInputStream、BufferedOutputStream(相当于两个buf,一个负责读,一个负责写),这样在两个buf之间进行拷贝,只在内存中进行,不必操作硬盘,节省时间;如果同时在之间再加入buf,又会节省跳转时间,一次读一片,节省了循环跳转的时间。综上:节省时间体现在两个地方:1.将操作放到内存执行,尽量少的进入底层;2.减少跳转,一次读或写一片。










//自定义BufferedInputStream,实现了它的两个方法:read()和close()

import java.io.*;


class MyBufferedInputStream
{
private InputStream in;


private byte[] buf = new byte[1024*4];//BufferedInputStream内定义了一个buf

private int pos = 0,count = 0;

MyBufferedInputStream(InputStream in)
{
this.in = in;
}


//一次读一个字节,从缓冲区(字节数组)获取。
public int myRead()throws IOException
{
//通过in对象读取硬盘上数据,并存储buf中。
if(count==0)
{
count = in.read(buf);
if(count<0)
return -1;
pos = 0;
byte b = buf[pos];


count--;
pos++;
return b&255;
}
else if(count>0)
{
byte b = buf[pos];


count--;
pos++;
return b&0xff;
}
return -1;


}
public void myClose()throws IOException
{
in.close();
}
}




/*
11111111-111111110000000000101001001010100101010010101001010




byte: -1  --->  int : -1;
00000000 00000000 00000000 11111111  255


11111111 11111111 11111111 11111111




11111111  -->提升了一个int类型 那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。
怎么补0呢?


 11111111 11111111 11111111 11111111                        
&00000000 00000000 00000000 11111111 
------------------------------------
 00000000 00000000 00000000 11111111 


0000-0001
1111-1110
000000001
1111-1111  -1




结论:
字节流的读一个字节的read方法为什么返回值类型不是byte,而是int。
因为有可能会读到连续8个二进制1的情况,8个二进制1对应的十进制是-1.
那么就会数据还没有读完,就结束的情况。因为我们判断读取结束是通过结尾标记-1来确定的。
所以,为了避免这种情况将读到的字节进行int类型的提升。
并在保留原字节数据的情况前面了补了24个0,变成了int类型的数值。
而在写入数据时,只写该int类型数据的最低8位(write()方法默认的只写低8位)。

注意:在进行字节流操作时,最好用BufferedInputStream,一:它可以提高效率,二:对字节流的-1情况进行了判断
BufferedInputStream.read()实现的源码:
 public synchronized int read() throws IOException {
        if (pos >= count) {
            fill();
            if (pos >= count)
                return -1;
        }
        return getBufIfOpen()[pos++] & 0xff;
    }




//

只有转换流才能操作编码,如下:用utf-8格式写入abc.txt的中文字符,只能有utf-8才能解出来,否则,就会乱码(默认是GBK编码)


package demo;


import java.io.*;




class Demo {
public static void main(String[] args)throws IOException{

BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("abc.txt"),"utf-8"));
String line=null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
bufw.write(line);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
}
}

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

package demo;


import java.io.*;




class Demo {
public static void main(String[] args)throws IOException{

BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt"),"utf-8"));
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));
String line=null;
while((line=bufr.readLine())!=null){
bufw.write(line);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
}
}
*/

//测试

package demo;

import java.io.*;
class Demo{
public static void main(String [] args) throws IOException 
{
FileWriter fw=new FileWriter("abc.txt");
for(long i=0;i<7*1024+1022;i++){
fw.write("a");
}
fw.write("李李李李");
}


}

超过8M才刷新,只将8M内容刷出,多余8M小于16M的部分会继续缓存


//

//管道流
package demo;
import java.io.*;
public class Demo {
public static void main(String[] args)throws Exception
{
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
in.connect(out);
Read r=new Read(in);
Write w=new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
class Read implements Runnable{
private PipedInputStream in;
Read(PipedInputStream in){
this.in=in;
}
public void run(){
try{
byte[] buf=new byte[1024];
int len=in.read(buf);
String s=new String(buf,0,len);
System.out.println(s);
in.close();
}catch(Exception e){
throw new RuntimeException("读取失败");
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out=out;
}
public void run(){
try{
out.write("shujulaile".getBytes());
out.close();
}catch(Exception e){
throw new RuntimeException("写入失败");
}
}

}

0 0
原创粉丝点击