6.IO流综合练习
A:把集合中的数据存储到文本文件中
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\a.txt"));
for(String str : array){
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
B:把文本文件中的数据读取到集合中并遍历集合
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while((line = br.readLine()) != null){
array.add(line);
}
br.close();
for(String str : array){
Syste.out.println(str);
}
C:复制指定目录下的指定文件,并修改后缀名
//封装目录
File srcFolder = new File("E:\\Java");
File destFolder = new File("D:\\Jad");
//如果目的地不存在则创建
if(!destFolder.exists()){
destFolder.mkdir();
}
//从数据源获取指定文件名添加到文件数组中
File[] fileArray = srcFolder.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
return new File(dir,name).isFile() && name.endsWith(".java");
}
});
//遍历数组得到新路径并复制文件
for(File file : fileArray){
String neme = file.getName();
File newFile = new File(destFolder,name);
coptyFile(file,newFile);
}
//在目的地目录下完成文件名称修改
File[] destFileArray = destFolder.listFiles();
for(File destFile : destFileArray){
String name = destFile.getName();
String newName = name.replace(".java",".jad");
File newFile = new File(sestFolder,newName);
destFile.renameTo(newFile);
}
//复制文件的方法
private static void copyFile(File file,File newFile) throws IOException {
BufferedInputStrean bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newfile));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
D:复制多级文件夹
//封装目录
File srcFolder = new File("E:\\JavaSE");
File destFolder = new File("D:\\");
copyFolder(srcFolder,destFolder);
//创建新目录路径,复制文件夹,创建文件路径
private static void copyFolder(File srcFolder,File destFolder) throws IOException {
if(srcFolder.isDirectory()){
File newFolder = new File(destFolder,srcFolder.getName());
newFolder.mkdir();
File[] fileArray = srcFolder.listFiles();
for(File file : fileArray){
copyFolder(file,newFolder);
}
}else{
File newFile = new File(destFolder,srcFolder.getName());
copy(srcFolder,newFile);
}
}
//复制文件
private static void copyFile(File srcFolder,File newFile) throw IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFolder));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read()) != -1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
E:键盘录入5个学生信息,按照总分从高到低存储到文本文件
//在Student类中写出getSum()方法
public int getSum(){
return this.chinese + this.math + this.english;
}
//测试类中完成下列代码
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
public int compare(Student s1,Student s2){
int num = s2.getSum() - s1.getSum();
int num2 = num == 0? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0? s1.getMath() - s2.getMath() : num2;
int num4 = num3 == 0? s1.getEnglish() - s2.getEnglish() : num3;
int num5 = num4 == 0? s1.getName.compareTo(s2.getName()) : num4;
return num5;
}
});
for(int x = 0; x < 5; x++){
键盘录入学生信息
创建学生对象
将学生对象添加为集合元素
}
BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt"));
bw.write("学生信息如下:");
bw.newLine();
bw.flush();
bw.write("姓名 语文成绩 数学成绩 英语成绩");
bw.newLine();
bw.flush();
for(Student s : ts){
StringBuilder sbu = new StringBuilder();
sbu.append(s.getName()).append(" ").append(s.getChinese()).append(" ")
.append(s.getMath()).append(" ").append(s.English);
bw.write(sbu.toString());
bw.newLine();
bw.flush();
}
bw.close();
System.out.println("学生信息存储完毕!");
F:用Reader模拟BufferedReader的特有功能
public class MyBufferedReader{
private Reader r;
public MyBufferedReader(Reader r){
this.r = r;
}
public String readLine() throws IOException {
StringBuilder sbu = new StringBuilder();
int ch = 0;
while((ch = r.read()) != -1){
if(ch = '\r'){
continue;
}
fi(ch == '\n'){
return return sbu.toString;
}else{
sbu.append((char)ch);
}
}
if(sbu.length() > 0){
return sbu.toString();
}
return null;
}
public void close() throws IOException {
this.r.close();
}
}
//测试类
MyBufferedReader mbr = new MyBufferedReader(new FileReader("aaa.txt"));
String line = null;
while((line = mbr.readLine()) != null){
System.out.println(line);
}
mbr.colse();
7.字符转换流的理解
字符读入转换流
InputStreamReader = FileInputStream + 编码表
FileReader = FileInputStream + 编码表
字符写出转换流
OutputStreamWriter = FileOutputStream + 编码表
FileWriter = FileOutputStream + 编码表
8.打印流
A: 字节打印流:printStream
字符打印流:printWriter
B:能直接对文本文件操作的流对象
FileInputStream
FileOutputStream
FileReader
FileWriter
PrintStream
PrintWriter
C:打印流的自动刷新功能
PrintWriter pw = new PrintWriter(new FileWriter("aaa.txt"),true);
必须配合println()方式使用才可以,可同时实现自动刷新和换行的功能
此时println()等价于bw.write(); bw.newLine(); bw.flush();
D:使用打印流实现文件复制
BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
PrintWriter pw = new PrintlnWriter(new FileWriter("bbb.txt"));
String line = null;
while((line = br.readLine()) != null){
pw.println(line);
}
pw.close();
br.close();
9.标准输入输出流
A: in 标准输入流的使用
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个字符串:");
String line = br.readLine();
System.out.println("请输入一个整数:");
line = br.readLine();
int i = Integer.parseInt(line);
B: out 标准输出流的使用
原理:
System.out.println("hello");
PrintStream ps = System.out;
ps.println("hello");
使用:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(line);
10.序列化流
序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输
对象 --- 流数据(ObjectOutputStream)
反序列化流:把文本文件中的对象数据或者网络中的流对象数据还原成对象
流数据 --- 对象(ObjectInputStream)
案例:
public class Person implements Serializable {
private static final long serialVersionUID = -2071565876962058344L;
private String name;
private transient int age; //使用transient修饰表示不被序列化
....
}
测试类
write();
read();
private static void read() throws IOException,ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
Object obj = ois.readObject();
ois.close();
}
private static void writ() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
Person p = new Person("盖聂",40);
oos.writeObject(p);
oos.close();
}
11.Properties
Properties是一个集合类,Hashtable的子类
A:特有功能
public Object setProperty(String key,String value):添加数据
public String getProperty(String key):根据键找值
public Set<String> stringPropertyNames():返回键的集合
B:和IO流结合使用的方法
把键值对形式的文本文件内容加载到集合中
public void load(Reader reader)
public void load(InputStream inStream)
把集合中的数据存储到文本文件中
public void store(Writer writer,String comments)
public void store(OutputStream out,String comments)
C:根据给定的文本文件判断是否有键为:"盖聂",如果有就修改其值为:"卫庄"
Properties prop = new Properties();
Reader r = new FileReader("user.txt");
prop.load(r);
r.close();
Set<String> set = prop.StringPropertyNames();
for(String key : set){
if("盖聂".equals(key)){
prop.setProperty(key,"卫庄");
break;
}
}
Writer w = new FileWriter("user.txt");
prop.store(w,null);
w.close(); |
|