バイトストリームと文字ストリームのIO

バイトストリームと文字ストリームのIO
バイトストリームと文字ストリームは、神話上の違いがあり、それは作るのですか?
イメージビデオ、テキストなどをストリーミングバイトの任意のデータ
の文字列はプレーンテキストのみを扱うことができる(文字ストリームを使用している場合、テキスト形式にもお勧めします)


コンピュータが同じああなので、文字と文字以外のファイルを処理する流れありません。
これは簡単にその開口部の唯一の最初のIOであるため、

1.思考

テーマホールド4点
//ソースの選択1つの
// 2 [ストリーム
// 3操作
// 4つのリリースリソース

2.コード

バイトストリーム

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//1  选择源 
//2 选择流
//3 操作 
// 4 释放资源
public class 标准版文件字节流 {
public static void main(String[] args) {
	标准版文件字节流 f= new 标准版文件字节流();
	f.out();
}
public void read() {//字节文件读取流
	//选择源
		File file = new File("king.txt");
		//选择流
		InputStream in= null;
		try {
		in = new FileInputStream(file);
			
		//操作(分段读取)
			byte [] flush =new byte [128];//缓冲容器
			int len = -1;//接收容器
			while((len = in.read(flush))!=-1) {
				 String str=new String(flush,0,len);//字节数组-->字符串  解码
				 System.out.println(str);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//释放资源
			
			if(null!=in) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
public void out() {
	//选择源
	File file  =new File("king.txt");
	//选择流
	OutputStream output =null;
	try {
		output = new FileOutputStream(file ,true);
		//操作
		String s ="我又来了";
		byte [] n=s.getBytes();//编码
		output.write(n, 0, n.length);
		output.flush();//刷新
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {//关闭流
		if(null!=output) {
			try {
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
}

ファイルのバイトストリームをコピーします。

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class 文件的拷贝 {
public static void main(String[] args) {
	文件的拷贝 f = new  文件的拷贝();
	f.copy("1.jpg", "测试的文件.jpg");
}
public void copy(String srcpath,String destpath) {
	//选择源
	//File file1  =new File("1.jpg");
	//File file2  =new File("copy.jpg");
	File file1  =new File(srcpath);
	File file2  =new File(destpath);
	//选择流
	InputStream in =null;
	OutputStream out = null;
	try {
		in=new FileInputStream(file1);
		out =new FileOutputStream(file2);
		//操作
		byte [] flush = new byte [1024];
		int len =-1;
		while((len=in.read(flush))!=-1) {
			out.write(flush, 0, flush.length);
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//释放资源
	try {
		if(null!=out) {
			out.close();
		}
	} catch (Exception e) {
		// TODO: handle exception
	}
	try {
		if(null!=in) {
			in.close();
		}
	} catch (Exception e) {
		// TODO: handle exception
	}
}
}

文字ストリーム

package IO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class 文件字符输入输出流 {
public static void main(String[] args) {
	文件字符输入输出流 f= new 文件字符输入输出流();
	f.out();
}
public void in() {
	//选择源
		File file  = new File("king.txt");
		//选择流
		Reader read=null;
		 try {
			read=new FileReader(file);
			char [] flush = new char [128];
			int len =-1;
			while((len=read.read(flush))!=-1) {
				System.out.println(flush);
			}
			//操作
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//释放资源
			if(null!=read) {
				try {
					read.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
 public void out() {
	 File file =new File("文件字符流测试.txt")	;
	 Writer write =null;
	 try {
		write=new FileWriter(file);
		String test ="我实在不想玩了";
		char [] flush =test.toCharArray();//字符串->数组
		write.write(flush, 0,flush.length);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(null!=write) {
			try {
				write.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 }
}

公開された27元の記事 ウォンの賞賛5 ビュー655

おすすめ

転載: blog.csdn.net/qq_44620773/article/details/103962764