分别使用字节流和字符流完成以下程序:
- 在指定的路径下新建一个 txt 文件 “aura.txt”,利用程序在文件中写入如下内容:
“Hadoop(hdfs、mapreduce、yarn) 元老级大数据处理技术框架,擅长离线数据分析,Hbase 分布式海量数据库,离线分析和在线业务通吃,Hive sql 数据仓库工具,使用方便,功能丰富,基于MR延迟大,Sqoop数据导入导出工具,Flume数据采集框架,Storm 实时流式计算框架,流式处理领域头牌框架,Spark 基于内存的分布式运算框架,一站式处理 all in one,新秀,发展势头迅猛”
2. 利用程序读取 aura.txt 文件的内容, 并在控制台打印
3. 利用程序复制 aura.txt 为 aura1.txt
public class TestIO {
//字节流
@Test
public void test1() throws IOException {
File f1 = new File("f:/test/auara.txt");
f1.createNewFile();
//FileInputStream fis= new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f1);
fos.write("Hadoop(hdfs、mapreduce、yarn) 元老级大数据处理技术框架,擅长离线数据分析,Hbase 分布式海量数据库,离线分析和在线业务通吃,Hive sql 数据仓库工具,使用方便,功能丰富,基于MR延迟大,Sqoop数据导入导出工具,Flume数据采集框架,Storm 实时流式计算框架,流式处理领域头牌框架,Spark 基于内存的分布式运算框架,一站式处理 all in one,新秀,发展势头迅猛".getBytes());
fos.close();
}
//字符流
@Test
public void test2() throws IOException {
File r1 = new File("f:/test/reader.txt");
r1.createNewFile();
FileWriter fw = new FileWriter(r1);
fw.write("Hadoop(hdfs、mapreduce、yarn) 元老级大数据处理技术框架,擅长离线数据分析,Hbase 分布式海量数据库,离线分析和在线业务通吃,Hive sql 数据仓库工具,使用方便,功能丰富,基于MR延迟大,Sqoop数据导入导出工具,Flume数据采集框架,Storm 实时流式计算框架,流式处理领域头牌框架,Spark 基于内存的分布式运算框架,一站式处理 all in one,新秀,发展势头迅猛\r\n");
fw.close();
}
//利用程序读取 aura.txt 文件的内容, 并在控制台打印
@Test
public void test5() throws IOException {
FileInputStream fis = new FileInputStream("f:/test/auara.txt");
//FileOutputStream fos = new FileOutputStream(f1);
byte[] bytes = new byte[1024];
int len ;
while((len=fis.read(bytes))!=-1) {
String s= new String(bytes, 0, len);
System.out.println(s);
}
//fos.close();
fis.close();
}
@Test
public void test6() throws IOException {
FileReader fr = new FileReader("f:/test/reader.txt");
char [] ch = new char [512];
int len ;
while ((len=fr.read(ch))!=-1) {
String s = new String(ch, 0, len);
System.out.println(s);
}
fr.close();
}
//字节流复制
@Test
public void test3() throws IOException {
FileInputStream fis = new FileInputStream("f:/test/reader.txt");
FileOutputStream fos = new FileOutputStream("f:/test/reader1.txt");
byte [] bytes = new byte[512];
int len ;
while ((len = fis.read(bytes))!=-1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
//字符流复制
@Test
public void test4() throws IOException {
FileReader fr = new FileReader ("f:/test/reader.txt");
FileWriter fe = new FileWriter("f:/test/reader2.txt");
char []ch = new char[1024];
int len ;
while ((len=fr.read(ch))!=-1) {
fe.write(ch, 0, len);
}
fe.close();
fr.close();
}
}