实验目的
1.理解Java技术体系中“流“的概念。
2.掌握System.in和System.out的基本使用方法
3.掌握字节流和字符流的常用类及使用方法。
4.掌握Java程序对文件的基本操作。
实验学时
4学时
实验内容
- 运行下列程序,分析程序的功能。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* @author
* @function 测试2种输入流方式的性能
*/
public class InputTest {
public static void main(String[] args) throws IOException {
InputTest inputTest = new InputTest();
inputTest.testScanner();
inputTest.testBufferReader();
}
public void testScanner(){
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
long start = System.currentTimeMillis();
System.out.println(str);
while(str.length()!=0){
str=sc.nextLine();
System.out.println(str);
}
long end = System.currentTimeMillis();
System.out.println("testScanner:"+(end - start) + "ms");
}
public void testBufferReader() throws IOException{
InputStreamReader inStream= new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(inStream);
String str = bf.readLine();
long start = System.currentTimeMillis();
System.out.println(str);
while(str.length()!=0){
str = bf.readLine();
System.out.println(str);
}
long end = System.currentTimeMillis();
System.out.println("testBufferReader:"+(end - start) + "ms");
}
}
分析:程序的功能是返回程序员自键盘输入到按下回车键所用的时间(毫秒数)。
如下,输入一个”1”后立马回车比缓慢输入一个”baby”所用时间要短。
运行结果:
- 运行下列程序,分析程序的功能。
import java.io.File;
import java.io.IOException;
public class DirOperation {
public static void main(String[] args) throws IOException {
//在D盘下创建一个文件为HelloWorld.txt文件
File file=new File("D:","HelloWorld.txt");
boolean isCreate;
try {
isCreate = file.createNewFile();
if (isCreate) {
System.out.println("创建文件成功!");
}else {
System.out.println("创建文件失败!文件已经存在");
}
} catch (IOException e) {
System.out.println("创建文件失败!");
}
// 判断是文件还是目录,
if (file.isFile()) {
System.out.println("这是一个文件");
} else {
System.out.println("这是一个目录");
}
//再创建一个目录out
File file2=new File("D:/out");
file2.mkdirs();
File file3=new File("D:/out","sample.txt");
file3.createNewFile();
//HelloWorld.txt移动到Out目录下
if (file.renameTo(new File(file2.getPath()+ "/"+file.getName()))){ System.out.println("文件移动成功!");
} else {
System.out.println("文件移动失败");
}
//遍历Out目录下的文件
String[] arr=file2.list();
for (String string : arr) {
System.out.println(string);
}
}
}
分析:通过实例化File对象在D盘下创建一个HelloWorld.txt文件,并使用try-catch语句判断文件是否创建成功,然后再在D盘下创建一个out目录,将文件移到out目录下并遍历。
该程序的功能是将程序员创建的文件移动到另一个目录下。
运行结果:
- 用两种方法实现文件的拷贝功能。
(1)用FileInputStream/FileOutputStream
实验代码:
```perl
package test9;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
File file1=new File("E:\\test.txt");
File file2=new File("out.txt");
try{
FileInputStream f=new FileInputStream(file1);
FileOutputStream out=new FileOutputStream(file2);
byte[] c=new byte[1024];
int len=0;
while((len=f.read(c))!=-1){
out.write(c,0,len);
}
System.out.println("文件拷贝成功");
f.close();
out.flush();
out.close();
}
catch(IOException e){
System.out.println("文件拷贝失败:" + e.getMessage());
}
}
}
(2)用BufferedInputStream/BufferedOutputStream
实验代码:
package test9;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferCopy {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
File file1=new File("E:\\test.txt");
File file2=new File("out.txt");
try{
FileInputStream f=new FileInputStream(file1);
FileOutputStream fout=new FileOutputStream(file2);
BufferedInputStream b=new BufferedInputStream(f);
BufferedOutputStream bout=new BufferedOutputStream(fout);
byte[] c=new byte[1024];
int len=0;
while((len=b.read(c))!=-1){
bout.write(c,0,len);
}
System.out.println("文件拷贝成功");
f.close();
fout.close();
b.close();
bout.flush();
bout.close();
}
catch(IOException e){
System.out.println("文件拷贝失败:"+e.getMessage());
}
}
}
运行结果: