java中常用的文件操作方法

来源:互联网 发布:c语言求n的阶乘 编辑:程序博客网 时间:2024/06/06 01:32

 java的File的常用操作方法总结。 

1.以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 

2.以字符为单位读取文件,常用于读文本,数字等类型的文件 

3.以行为单位读取文件,常用于读面向行的格式化文件 

4.随机读取文件内容 

5.RandomAccessFile追加文件 

6.FileWriter追加文件

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.zuidaima.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.InputStreamReader;  
  11. import java.io.RandomAccessFile;  
  12. import java.io.Reader;  
  13.   
  14. /** 
  15.  * 常用的文件操作类 
  16.  *  
  17.  *  
  18.  */  
  19. public class FileUtil {  
  20.   
  21.     /** 
  22.      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 
  23.      *  
  24.      * @param fileName 
  25.      *            文件的名 
  26.      */  
  27.     public static void readFileByBytes(String fileName) {  
  28.         File file = new File(fileName);  
  29.         InputStream in = null;  
  30.         try {  
  31.             System.out.println("以字节为单位读取文件内容,一次读一个字节:");  
  32.             // 一次读一个字节  
  33.             in = new FileInputStream(file);  
  34.             int tempbyte;  
  35.             while ((tempbyte = in.read()) != -1) {  
  36.                 System.out.write(tempbyte);  
  37.             }  
  38.             in.close();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.             return;  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 
  47.      *  
  48.      * @param fileName 
  49.      *            文件的名 
  50.      */  
  51.     public static void readFileByByte(String fileName) {  
  52.         File file = new File(fileName);  
  53.         InputStream in = null;  
  54.         try {  
  55.             System.out.println("以字节为单位读取文件内容,一次读多个字节:");  
  56.             // 一次读多个字节  
  57.             byte[] tempbytes = new byte[100];  
  58.             int byteread = 0;  
  59.             in = new FileInputStream(file);  
  60.             showAvailableBytes(in);  
  61.             // 读入多个字节到字节数组中,byteread为一次读入的字节数  
  62.             while ((byteread = in.read(tempbytes)) != -1) {  
  63.                 System.out.write(tempbytes, 0, byteread);  
  64.             }  
  65.         } catch (Exception e1) {  
  66.             e1.printStackTrace();  
  67.         } finally {  
  68.             if (in != null) {  
  69.                 try {  
  70.                     in.close();  
  71.                 } catch (IOException e1) {  
  72.                 }  
  73.             }  
  74.         }  
  75.     }  
  76.   
  77.     /** 
  78.      * 以字符为单位读取文件,常用于读文本,数字等类型的文件 
  79.      *  
  80.      * @param fileName 
  81.      *            文件名 
  82.      */  
  83.     public static void readFileByChar(String fileName) {  
  84.         File file = new File(fileName);  
  85.         Reader reader = null;  
  86.   
  87.         try {  
  88.             System.out.println("以字符为单位读取文件内容,一次读多个字节:");  
  89.             // 一次读多个字符  
  90.             char[] tempchars = new char[30];  
  91.             int charread = 0;  
  92.             reader = new InputStreamReader(new FileInputStream(file));  
  93.             // 读入多个字符到字符数组中,charread为一次读取字符数  
  94.             while ((charread = reader.read(tempchars)) != -1) {  
  95.                 // 同样屏蔽掉r不显示  
  96.                 if ((charread == tempchars.length)  
  97.                         && (tempchars[tempchars.length - 1] != 'r')) {  
  98.                     System.out.print(tempchars);  
  99.                 } else {  
  100.                     for (int i = 0; i < charread; i++) {  
  101.                         if (tempchars[i] == 'r') {  
  102.                             continue;  
  103.                         } else {  
  104.                             System.out.print(tempchars[i]);  
  105.                         }  
  106.                     }  
  107.                 }  
  108.             }  
  109.         } catch (Exception e1) {  
  110.             e1.printStackTrace();  
  111.         } finally {  
  112.             if (reader != null) {  
  113.                 try {  
  114.                     reader.close();  
  115.                 } catch (IOException e1) {  
  116.                 }  
  117.             }  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * 以字符为单位读取文件,常用于读文本,数字等类型的文件 
  123.      *  
  124.      * @param fileName 
  125.      *            文件名 
  126.      */  
  127.     public static void readFileByChars(String fileName) {  
  128.         File file = new File(fileName);  
  129.         Reader reader = null;  
  130.         try {  
  131.             System.out.println("以字符为单位读取文件内容,一次读多个字节:");  
  132.             // 一次读多个字符  
  133.             char[] tempchars = new char[30];  
  134.             int charread = 0;  
  135.             reader = new InputStreamReader(new FileInputStream(file));  
  136.             // 读入多个字符到字符数组中,charread为一次读取字符数  
  137.             while ((charread = reader.read(tempchars)) != -1) {  
  138.                 // 同样屏蔽掉r不显示  
  139.                 if ((charread == tempchars.length)  
  140.                         && (tempchars[tempchars.length - 1] != 'r')) {  
  141.                     System.out.print(tempchars);  
  142.                 } else {  
  143.                     for (int i = 0; i < charread; i++) {  
  144.                         if (tempchars[i] == 'r') {  
  145.                             continue;  
  146.                         } else {  
  147.                             System.out.print(tempchars[i]);  
  148.                         }  
  149.                     }  
  150.                 }  
  151.             }  
  152.         } catch (Exception e1) {  
  153.             e1.printStackTrace();  
  154.         } finally {  
  155.             if (reader != null) {  
  156.                 try {  
  157.                     reader.close();  
  158.                 } catch (IOException e1) {  
  159.                 }  
  160.             }  
  161.         }  
  162.     }  
  163.   
  164.     /** 
  165.      * 以行为单位读取文件,常用于读面向行的格式化文件 
  166.      *  
  167.      * @param fileName 
  168.      *            文件名 
  169.      */  
  170.     public static void readFileByLines(String fileName) {  
  171.         File file = new File(fileName);  
  172.         BufferedReader reader = null;  
  173.         try {  
  174.             System.out.println("以行为单位读取文件内容,一次读一整行:");  
  175.             reader = new BufferedReader(new FileReader(file));  
  176.             String tempString = null;  
  177.             int line = 1;  
  178.             // 一次读入一行,直到读入null为文件结束  
  179.             while ((tempString = reader.readLine()) != null) {  
  180.                 // 显示行号  
  181.                 System.out.println("line " + line + ": " + tempString);  
  182.                 line++;  
  183.             }  
  184.             reader.close();  
  185.         } catch (IOException e) {  
  186.             e.printStackTrace();  
  187.         } finally {  
  188.             if (reader != null) {  
  189.                 try {  
  190.                     reader.close();  
  191.                 } catch (IOException e1) {  
  192.                 }  
  193.             }  
  194.         }  
  195.     }  
  196.   
  197.     /** 
  198.      * 随机读取文件内容 
  199.      *  
  200.      * @param fileName 
  201.      *            文件名 
  202.      */  
  203.     public static void readFileByRandomAccess(String fileName) {  
  204.         RandomAccessFile randomFile = null;  
  205.         try {  
  206.             System.out.println("随机读取一段文件内容:");  
  207.             // 打开一个随机访问文件流,按只读方式  
  208.             randomFile = new RandomAccessFile(fileName, "r");  
  209.             // 文件长度,字节数  
  210.             long fileLength = randomFile.length();  
  211.             // 读文件的起始位置  
  212.             int beginIndex = (fileLength > 4) ? 4 : 0;  
  213.             // 将读文件的开始位置移到beginIndex位置。  
  214.             randomFile.seek(beginIndex);  
  215.             byte[] bytes = new byte[10];  
  216.             int byteread = 0;  
  217.             // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。  
  218.             // 将一次读取的字节数赋给byteread  
  219.             while ((byteread = randomFile.read(bytes)) != -1) {  
  220.                 System.out.write(bytes, 0, byteread);  
  221.             }  
  222.         } catch (IOException e) {  
  223.             e.printStackTrace();  
  224.         } finally {  
  225.             if (randomFile != null) {  
  226.                 try {  
  227.                     randomFile.close();  
  228.                 } catch (IOException e1) {  
  229.                 }  
  230.             }  
  231.         }  
  232.     }  
  233.   
  234.     /** 
  235.      * 显示输入流中还剩的字节数 
  236.      *  
  237.      * @param in 
  238.      */  
  239.     private static void showAvailableBytes(InputStream in) {  
  240.         try {  
  241.             System.out.println("当前字节输入流中的字节数为:" + in.available());  
  242.         } catch (IOException e) {  
  243.             e.printStackTrace();  
  244.         }  
  245.     }  
  246.   
  247.     /** 
  248.      * RandomAccessFile追加文件 
  249.      *  
  250.      * @param fileName 
  251.      *            文件名 
  252.      * @param content 
  253.      *            追加的内容 
  254.      */  
  255.     public static void appendMethodByRandomAccessFile(String fileName,  
  256.             String content) {  
  257.         System.out.println("RandomAccessFile追加文件");  
  258.         try {  
  259.             // 打开一个随机访问文件流,按读写方式  
  260.             RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");  
  261.             // 文件长度,字节数  
  262.             long fileLength = randomFile.length();  
  263.             // 将写文件指针移到文件尾。  
  264.             randomFile.seek(fileLength);  
  265.             randomFile.writeBytes(content);  
  266.             randomFile.close();  
  267.         } catch (IOException e) {  
  268.             e.printStackTrace();  
  269.         }  
  270.     }  
  271.   
  272.     /** 
  273.      * FileWriter追加文件 
  274.      *  
  275.      * @param fileName 
  276.      * @param content 
  277.      */  
  278.     public static void appendMethodByFileWriter(String fileName, String content) {  
  279.         System.out.println("appendMethodByFileWriter");  
  280.         try {  
  281.             // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件  
  282.             FileWriter writer = new FileWriter(fileName, true);  
  283.             writer.write(content);  
  284.             writer.close();  
  285.         } catch (IOException e) {  
  286.             e.printStackTrace();  
  287.         }  
  288.     }  
  289.   
  290.     public static void main(String[] args) {  
  291.         String path = FileUtil.class.getResource("").getFile();  
  292.         String fileName = path + "/temp.txt";  
  293.         FileUtil.readFileByByte(fileName);  
  294.         System.out.println("");  
  295.         FileUtil.readFileByBytes(fileName);  
  296.         System.out.println("");  
  297.         FileUtil.readFileByChar(fileName);  
  298.         System.out.println("");  
  299.         FileUtil.readFileByChars(fileName);  
  300.         System.out.println("");  
  301.         FileUtil.readFileByLines(fileName);  
  302.         System.out.println("");  
  303.         FileUtil.readFileByRandomAccess(fileName);  
  304.         System.out.println("");  
  305.         String content = "\nnew append RandomAccessFile!";  
  306.         FileUtil.appendMethodByRandomAccessFile(fileName, content);  
  307.         FileUtil.appendMethodByRandomAccessFile(fileName,  
  308.                 "\nappend end RandomAccessFile");  
  309.         FileUtil.readFileByLines(fileName);  
  310.         System.out.println("");  
  311.         FileUtil.appendMethodByFileWriter(fileName, content);  
  312.         FileUtil.appendMethodByFileWriter(fileName,  
  313.                 "\nappend end appendMethodByFileWriter");  
  314.         FileUtil.readFileByLines(fileName);  
  315.     }  
  316. }  
  317.   
  318.                       

完整代码地址:http://www.zuidaima.com/share/1550463416814592.htm
0 0
原创粉丝点击