赞
踩
3、创建c:/test.txt文件并在其中输入"hello world" 创建一个输入流读取该文件中的文本 并且把小写的l变成大写L再利用输出流写入到d:\test.txt中
3.1 实现步骤:
3.1.1 在本地硬盘C盘下创建一个文件test.txt
3.1.2 创建一个包含main()方法的类,并在main中编写代码
3.1.3 运行代码并且测试结果
3.2 实现过滤器的功能
这里总共写了三个方法:
package com.neuedu.chapter03._文件管理Java7特性; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; public class FileReader_FileWriter_Homework3 { /** * 方法1 */ public static void methodOne() { FileReader reader = null; FileWriter writer = null; try { reader = new FileReader("file/test.txt"); //读取文件 writer = new FileWriter("file/test_back.txt"); //写文件 int content = -1; while((content = reader.read()) !=-1) { //将字符数组一个一个地比较,将“l”转化为“L” if(content == 'l') { content = 'L'; } writer.write(content); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 方法2 */ public static void methodTow() { FileReader reader = null; FileWriter writer = null; try { reader = new FileReader("file/test.txt"); //字符流输入 writer = new FileWriter("file/test_back.txt"); //字符流输出 char[] ch = new char[11]; int length; while((length = reader.read(ch)) != -1) { System.out.println(ch); System.out.println(length); //将字符数组转换为一个字符串,在用字符串的replace()方法将“l”转化为“L” //再见转化号的字符串用toCharArray()方法转化为字符数组 ch = new String(ch).replace("l", "L").toCharArray(); writer.write(ch,0,length); //写入指定文件 } writer.flush(); //强制刷新 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 方法3 */ public static void methodThree() { FileReader file=null; String s = ""; BufferedReader br = null; BufferedWriter writer =null; try { file = new FileReader("file/test.txt"); br = new BufferedReader(file); s = br.readLine(); //使用吃力流的方法 System.out.println("原文件"+s); s = s.replace("l","L"); System.out.println(s); FileWriter fw = new FileWriter("file/test_back.txt"); writer = new BufferedWriter(fw); writer.write(s); writer.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { //methodTow(); methodThree(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。