Java之IO输入输出

 

首先介绍File类:

我们直接上代码:

 1 package com.learn.chap10.sec02;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 /**
 7  * 测试创建目录和文件
 8  * @author Administrator
 9  *
10  */
11 public class TestFile {
12     public static void main(String[] args) throws IOException {
13         File file=new File("E://java创建目录");
14         boolean b = file.mkdir();
15         if(b){
16             System.out.println("创建目录成功!");
17             file=new File("E://java创建目录//test.txt");
18             boolean m = file.createNewFile();
19             if(m){
20                 System.out.println("创建文件成功!");
21             }else{
22                 System.out.println("创建文件失败!");
23             }
24         }else {
25             System.out.println("创建目录失败!");
26         }
27     }
28 }
 1 package com.learn.chap10.sec02;
 2 
 3 import java.io.File;
 4 
 5 public class TestFile1 {
 6     public static void main(String[] args) {
 7         File file = new File("E://java创建目录//test.txt");
 8         if(file.exists()){ // 假如文件存在
 9             boolean a = file.delete(); // 删除文件
10             if(a){
11                 System.out.println("文件删除成功!");
12             }else {
13                 System.out.println("文件删除失败!");
14             }
15         }
16         
17         file = new File("E://java创建目录");
18         if(file.exists()){ // 假如目录存在
19             boolean m = file.delete(); // 删除目录(注意:delete只能删除空目录)
20             if(m){
21                 System.out.println("目录删除成功!");
22             }else {
23                 System.out.println("目录删除失败!");
24             }
25         }
26         
27     }
28 }
 1 package com.learn.chap10.sec02;
 2 
 3 import java.io.File;
 4 
 5 public class TestFile2 {
 6     public static void listFile(File file){
 7         if(file != null){
 8             if(file.isDirectory()){
 9                 File[] a = file.listFiles(); // 遍历目录
10                 if(a != null){
11                     for (int i = 0; i < a.length; i++) {
12                         listFile(a[i]); // 递归调用
13                     }
14                 }
15             }else {
16                 System.out.println(file);
17             }
18         }
19     }
20     
21     public static void main(String[] args) {
22         listFile(new File("E://mywamp//apache//conf//original"));
23     }
24 }

再介绍字节流:

上下代码:

 1 package com.learn.chap10.sec03;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 
 6 import java.io.InputStream;
 7 /**
 8  * 字节流---文件输入流测试(流入内存:从磁盘读取至内存)
 9  * 输入、输出是针对内存 (举例: 输入流 即数据从硬盘流向内存,也可说是从硬盘读取数据至内存)
10  * 读取、写入是针对磁盘
11  * @author Administrator
12  *
13  */
14 public class Demo1 {
15     public static void main(String[] args) throws Exception {
16         File file=new File("E://测试文件.txt");
17         InputStream inputStream = new FileInputStream(file);
18         InputStream inputStream1 = new FileInputStream(file);
19         InputStream inputStream2 = new FileInputStream(file);
20         // 读取数据方法一
21         byte[] b=new byte[1024];
22         inputStream.read(b);
23         
24         // 读取数据方法二
25         int fileLength1 = (int)file.length();
26         byte[] b1=new byte[fileLength1];
27         inputStream1.read(b1);
28         
29         // 读取数据方法三
30         int fileLength = (int)file.length();
31         byte[] m=new byte[fileLength];
32         int temp =0;
33         int len=0;
34         while((temp=inputStream2.read()) != -1){
35             m[len++] = (byte)temp;
36         }
37         
38         
39         inputStream.close(); // 关闭输入流
40         inputStream1.close(); // 关闭输入流
41         inputStream2.close(); // 关闭输入流
42         System.out.println("读取的内容为:"+new String(b));
43         System.out.println("读取的内容为:"+new String(b1));
44         System.out.println("读取的内容为:"+new String(m));
45     }


46 }





1
package com.learn.chap10.sec03; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.OutputStream; 6 /** 7 * 字节流---文件输出流测试 8 * @author Administrator 9 * 10 */ 11 public class Demo2 { 12 public static void main(String[] args) throws Exception { 13 File file=new File("E://测试文件.txt"); 14 OutputStream out = new FileOutputStream(file,true);// 加true 表示追加 15 String string="呵呵呵呵12"; 16 byte[] b=string.getBytes(); 17 out.write(b); 18 out.close(); 19 } 20 }

 

 

 1 package com.learn.chap10.sec03;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.InputStream;
 8 import java.io.OutputStream;
 9 
10 /**
11  * 字节流---缓冲流和非缓冲流比较
12  * @author Administrator
13  *
14  */
15 public class Demo3 {
16     /**
17      * 非缓存
18      * @throws Exception
19      */
20     public static void stream() throws Exception{
21         InputStream inputStream=new FileInputStream("E://测试文件1.txt");
22         OutputStream outputStream=new FileOutputStream("E://测试文件2.txt");
23         int b=0;
24         long start_time = System.currentTimeMillis(); // 开始时间
25         while((b=inputStream.read()) != -1){
26             outputStream.write(b);
27         }
28         inputStream.close();
29         outputStream.close();
30         long end_time = System.currentTimeMillis(); // 结束时间
31         System.out.println("非缓存花费时间:"+(end_time-start_time));
32         
33     }
34     
35     /**
36      * 缓冲流  节省时间  性能优
37      * @throws Exception
38      */
39     public static void buffer_stream() throws Exception {
40         // 定义了一个带缓冲的字节输入流
41         BufferedInputStream bInputStream = new BufferedInputStream(new FileInputStream("E://测试文件1.txt"));
42         // 定义了一个带缓冲的字节输出流
43         BufferedOutputStream bOutputStream = new BufferedOutputStream(new FileOutputStream("E://测试文件2.txt"));
44         int b=0;
45         long start_time = System.currentTimeMillis(); // 开始时间
46         while((b=bInputStream.read()) != -1){
47             bOutputStream.write(b);
48         }
49         bInputStream.close();
50         bOutputStream.close();
51         long end_time = System.currentTimeMillis(); // 结束时间
52         System.out.println("缓存花费时间:"+(end_time-start_time));
53     }
54     
55     public static void main(String[] args) throws Exception {
56         stream();
57         buffer_stream();
58     }
59 }

 

再介绍下字符流:

上下代码:

 1 package com.learn.chap10.sec04;
 2 
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.io.Reader;
 6 /**
 7  * 字符流----输入流
 8  * @author Administrator
 9  *
10  */
11 public class Demo1 {
12     public static void main(String[] args) throws Exception {
13         File file=new File("E://测试文件1.txt");
14         Reader reader=new FileReader(file);
15         char[] c=new char[1024]; // 字符数组
16         // 方法一
17         int len=reader.read(c);
18         
19         // 方法二
20         /*int temp=0;
21         int len=0;
22         while((temp=reader.read()) != -1){
23             c[len++] = (char)temp;
24         }*/
25         reader.close();
26         System.out.println("读取内容是:"+new String(c,0,len));
27         
28     }
29 }

运行结果:

读取内容是:gffdhgfhgjhgjhgjhk
kukhjkhjk gfhfghgjhjhkjhkjhkjhkjhkj

 

 1 package com.learn.chap10.sec04;
 2 
 3 import java.io.File;
 4 import java.io.FileWriter;
 5 import java.io.Writer;
 6 /**
 7  * 字符流----输出流
 8  * @author Administrator
 9  *
10  */
11 public class Demo2 {
12     public static void main(String[] args) throws Exception {
13         File file=new File("E://测试文件1.txt");
14         Writer outWriter = new FileWriter(file,true);
15         String string="adffdsf";
16         outWriter.write(string); // 将字符串写入输出流
17         outWriter.close(); // 关闭数据流
18     }
19 }

 

posted on 2016-10-28 07:42  eaglezb  阅读(457)  评论(0编辑  收藏  举报

导航