一、实验目的
1、了解流、输入/输出流的概念。
2、掌握常见的输入/输出流类及其主要方法。
3、掌握用I/O流实现文件读/写的方法。
二、实验内容
1.编写程序将从键盘上输入的一行内容写入到文件copyfile.txt中,文件的保存位置自己确定。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.next();
//将字符串转化为byte数组
byte[] b=str.getBytes();
//创建输出流
FileOutputStream f=null;
try {
f=new FileOutputStream("C:\\JavaExercise\\copyfile.txt");
//开始写
f.write(b);
//输出流最后要刷新
f.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
f.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 选择一个含有中文注释的.java文件,使用FileInputStream和FileReader分别读取文件,并把内容显示在控制台上。然后使用FileOutputStream和FileWriter将文件分别将文件复制到另一个文件copyfile1.txt和 copyfile2.txt中,文件的保存位置自己确定。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2_1 {
public static void main(String[] args) {
//先创建流
FileInputStream fi=null;
FileOutputStream fo=null;
try {
fi=new FileInputStream("D:\\Users\\Administrator\\eclipse-workspace\\FirstProject\\src\\com\\java\\exer7\\Test1.java");
fo=new FileOutputStream("C:\\JavaExercise\\copyfile2.txt");
//边读边拷贝
//准备byte数组
byte[] b=new byte[1024];
int readCount=0;
while((readCount=fi.read(b))!=-1) {
System.out.println(new String(b, 0, readCount));
fo.write(b);
}
//刷新输出流
fo.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
if(fi!=null) {
try {
fi.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fo!=null) {
try {
fi.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test2_2 {
public static void main(String[] args) {
//先创建流
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
fileReader = new FileReader("D:\\Users\\Administrator\\eclipse-workspace\\FirstProject\\src\\com\\java\\exer7\\Test1.java");
fileWriter = new FileWriter("C:\\JavaExercise\\copyfile1.txt");
//准备char数组
char[] chars = new char[4];
int readCount = 0;
//边读边写
while((readCount = fileReader.read(chars)) != -1){
System.out.print(new String(chars,0,readCount));
fileWriter.write(chars);
}
//刷新
fileWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter != null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.利用上机6中第3题 的Book类,创建10个相应的对象,写入到文件book2.dat中;再从文件book2.dat中读出copy到文件book3.dat中。
import java.io.Serializable;
import java.util.*;
public class Book implements Comparable<Book>, Serializable {
private String isbn;
private String title;
private String author;
private final static Book[] books = generateData();
public static Book[] generateData () {
Book[] books = new Book[10];
for (int i = 0; i < 10; i++) {
Book book = new Book(i + 1 + "", "书本" + i, "biubiu");
books[i] = book;
}
return books;
}
public static void linkedList() {
List<Book> bookLinkedList = new LinkedList<>();
for (int i = 0; i < books.length; i++) {
bookLinkedList.add(books[i]);
}
bookLinkedList.stream().forEach(System.out::println);
}
public static void arrayList() {
List<Book> bookArrayList = new ArrayList<>();
for (int i = 0; i < books.length; i++) {
bookArrayList.add(books[i]);
}
bookArrayList.stream().forEach(System.out::println);
}
public static void hashSet() {
Set<Book> bookHashSet = new HashSet<>();
for (int i = 0; i < books.length; i++) {
bookHashSet.add(books[i]);
}
bookHashSet.stream().forEach(System.out::println);
}
public static void hasMap() {
Map<String,Book> bookHashMap = new HashMap<>();
for (int i = 0; i < books.length; i++) {
bookHashMap.put(books[i].isbn,books[i]);
}
for (String key : bookHashMap.keySet()) {
System.out.println(bookHashMap.get(key));
}
}
public static void treeSet() {
TreeSet<Book> treeSet = new TreeSet();
for (int i = 0; i < books.length; i++) {
treeSet.add(books[i]);
}
treeSet.stream().forEach(System.out::println);
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"isbn='" + isbn + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
'}';
}
@Override
public int compareTo(Book o) {
return this.isbn.compareTo(o.isbn);
}
public static void main(String[] args) {
System.out.println("linkedList");
linkedList();
System.out.println("arrayList");
arrayList();
System.out.println("hashSet");
hashSet();
System.out.println("hasMap");
hasMap();
System.out.println("treeSet");
treeSet();
}
}