实验三 文件与数据流 实验报告

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验三文件与数据流实验报告

一、实验目的

1. 掌握输入输出流类的层次关系和类中的方法及输入输出流的编程方法。

2. 理解对象序列化。

二、实验要求

1. 利用InputStream(抽象类)的子类FileInputStream以及BufferedInputStream类对文件进行读入操作。BufferedInputStream相对于FileInputStream类而言增加了缓存,减少了IO 操作,在一定程序上提升了程序的性能。同理,利用OutputStream的子类FileOutputStream 和BufferedOutputStream类进行文件的输出操作。

2. 通过DataInputStream和DataOutputStream对象进行基本数据类型的写入文件和从文件读出的操作。

3. 通过ObjectInputStream和ObjectOutputStream可以直接把对象写入到文件或者从文件中读出。同时,如果要使对象能够通过这个类写入到文件中,这个类需要实现Serializable 接口。

三、实验内容

1. 将文件f1.txt的内容加密(字母’A’ 变为’C’,字母’B’ 变为字母‘D’,…,字母‘Z’ 变为’B’)后存放到f

2.txt中。读出f2.txt文件的内容,并显示解密后的内容。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

public class FileExperiment00 {

public static void main(String[] args) {

try {

encrypt();

decrypt();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void encrypt() throws Exception {

File f1 = new File("f1.txt");

File f2 = new File("f2.txt");

if (!f1.exists()) {

System.out.println("文件不存在");

return;

}

if (!f2.exists()) {

f2.createNewFile();

}

FileInputStream in = new FileInputStream(f1);

BufferedInputStream bis = new BufferedInputStream(in);

FileOutputStream out = new FileOutputStream(f2);

BufferedOutputStream bos = new BufferedOutputStream(out);

int b;

while ((b = bis.read()) != -1) {

bos.write(Encrypt.encrypt((byte) b));

}

bos.close();

bis.close();

}

public static void decrypt() throws Exception {

File file = new File("f2.txt");

if (!file.exists()) {

System.out.println("文件不存在!");

return;

}

FileInputStream in = new FileInputStream(file);

BufferedInputStream bis = new BufferedInputStream(in);

byte[] buffer = new byte[2];

while (bis.read(buffer) > 0) {

buffer[0] = Encrypt.decrypt(buffer[0]);

buffer[1] = Encrypt.decrypt(buffer[1]);

System.out.print(new String(buffer));

}

bis.close();

}

}

class Encrypt {

public static byte encrypt(byte b) {

if ((b >= 65 && b <= 88) || (b >= 97 && b <= 120)) {

return (byte) (b + 2);

} else if ((b >= 89 && b <= 90) || (b >= 121 && b <= 122)) { return (byte) (b - 24);

} else {

return b;

}

}

public static byte decrypt(byte b) {

if ((b >= 67 && b <= 90) || (b >= 99 && b <= 122)) {

return (byte) (b - 2);

} else if ((b >= 65 && b <= 66) || (b >= 97 && b <= 98)) { return (byte) (b + 24);

} else {

return b;

}

}

}

2. 从键盘中输入若干个实数(以-999999.999结束),按大小顺序存放到d1.txt,并从该文件中读取数据,在屏幕上显示(每行输出5个数)

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Arrays;

import java.util.LinkedList;

import java.util.Scanner;

public class FileExperiment01 {

public static void main(String[] args) {

try {

getData();

readData();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void readData() throws Exception {

File file = new File("d1.txt");

DataInputStream in =

new DataInputStream(new FileInputStream(file));

double d = 0;

while (true) {

try {

d = in.readDouble();

System.out.println(d);

} catch (EOFException e) {

break;

}

}

相关文档
最新文档