欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java IO流相关知识代码解析

程序员文章站 2024-02-19 19:15:40
一、io流的分类 字符流 reader inputstreamreader(节点流) bufferedreader(处理流) writer output...

一、io流的分类

字符流

reader
inputstreamreader(节点流)
bufferedreader(处理流)
writer
outputstreamwriter(节点流)
bufferedwriter(处理流)
printwriter

字节流

inputstream
fileinputstream(节点流)
bufferedinputstream(处理流)
objectinputstream(处理流)
printstream
outputstream
fileoutputstream(节点流)
bufferedoutputstream(处理流)
objectoutputstream(处理流)

断点处理的流

randomaccessfile

二、io流的用法

1、转换流的用法

     fileinputstream in = new fileinputstream(newfile(""));     
      readerreader = new inputstreamreader(in);//字节转字符
      fileoutputstreamout = new fileoutputstream(newfile(""));
      writer writer = new outputstreamwriter(out);//字符转字节

2、对象序列化,对象需要实现serializable接口

      fileoutputstreamfileoutputstream = new fileoutputstream("c:\\users\\lx\\desktop\\record.txt");
      objectoutputstreamobjectoutputstream = new objectoutputstream(fileoutputstream);
      objectoutputstream.writeobject(object);//向指定文件写入对象object
      objectoutputstream.close();

      fileinputstreamfileinputstream = new fileinputstream("c:\\users\\lx\\desktop\\record.txt");
      objectinputstreamobjectinputstream = new objectinputstream(fileinputstream);
      object = objectinputstream.readobject();//读取得到对象object
      fileinputstream . lose();

3、断点的运用

public class copy extends thread{
	//可以利用多线程实现拷贝  
	longstart;
	longend;
	filesorce;
	filetargetdir;
	publiccopy() {
	}
	publiccopy(longstart,long end, file sorce, file targetdir) {
		//利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件
		super();
		this.start= start;
		this.end= end;
		this.sorce= sorce;
		this.targetdir= targetdir;
	}
	@override
	   publicvoid run(){
		try{
			randomaccessfilesouceraf = new randomaccessfile(sorce,"r");
			randomaccessfiletargetraf = new randomaccessfile(newfile(targetdir,sorce.getname()),"rw");
			souceraf.seek(start);
			targetraf.seek(start);
			intlen= 0;
			byte[]bs = new byte[1024];
			longseek;
			system.out.println(start+"---->"+end+this.getname());
			while((len= souceraf.read(bs))!=-1){
				targetraf.write(bs, 0, len);
				seek= souceraf.getfilepointer();
				//获取断点位置
				if(seek== end){
					break;
				}
			}
			targetraf.close();
			souceraf.close();
		}
		catch (ioexception e) {
			e.printstacktrace();
		}
	}
}

4、字节流的用法

public class test_inputstream {
	//利用字节流获取文本文件内容,但是容易出现问题
	/*  
  //可能出现int长度越界
  public static void main(string[] args) throws ioexception {
    inputstream inputstream = new fileinputstream(new file("c:\\users\\lx\\desktop\\test\\33.txt"));
    byte[] b = new byte[inputstream.available()];  
    inputstream.read(b);
    string str = new string(b);
    system.out.println(str);
  }
*/
	//可能出现乱码    
	public static void main(string[] args) throws ioexception {
		file file = new file("c:\\users\\lx\\desktop\\test\\33.txt");
		inputstream inputstream = new fileinputstream(file);
		//统计每次读取的实际长度
		int len = 0;
		//声明每次读取1024个字节
		byte[] b = new byte[2];
		stringbuffer sbuffer = new stringbuffer();
		while((len=inputstream.read(b))!=-1){
			sbuffer.append(new string(b,0,len));
		}
		system.out.println(sbuffer.tostring());
	}
}
//利用字节流拷贝文件
public void copy(file sourcefile, file targetdir) {
	//
	fileinputstreamfileinputstream = null;
	fileoutputstreamfileoutputstream = null;
	fileinputstream= new fileinputstream(sourcefile);
	filetargetfile = new file(targetdir,sourcefile.getname());
	fileoutputstream= new fileoutputstream(targetfile);
	byte[]b = new byte[1024];
	intlen = 0;
	while((len= fileinputstream.read(b)) != -1) {
		fileoutputstream.write(b, 0, len);
	}
}

5、缓存字符流的用法

publicstatic void main(string[] args) throws ioexception {
	//缓存字符流实现写入文件
	inputstreamin = system.in;
	readerreader = new inputstreamreader(in);
	bufferedreaderbr = new bufferedreader(reader);
	bufferedwriterbw = new bufferedwriter(new filewriter(new file("src/1.txt")));
	strings="";
	while((s=br.readline())!=null) {
		bw.write(s);
		bw.newline();
		bw.flush();
		//字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
}

总结

以上就是本文关于java io流相关知识代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!