字符流是什么?2017-5-15
字节流读写类 :InputStream 和OutpuStream
字符流读写类: Reader和Writer
测试代码:
package test.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class Setfile {
private String filename;
public Setfile(){
}
public Setfile(String s){
this.filename=s;
}
//字节输出流 OutputStream
public void Fileset(){ //一次性写入文件 ,每次运行会覆盖以前的 内容
try{
File f=new File("d:"+File.separator+filename);//声明file 对象,file.separator就是‘\’,
//如果没有这个文件,就会自动新建,比如 d:\kkk.txt
OutputStream out=new FileOutputStream(f);//创建字节流 输出到文件f
String str="hello,java world!";
byte b[]=str.getBytes(); //因为是字节流,所以要把字符转换成字节。当然你可以直接用字节
out.write(b); //写入内容到文件
out.close(); // 关闭流, 一定要关闭流释放空间
}catch(IOException e){
e.printStackTrace();
}
}
public void Fileset(String s){ //一个一个字节写入文件 每次输出会覆盖以前的内容
try{
File f=new File("d:"+File.separator+s);
OutputStream out2=new FileOutputStream(f);
String str="hello java world";
byte b[]=str.getBytes();//转换成字节
for(int i=0;i<b.length;i++){
//如果你要对字符串操作,可以一个一个字节进行写入。
out2.write(b[i]);
}
out2.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void Fileset(String path,String s){//追加内容到文件中
try{
File f=new File(path);
OutputStream out3=new FileOutputStream(f,true);//FileOutputStream(File f,Boolean b)追加内容
byte b[]=s.getBytes();//转换成字节
out3.write(b);
out3.close();
}catch(IOException e){
e.printStackTrace();
}
}
//字符写入流 Writer
public void writerFile(){ //会覆盖以前的内容
try{
File f=new File("d:"+File.separator+filename);//声明file 对象,file.separator就是‘\’,
//如果没有这个文件,就会自动新建,比如 d:\kkk.txt
Writer w=new FileWriter(f); //创建字符流 输出到文件 f
String str="hello,java Writer";
w.write(str); //将字符串写入文件,字符不用转换字节,因为是字符流
w.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void writerFile(String s){ //追加内容
try{
File f=new File("d:"+File.separator+filename);
Writer w=new FileWriter(f,true); //创建字符流 追加输出到文件 f
w.write(s); //字符不用转换字节,因为是字符流
w.close();
}catch(IOException e){
e.printStackTrace();
}
}
//输入流
//读出字节流
public void outFilebyByte(){ //一次性读出
try{
File f=new File("d:"+File.separator+filename); //同上
InputStream in=new FileInputStream(f); //InputStream 是读出字节流的类
byte b[]=new byte[(int) f.length()];//创建字节数组 存储读出的内容
// byte b[]=new byte[1024]; 这种方法也可以,但是太浪费空间
in.read(b); //读出文件内容,存放到字节数组中
in.close(); //关闭字节流
System.out.println(new String(b));
}catch(IOException e){
e.printStackTrace();
}
}
public void outFilebyByte(String s){//一个一个字节读出
try{
File f=new File("d:"+File.separator+s); //同上
InputStream in=new FileInputStream(f); //InputStream 是读出字节流的类
byte b[]=new byte[(int) f.length()];//创建字节数组 存储读出的内容
for(int i=0;i<b.length;i++){
b[i]=(byte)in.read(); //read()方法返回的是字节的Ascll码的值,是int型 所以要转换为byte型
//一个字节的读出文件内容,存放到字节数组中
}
in.close(); //关闭字节流
System.out.println(new String(b));
}catch(IOException e){
e.printStackTrace();
}
}
//以上都是知道文件大小的情况下,读出的内容
//下面是不知道文件大小的情况下读出文件数据
public void outFilebyByte(String path,String s){//一个一个字节读出
try{
File f=new File(path+s); //同上
InputStream in=new FileInputStream(f); //InputStream 是读出字节流的类
byte b[]=new byte[1024];//创建字节数组 存储读出的内容
int i=0;
int coun=0;
while((coun=in.read())!=-1){
b[i]=(byte)coun; //read()方法返回的是字节的Ascll码的值,是int型 所以要转换为byte型
//一个字节的读出文件内容,存放到字节数组中
i++;
}
in.close(); //关闭字节流
System.out.println(new String(b,0,i));
}catch(IOException e){
e.printStackTrace();
}
}
//字符流 读出内容
public void outFilebychar(){ //一次性读出
try{
File f=new File("d:"+File.separator+filename);
Reader r=new FileReader(f); //Reader 是读出字符流的类
char c[]=new char[(int) f.length()];//创建字符数组 存储读出的内容
int i=r.read(c); //读出文件内容,存放到字符数组中
r.close(); //关闭字符流
System.out.println(new String(c,0,i));
}catch(IOException e){
e.printStackTrace();
}
}
public void outFilebychar(String s){ //在不知道文件大小的情况下,一个个字符读出
try{
File f=new File("d:"+File.separator+s);
Reader r=new FileReader(f); //Reader 是读出字符流的类
char c[]=new char[1024];//创建字符数组 存储读出的内容
int i=0;
int coun=0;
while((coun=r.read())!=-1){
c[i]=(char)coun; //读出文件内容,存放到字符数组中
i++;
}
r.close(); //关闭字符流
System.out.println(new String(c,0,i));
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
//测试
//new Setfile("kkk.txt").Fileset();
//new Setfile().Fileset("kkk.txt");
//new Setfile().Fileset("d:\\kkk.txt", "hhhhhhhh");
//new Setfile("kkk.txt").writerFile();
//new Setfile("kkk.txt").writerFile("\r\nhhhhhhhhh");//"\r\n 是换行“
//new Setfile().outFilebyByte("kkk.txt");
//new Setfile().outFilebyByte("d:\\","kkk.txt");
//new Setfile("kkk.txt").outFilebychar();
//new Setfile().outFilebychar("kkk.txt");
}
}
-
字节流和字符流相比,大多数情况下都使用字节流,如果是对中文数据建议使用字符流(刚好一个字符可以存放一个字)
-
别忘了关闭数据流哦