- 实验目的
1. 理解I/O流的概念,掌握其分类
2. 掌握文本文件读写、二进制文件读写
- 实验内容
1. 分别使用FileWriter 和 BufferedWriter 往文件中写入1万个随机数,比较用时的多少?(用时采用方法System.currentTimeMillis())求时间差;
使用:FileWriter
package project9;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class FWrite {
public static void main(String[] args) throws IOException{
double t1=System.currentTimeMillis();
FileWriter f=new FileWriter("f1.txt");
Random r=new Random();
for(int i=0;i<10000;i++) {
f.write(r.nextInt(100)+" ");//随机写入10000个0——100的数
}
f.close();
double t2=System.currentTimeMillis();
System.out.println("时间差为:"+(t2-t1)+"ms");
}
}
使用:BufferedWriter
package project9;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class BWrite {
public static void main(String[] args) throws IOException{
double t1=System.currentTimeMillis();
FileWriter f=new FileWriter("f2.txt");
BufferedWriter b=new BufferedWriter(f);
Random r=new Random();
for(int i=0;i<10000;i++) {
b.write(r.nextInt(100)+" ");
}
b.close();
double t2=System.currentTimeMillis();
System.out.println("时间差为:"+(t2-t1)+"ms");
}
}
2.课本P547,19.2 :编写一个文件,如果文件Exercise 19_2.dat 不存在,就创建一个名为Exercise 19_2.txt的文件。向这个文件追加新数据。使用二进制I/O将100个随机生成的整数写入这个文件中。
package project9;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Exercise {
public static void main(String[] args) throws IOException{
File f=new File("Exercise 19_2.dat");
if(!f.exists()) {
f=new File("Exercise 19_2.txt");
}
PrintWriter p=new PrintWriter(f);
Random r=new Random();
for(int i=0;i<100;i++) {
p.print(r.nextInt(10000)+" ");
}
p.close();
}
}
3.(对二进制数据文件中的所有数据求和)假设已经使用DataOutputStream中的writeInt(int)创建一个名为Exercise 19_3.dat的二进制数据文件,文件包含数目不确定的整数,编写一个程序来计算这些整数的总和。
package project9;
import java.util.*;
import java.io.*;
public class Sum {
public static void main(String[] args) throws IOException{
DataOutputStream output=new DataOutputStream(new FileOutputStream("Exercise 19_3.dat"));
DataInputStream input=new DataInputStream(new FileInputStream("Exercise 19_3.dat"));
Random r=new Random();
int s=0;
int n=r.nextInt(50)+10;
System.out.println("文件包含"+n+"个整数");
for(int i=0;i<n;i++) {
int m=r.nextInt(100);
output.writeInt(m);
System.out.print(input.readInt()+" ");//读出文件的内容并输出
s+=m;
}
output.close();
input.close();
System.out.println();
System.out.println("它们的和为:"+s);
}
}
4.在文本文件bigbook.txt中包含有很长篇幅的英语短文,编写程序要求统计文件的所有短文中包含英文字母“A”的个数,并显示统计的时间。
package project9;
import java.io.File;
import java.util.Scanner;
public class Bigbook {
public static void main(String[] args) throws Exception {
double t1=System.currentTimeMillis();
int n=0;
File file = new File("bigbook.txt");
Scanner input=new Scanner(file);
while(input.hasNext()) {//一定要另存为文件,把编码格式改为ANSI!否则返回值是假
String s;
s=input.nextLine();
System.out.println(s);//输出读取的内容
n+=sort(s);
}
input.close();
double t2=System.currentTimeMillis();
System.out.println("有"+n+"个A");
System.out.println("用时为:"+(t2-t1)+"ms");
}
public static int sort(String s) {
char a='A';
int n=0;
for(int i=0;i<s.length();i++)
if(a==s.charAt(i))
n++;
return n;
}
}
5.编写一个Java应用程序,给当前的类所在的文件ReadExample.java添加行号拷贝到另一个文件temp.text。
package project9;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class ReadExample {
public static void main(String[] args) throws Exception {
String s;
BufferedReader r=new BufferedReader(new FileReader("src\\project9\\ReadExample.java"));
BufferedWriter w=new BufferedWriter(new FileWriter("temp.text"));
for (int i = 1;(s=r.readLine())!= null ; i++) {
w.write(i+":"+s+"\n");
}
r.close();
w.close();
}
}
(将对象和数组存储在文件中)编写一个程序,向一个名为Exercise19_5.dat的文件中存储一个含5个int值1,2,3,4,5的数组,存储一个表示当前时间的Date对象,存储一个double值5.5
package project9;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class ObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException{//用readObject必须抛出ClassNotFoundException
int[] a= {1,2,3,4,5};
double b=5.5;
//写
@SuppressWarnings("resource")
ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("Exercise19_5.dat"));
output.writeDouble(b);//要先写非对象类型的,否则会报错
output.writeObject(a);
output.writeObject(new Date());
//读
@SuppressWarnings("resource")
ObjectInputStream input=new ObjectInputStream(new FileInputStream("Exercise19_5.dat"));
double e=input.readDouble();
int[] c=(int[])(input.readObject());
Date d=(Date)(input.readObject());
System.out.println("数组为:");
for(int i=0;i<c.length;i++) {
System.out.print(c[i]+" ");
}
System.out.println();
System.out.println("当前时间:"+d);
System.out.println("double值:"+e);
output.close();
input.close();
}
}
(组合文件)编写一个工具程序,使它能够用下面的命令,将文件组合在一起构成一个新文件:
java Exercise19_12 SourceFile1…SourceFilen TargetFile
这个命令将 SourceFile1,…,SourceFilen合并为TargetFile。
package project9;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class Merge {
public static void main(String[] args) throws IOException {
Scanner input=new Scanner(System.in);
System.out.println("你想合并几个文件?");
int n=input.nextInt();
File[] SourceFile=new File[n];
for(int i=0;i<n;i++) {
SourceFile[i]=new File("SourceFile"+(i+1)+".txt");
PrintWriter p=new PrintWriter(SourceFile[i]);
Random r=new Random();
for(int j=0;j<10;j++) {
p.print(r.nextInt(100)+" ");//随机生成10个数写入每个文件
}
p.close();
}
BufferedWriter w=new BufferedWriter(new FileWriter("TargetFile.txt"));
for(int i=0;i<n;i++) {
BufferedReader r=new BufferedReader(new FileReader(SourceFile[i]));
w.write(r.readLine()+"\n");
r.close();
}
w.close();
}
}
设计一个复数类Complex,并在该类中实现将对象自身保存到文件的功能,实例方法void save(String fileName);从文件恢复数据的静态方法static Complex load(String fileName)
package project9;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Complex {
private double real;
private String vir;
@SuppressWarnings("static-access")
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Complex c1=new Complex(4,"+5i");
c1.save("complex.txt");
System.out.println((c1.load("complex.txt")).getComplex());
}
public Complex() {
}
public Complex(double real,String vir) {
this.real=real;
this.vir=vir;
}
public String getComplex() {
return real+vir;
}
void save(String filename) throws IOException {
DataOutputStream output=new DataOutputStream(new FileOutputStream(filename));
//ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(filename));
output.writeDouble(real);
output.writeUTF(vir);
}
static Complex load(String fileName) throws ClassNotFoundException, IOException {
DataInputStream input=new DataInputStream(new FileInputStream(fileName));
//ObjectInputStream input=new ObjectInputStream(new FileInputStream(fileName));
//System.out.println(input.readDouble());
double n=input.readDouble();
String s=input.readUTF();
Complex c=new Complex(n,s);
return c;
}
}