zoukankan      html  css  js  c++  java
  • 0226字符流 转换流

    根据字节流我们可以对文件夹进行基础的操作,但是我们要对文件写入中文字符时怎么处理

    在字节流中我们可以将利用getbytes方法和new String 进行转换从而可以在访问文件中的中文字符,还可以通过字符流对文件中的中文字符进行操作

    1、字符输出流 writer

    该类是抽象类,不能创建类对象,所以我们要创建其子类对象 FileWriter

    常见构造方法

    FileWriter(File file)

    FileWriter(String name)

    这两个构造方法都是用来明确读写文件的位置,跟前面所学的字节流的构造方法都是一个类型的,同时开启续写功能就是在参数后边加一个布尔值,默认不写是false

    常用类方法

    write(int c)单个字节写入

    write(char[] ch)字符数组写入

    write(char[] ,int a,int b)字符数组写入,从a开始写入b个

    write(String str)字符串写入

    write(String str,int a,int b)字符串写入,从a开始 传入b个、

    代码展示

    	public static void main(String[] args) throws IOException {
    		//明确目的地
    		FileWriter fw=new FileWriter("F:\io1127\demo11.txt",true);
    		//写入一个字符
    		fw.write(100);
    		//写入字符数组
    		char[] ch={'0','2','r','中'};
    		fw.write(ch,1,2);
    		//写入字符串
    		String s="海绵宝宝";
    		fw.write(s, 2, 2);
    		//释放资源
    		fw.close();
    		
    		
    	}
    

      

    2、字符输入流 Reader

    该类是抽象类,不能创建类对象,所以我们要创建其子类对象 FileReader

    常见构造方法

    FileReader(File file)

    FileReader(String name)

    这两个构造方法都是用来明确读写文件的位置,跟前面所学的字节流的构造方法都是一个类型的,同时开启续写功能就是在参数后边加一个布尔值,默认不写是false

    常用类方法

    read()单个字节读取

    read(char[] ch)字符数组读取

    代码展示单个字节读取

    	public static void main(String[] args) throws IOException {
    		FileReader fr=new FileReader("F:\io1127\demo11.txt");
    		//读取一个字符
    		int len=0;
    		while((len=fr.read())!=-1){
    			System.out.print((char)len);
    		}
    		
    		//释放
    		fr.close();
    	}
    

      

    代码展示字符数组读取

    public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    
    		FileReader fr=new FileReader("F:\io1127\demo11.txt");
    		//一个字符数组
    		char[] ch=new char[1024];
    		int len=0;
    		while((len=fr.read(ch))!=-1){
    			System.out.println(new String(ch,0,len));
    		}
    		//释放
    		fr.close();
    	}
    

      

    文件复制

    单个字节复制

    	public static void main(String[] args) throws IOException {
    		//明确位置
    		FileReader fr=new FileReader("F:\io1127\demo11.txt");
    		//明确目的
    		FileWriter fw=new FileWriter("F:\io1127\work1\demo11.txt");
    		int len=0;
    		while((len=fr.read())!=-1){
    			fw.write(len);
    			fw.flush();
    		}
    		//释放资源
    		fr.close();
    		fw.close();
    	}
    

      

    数组复制

    	public static void get() throws IOException{
    		//明确位置
    		FileReader fr=new FileReader("F:\io1127\demo11.txt");
    		//明确目的
    		FileWriter fw=new FileWriter("F:\io1127\work\demo11.txt");
    		char[] ch=new char[1024];
    		int len=0;
    		while((len=fr.read(ch))!=-1){
    			fw.write(ch);
    			fw.flush();
    		}
    		//释放资源
    		fr.close();
    		fw.close();	
    	}
    

      

    3.flush()和close()方法的区别

    flush()刷新该流缓冲区,最好是写一句刷新一下

    close()关闭该流,但先要刷新一下

    4、转换流

    我们的工作区是jbk码表,那我们写入的文件有可能是utf8码表的文件,这就写入字符的时候会出现乱码的情况,那么转换流就是用来指定码表用的,保证写入的字符以指定的码表写入,这样就不会出现乱码的情况。

    输出流构造方法

    OutputStreamWriter(FileOutputStream,mabiao);

    代码展示

    	public static void main(String[] args) throws IOException {
    		//明确目的地
    		FileOutputStream fos=new FileOutputStream("F:\io1127\utf8.txt",true);
    		//创建转换流对象明确码表
    		OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
    		//写入
    		osw.write("海绵宝宝");
    		osw.close();
    	}
    

      图解

     输入流构造方法

    InputeStreamReader(FileInputStream,mabiao);

    代码展示

    	public static void main(String[] args) throws IOException {
    		FileInputStream fis=new FileInputStream("F:\io1127\utf8.txt");
    		InputStreamReader isr=new InputStreamReader(fis,"utf-8");
    		char[] ch=new char[1024];
    		int len=0;
    		while((len=isr.read(ch))!=-1){
    			System.out.println(new String(ch,0,len));
    		}
    		isr.close();
    	}
    

      

    利用转换流对文件进行复制

    	//用转换流复制utf-8编码的文件
    	public static void get4() throws IOException{
    		//
    		FileInputStream fis=new FileInputStream("F:\io1127\utf8.txt");
    		
    		InputStreamReader isr=new InputStreamReader(fis,"utf-8");
    		
    		FileOutputStream fos=new FileOutputStream("F:\io1127\a\utf8.txt");
    		
    		OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
    		
    		char[] ch=new char[1024];
    		int len=0;
    		while((len=isr.read(ch))!=-1){
    			osw.write(ch);
    			osw.flush();
    		}
    		isr.close();
    		osw.close();
    		
    		
    	}
    

      

  • 相关阅读:
    下载、安装、编译 QTLtools
    解决方案:使用QTLtools进行表型PCA分析,得到了一堆NA
    报错:terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create
    使用 bcftools 进行基因型过滤(genotypes QC)
    使用 QTLtools 进行 PCA 分析
    生物信息学博士后招聘:香港浸会大学张璐博士和卞兆祥教授联合招聘
    Error in getNodeSet(html, path = "//div[@class='plain-box float-right archive-box']")[[1]] : 下标出界
    说说程序员写文档这事
    碎碎念五十
    计算一个服务端方法运行多次的平均耗时(Java)
  • 原文地址:https://www.cnblogs.com/-gongxue/p/14454356.html
Copyright © 2011-2022 走看看