字符流

字符

字符是由字符组成的,例如 FileReader、FileWriter、BufferedReader、BufferedWriter、InputStreamReader、OutputStreamWriter 等。



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;


//1.用输出字符流(new FileWriter())向"D:/a.txt"写入“你好,小样”换行后“哈哈哈,把我输出出去”。


public class FileWrite {
public static void main(String[] args) {
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw = new FileWriter("D:\\a.txt");
bw = new BufferedWriter(fw);
String b="你好,小样";
String b2="哈哈哈,把我输出出去";
bw.write(b);
bw.write('\r');
bw.write('\n');
bw.write(b2);
System.out.println("输入完成");
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw!=null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


}

//2.实现从控制台接收一些字符串,写入文件中,当输入”exit”,停止
import java.io.*;
public class Test2 {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
String st="d:\\a.txt";
try {
System.out.println("输入文字:");
br=new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new FileWriter(st));
String str=null;
while (!(str=br.readLine()).equals("exit")) {
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


//3.新建立一个文件“e:/b.txt”将1题中的内容复制过来
public class Test3 {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader("D:\\a.txt"));
bw= new BufferedWriter(new FileWriter("D:\\a2.txt"));
while (br.ready()) {
// bw.write(br.readLine());
bw.append(br.readLine());
bw.newLine();
bw.flush();
}
// String str=null;
// while ((str=br.readLine())!=null) {
// bw.append(str);
// bw.newLine();
// str=br.readLine();
// bw.flush();
// }
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}

}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


//4.将a.txt中内容复制到b.txt,同时要求把a.txt中出现的所有a字母变成A
//如a.txt内容如下:
//aabcdaf
//那么b.txt要变成:
//AAbcdAf
public class Test4 {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader("D:\\a.txt"));
bw= new BufferedWriter(new FileWriter("D:\\a2.txt"));
// String str=null;
// while ((str=br.readLine())!=null) {
// str=br.readLine().replace('a', 'A');
// bw.append(str);
// bw.newLine();
// bw.flush();
// }
while ((br.ready())){
bw.append(br.readLine().replace('a', 'A'));  //replace 替换
bw.newLine();
bw.flush();
}


} catch (IOException e) {
e.printStackTrace();
}
finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}

}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


练习5:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

//:5.用输出字符流向(new FileWriter(f,true))"e:/a.txt"追加“嘿嘿嘿,会做么!!“
public class Test5 {
public static void main(String[] args) {
BufferedWriter bw=null;
try {
bw = new BufferedWriter(new FileWriter("D:\\a.txt", true));
bw.write("\r\n嘿嘿嘿,会做么!!");
System.out.println("输入完成!");
} catch (IOException e) {
e.printStackTrace();
}

finally{
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}


练习6:



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


//1.用输入字符流读取”e:/a.txt“中内容,到控制台输出
//三种方法:
//(1)、一个一个字符读出来显示并换行
//(2)、一次读10个字符出来并换行
//(3)、一行一行的读出来.使用输入字符流(大的)BufferedReader 读取”e:/a.txt“中内容,到控制台输出




public class Test6 {
public static void main(String[] args) {
// way1();
// way2();
way3();
}


public static void way1() {
FileReader fr=null;
try {
fr = new FileReader("D:\\a.txt");
int a=0;
while ((a=fr.read())!=-1) {
System.out.println((char)a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


public static void way2() {
FileReader fr=null;
try {
fr = new FileReader("D:\\a.txt");
char[]cr=new char[10];
int a=fr.read(cr);
while (a!=-1) {
System.out.println(new String(cr));
a=fr.read(cr);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


public static void way3() {
BufferedReader br=null;
try {
br =new BufferedReader(new FileReader("D:\\a.txt"));
String str = null;
while ((str=br.readLine())!=null) {
System.out.println(str);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}
}

练习7:



import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;


//7.实现从控制台接收一些字符串,写入文件中,当输入”exit”,停止
public class Test7 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
FileOutputStream fos=null;
try {
fos = new FileOutputStream("D:\\a.txt");
System.out.println("请输入一些字符串");
String str=sc.nextLine();
while (!"exit".equals(str)) {
fos.write(str.getBytes());
fos.write('\r');
fos.write('\n');
str=sc.nextLine();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值