• 【自动化__持续集成】___java___文本文件__字符流


    一、代码如下

    package www.wujianbotwo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    
    public class Demo10 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Demo10 demo10= new Demo10();
    		demo10.charRead();
    
    	}
    	public void charRead() {
    		try {
    			File file= new File("D:\Test.txt");
    			InputStream in= new FileInputStream(file);
    			InputStreamReader reader= new InputStreamReader(in, "GBk");
    			int temp;
    			while((temp=reader.read()) != -1) {
    				if((char)temp != '
    ') {
    					System.out.print((char)temp);
    				}
    			}
    			reader.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    
    package www.wujianbotwo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    
    public class Demo10 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Demo10 demo10= new Demo10();
    		demo10.charRead();
    
    	}
    	public void charRead() {
    		try {
    			File file= new File("D:\Test.txt");
    			InputStream in= new FileInputStream(file);
    			InputStreamReader reader= new InputStreamReader(in, "GBk");
    			int temp;
    			do {
    				
    				temp= reader.read();
    				if((char)temp != '
    ') {
    					System.out.print((char)temp);
    				}
    				
    			}while(temp != -1);
    			//while((temp=reader.read()) != -1) {
    			//	if((char)temp != '
    ') {
    			//		System.out.print((char)temp);
    			//	}
    			//}
    			reader.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    
    public void charWrite() {
    		try {
    			File file= new File("D:\Test.txt");
    			OutputStream in= new FileOutputStream(file, true);
    			OutputStreamWriter out= new OutputStreamWriter(in, "GBK");
    			String conten= "我要回家。
    ";
    			char[] ch= conten.toCharArray();
    			out.write(ch, 0, ch.length);
    			out.flush();
    			out.close();
    			System.out.println("写入成功");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public void charWriteTwo() {
    		try {
    			File file= new File("D:\Test.txt");
    			OutputStream in= new FileOutputStream(file, false);
    			OutputStreamWriter out= new OutputStreamWriter(in, "GBK");
    			String conten= "我要回家。
    ";
    			char[] ch= conten.toCharArray();
    			out.write(ch, 0, ch.length);
    			out.flush();
    			out.close();
    			System.out.println("写入成功");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    package www.wujianbotwo;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    
    public class Demo11 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Demo11 demo11= new Demo11();
    		demo11.bufferedRead();
    
    	}
    	
    	public void bufferedRead() {
    		// TODO Auto-generated method stub
    		try {
    			File file= new File("D:\Test.txt");
    			InputStream in= new FileInputStream(file);
    			InputStreamReader reader= new InputStreamReader(in, "GBK");
    			BufferedReader br= new BufferedReader(reader);
    			String line= "";
    			do {
    				line= br.readLine();
    				System.out.println(line);
    			}while (line != null);
    			br.close();
    			reader.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    }
    
  • 相关阅读:
    (转)java反射机制及简单工厂模式
    (转)JAVA反射机制理解
    (转)前缀、中缀、后缀表达式
    (转)java提高篇(四)-----理解java的三大特性之多态
    (转)java for循环的执行顺序和几种常用写法
    (转)JAVA堆栈操作
    POI 实现合并单元格以及列自适应宽度
    前端缓存支持的文件格式及请求方式
    freemarker在xml文件中遍历list数据
    freemarker在线编辑
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7492127.html
Copyright © 2020-2023  润新知