20190307课堂作业

package Test2019.M03.d05;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.Test;

public class io字符流 {
	
	@Test
	public void fun1(){
		//用字符流读取,
		FileReader fr = null;
		FileWriter fw = null;		
		try {
			fr = new FileReader("D:/test/1/我的青春谁做主.txt");
			File f = new File("D:/test/2/FileWriter.txt");
			f.createNewFile();
			fw = new FileWriter("D:/test/2/FileWriter.txt");
				
			char[] c = new char[10];
			int data;
			StringBuffer sb = new StringBuffer();
			
			while((data=fr.read(c))!=-1){
				for(int i=0;i<data;i++){
					sb.append(c[i]);	
					System.out.println(sb);	
					fw.write(c[i]);
				}
			}
			
			fw.flush();
			//记得关流,不关流的话,起码要清空缓冲区
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				/*fw.close();*/
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}}}
	@Test
	public void fun2(){
		//用字符流读取,
		InputStream is = null;
		InputStreamReader fr = null;
		FileWriter fw = null;		
		try {
			is = new FileInputStream("D:/test/1/我的青春谁做主.txt");
			fr = new InputStreamReader(is,"utf8");
			File f = new File("D:/test/2/FileWriter.txt");
			f.createNewFile();
			fw = new FileWriter("D:/test/2/FileWriter.txt");
			
			char[] c = new char[10];
			int data;
			StringBuffer sb = new StringBuffer();
			
			while((data=fr.read(c))!=-1){
				for(int i=0;i<data;i++){
					sb.append(c[i]);	
					System.out.println(sb);	
					fw.write(c[i]);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fw.close();
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}}}
	
	//bufferreader的读发
	@Test
	public void fun3(){
		//用字符流读取,
		InputStream is = null;
		InputStreamReader fr = null;
		FileWriter fw = null;	
		BufferedReader br = null;
		
		try {
			is = new FileInputStream("D:/test/1/我的青春谁做主.txt");
			fr = new InputStreamReader(is,"utf-8");
			br = new BufferedReader(fr);
			//新建文件
			File f = new File("D:/test/2/FileWriter.txt");
			f.createNewFile();
			
			String st;
			while((st=br.readLine())!=null){
				System.out.print(st);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				fw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}}}
	
	
	@Test
	
	//bufferedWriter
	public void fun4(){
		//用字符流读取,
		InputStream is = null;
		InputStreamReader fr = null;
		FileWriter fw = null;	
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		try {
			is = new FileInputStream("D:/test/1/我的青春谁做主.txt");
			fr = new InputStreamReader(is,"utf-8");
			br = new BufferedReader(fr);
			
			//新建文件
			File f = new File("D:/test/2/FileWriter.txt");
			f.createNewFile();
			fw = new FileWriter("D:/test/2/FileWriter.txt");
			bw = new BufferedWriter(fw);
			
			String st;
			StringBuffer sb = new StringBuffer();
			while((st=br.readLine())!=null){
				System.out.print(st);
				sb.append(st);
			}
			
			bw.write(sb.toString());
			bw.newLine();
			bw.write("来来来");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				br.close();
				fw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}}}
	
	@Test
	public void fun5(){
		//字符串替换的方法 
		String a = "你我他,大家共创未来!";
		a=a.replace("大家", "we");
		System.out.println(a);
		
		
	}
	
	
	
}

  

上一篇:在Java中使用PrintWriter时我需要关闭什么


下一篇:matlab求解动态规划