码迷,mamicode.com
首页 > 编程语言 > 详细

Java 文件读写操作

时间:2017-11-28 23:11:31      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:pch   images   bsp   输入流   play   只读   handle   int   new   

1【1】按字节读写,一次只读取一个字节,效率比较低

 1 package bk1;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 
10 public class Dx1 {
11     public void display() //按字节读取文件内容,每次只能读取一个字节
12     {
13         File file =new File("课表.txt");//用来传文件的名字
14         InputStream in = null;
15         try{
16         in=new FileInputStream(file); 
17             
18             
19             
20         
21         int zijie;
22         System.out.println("按字节读出的结果");
23         while((zijie=in.read())!=-1)
24         {
25             System.out.write(zijie);
26         }
27         in.close();
28         }
29         catch (IOException e) {
30             // TODO: handle exception
31             System.out.println(e.getMessage());
32             return;//退出程序
33         }
34     }
35 
36     public static void main(String[] args) throws IOException {
37         // TODO Auto-generated method stub
38         Dx1 D=new Dx1();
39         D.display();
40     }
41 
42 }

【2】按字节读写,一次可以读写多个字节

 1 package bk1;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 
 8 public class Dx2 {
 9     public void display()
10 
11     {
12         File file =new File("课表.txt");
13         InputStream in = null;
14         try {
15             System.out.println("以字节为单位读取文件内容,一次读多个字节:");
16             // 一次读多个字节
17             byte[] tempbytes = new byte[100];
18             int byteread = 0;
19             in = new FileInputStream("课表.txt");
20             Dx2.showAvailableBytes(in);
21             // 读入多个字节到字节数组中,byteread为一次读入的字节数
22             while ((byteread = in.read(tempbytes)) != -1) {
23                 System.out.write(tempbytes, 0, byteread);
24             }
25         } catch (Exception e1) {
26             e1.printStackTrace();
27         } finally {
28             if (in != null) {
29                 try {
30                     in.close();
31                 } catch (IOException e1) {
32                 }
33             }
34         }
35     }
36     static void showAvailableBytes(InputStream in) {
37         try {
38             System.out.println("当前字节输入流中的字节数为:" + in.available());
39         } catch (IOException e) {
40             e.printStackTrace();
41         }
42     }
43     
44     public static void main(String[] args) {
45         // TODO Auto-generated method stub
46         Dx2 D=new Dx2();
47         D.display();
48     }
49 
50 }

2【1】以字符为单位读取文件内容,一次读一个字符

【2】以字符为单位读取文件内容,一次读多个字符

 1 package bk1;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.io.Reader;
 8 
 9 public class Dx3 {
10     public static void readFileByChars(String fileName) {
11         File file = new File("课表.txt");
12         Reader reader = null;
13         try {
14             System.out.println("以字符为单位读取文件内容,一次读一个字符:");
15             // 一次读一个字符
16             
17             reader=new InputStreamReader(new FileInputStream(file));
18             int tempchar;
19             while ((tempchar = reader.read()) != -1) {
20                 // 对于windows下,rn这两个字符在一起时,表示一个换行。
21                 // 但如果这两个字符分开显示时,会换两次行。
22                 // 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。
23                 if (((char) tempchar) != ‘r‘) {
24                     System.out.print((char) tempchar);
25                 }
26             }
27             reader.close();
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31         try {
32             System.out.println("以字符为单位读取文件内容,一次读多个字符:");
33             // 一次读多个字符
34             char[] tempchars = new char[30];
35             int charread = 0;
36             reader = new InputStreamReader(new FileInputStream("课表.txt"));
37             // 读入多个字符到字符数组中,charread为一次读取字符数
38             while ((charread = reader.read(tempchars)) != -1) {
39                 // 同样屏蔽掉r不显示
40                 if ((charread == tempchars.length)
41                         && (tempchars[tempchars.length - 1] != ‘r‘)) {
42                     System.out.print(tempchars);
43                 } else {
44                     for (int i = 0; i < charread; i++) {
45                         if (tempchars[i] == ‘r‘) {
46                             continue;
47                         } else {
48                             System.out.print(tempchars[i]);
49                         }
50                     }
51                 }
52             }
53         } catch (Exception e1) {
54             e1.printStackTrace();
55         } finally {
56             if (reader != null) {
57                 try {
58                     reader.close();
59                 } catch (IOException e1) {
60                 }
61             }
62         }
63     }
64 
65     public static void main(String[] args) {
66         // TODO Auto-generated method stub
67         Dx3.readFileByChars("课表.txt");
68     }
69 
70 }

截图:

技术分享图片

技术分享图片

文件中的r字符无法读出。

3以行为单位读取文件内容,一次读一整行

 1 package bk1;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 
 8 public class Dx4 {
 9     public static void readFileByLines(String fileName) {
10         File file = new File("课表.txt");
11         BufferedReader reader = null;
12         try {
13             System.out.println("以行为单位读取文件内容,一次读一整行:");
14             reader = new BufferedReader(new FileReader(file));
15             String tempString = null;
16             int line = 1;
17             // 一次读入一行,直到读入null为文件结束
18             while ((tempString = reader.readLine()) != null) {
19                 // 显示行号
20                 System.out.println("line " + line + ": " + tempString);
21                 line++;
22             }
23             reader.close();
24         } catch (IOException e) {
25             e.printStackTrace();
26         } finally {
27             if (reader != null) {
28                 try {
29                     reader.close();
30                 } catch (IOException e1) {
31                 }
32             }
33         }
34     }
35 
36     public static void main(String[] args) {
37         // TODO Auto-generated method stub
38         Dx4.readFileByLines("课表.txt");
39 
40     }
41 
42 }

4随机读取一段文件内容

package bk1;

import java.io.IOException;
import java.io.RandomAccessFile;

public class Dx5 {
    public static void readFileByRandomAccess(String fileName) {
        RandomAccessFile randomFile = null;
        try {
            System.out.println("随机读取一段文件内容:");
            // 打开一个随机访问文件流,按只读方式
            randomFile = new RandomAccessFile("课表.txt", "r");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            // 读文件的起始位置
            int beginIndex = (fileLength > 4) ? 4 : 0;
            // 将读文件的开始位置移到beginIndex位置。
            randomFile.seek(beginIndex);
            byte[] bytes = new byte[10];
            int byteread = 0;
            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
            // 将一次读取的字节数赋给byteread
            while ((byteread = randomFile.read(bytes)) != -1) {
                System.out.write(bytes, 0, byteread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Dx5.readFileByRandomAccess("课表.txt");
    }

}

 

Java 文件读写操作

标签:pch   images   bsp   输入流   play   只读   handle   int   new   

原文地址:http://www.cnblogs.com/zyt-bg/p/7912553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!