楼主: yujianta14
507 0

Java文件读写 [推广有奖]

  • 0关注
  • 0粉丝

本科生

36%

还不是VIP/贵宾

-

威望
0
论坛币
130 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
1703 点
帖子
45
精华
0
在线时间
10 小时
注册时间
2016-4-18
最后登录
2016-6-27

楼主
yujianta14 发表于 2016-6-12 17:51:24 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. public class ReadFromFile {
  2.     /**
  3.      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  4.      */
  5.     public static void readFileByBytes(String fileName) {
  6.         File file = new File(fileName);
  7.         InputStream in = null;
  8.         try {
  9.             System.out.println("以字节为单位读取文件内容,一次读一个字节:");
  10.             // 一次读一个字节
  11.             in = new FileInputStream(file);
  12.             int tempbyte;
  13.             while ((tempbyte = in.read()) != -1) {
  14.                 System.out.write(tempbyte);
  15.             }
  16.             in.close();
  17.         } catch (IOException e) {
  18.             e.printStackTrace();
  19.             return;
  20.         }
  21.         try {
  22.             System.out.println("以字节为单位读取文件内容,一次读多个字节:");
  23.             // 一次读多个字节
  24.             byte[] tempbytes = new byte[100];
  25.             int byteread = 0;
  26.             in = new FileInputStream(fileName);
  27.             ReadFromFile.showAvailableBytes(in);
  28.             // 读入多个字节到字节数组中,byteread为一次读入的字节数
  29.             while ((byteread = in.read(tempbytes)) != -1) {
  30.                 System.out.write(tempbytes, 0, byteread);
  31.             }
  32.         } catch (Exception e1) {
  33.             e1.printStackTrace();
  34.         } finally {
  35.             if (in != null) {
  36.                 try {
  37.                     in.close();
  38.                 } catch (IOException e1) {
  39.                 }
  40.             }
  41.         }
  42.     }

  43.     /**
  44.      * 以字符为单位读取文件,常用于读文本,数字等类型的文件
  45.      */
  46.     public static void readFileByChars(String fileName) {
  47.         File file = new File(fileName);
  48.         Reader reader = null;
  49.         try {
  50.             System.out.println("以字符为单位读取文件内容,一次读一个字节:");
  51.             // 一次读一个字符
  52.             reader = new InputStreamReader(new FileInputStream(file));
  53.             int tempchar;
  54.             while ((tempchar = reader.read()) != -1) {
  55.                 // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
  56.                 // 但如果这两个字符分开显示时,会换两次行。
  57.                 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
  58.                 if (((char) tempchar) != '\r') {
  59.                     System.out.print((char) tempchar);
  60.                 }
  61.             }
  62.             reader.close();
  63.         } catch (Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.         try {
  67.             System.out.println("以字符为单位读取文件内容,一次读多个字节:");
  68.             // 一次读多个字符
  69.             char[] tempchars = new char[30];
  70.             int charread = 0;
  71.             reader = new InputStreamReader(new FileInputStream(fileName));
  72.             // 读入多个字符到字符数组中,charread为一次读取字符数
  73.             while ((charread = reader.read(tempchars)) != -1) {
  74.                 // 同样屏蔽掉\r不显示
  75.                 if ((charread == tempchars.length)
  76.                         && (tempchars[tempchars.length - 1] != '\r')) {
  77.                     System.out.print(tempchars);
  78.                 } else {
  79.                     for (int i = 0; i < charread; i++) {
  80.                         if (tempchars[i] == '\r') {
  81.                             continue;
  82.                         } else {
  83.                             System.out.print(tempchars[i]);
  84.                         }
  85.                     }
  86.                 }
  87.             }

  88.         } catch (Exception e1) {
  89.             e1.printStackTrace();
  90.         } finally {
  91.             if (reader != null) {
  92.                 try {
  93.                     reader.close();
  94.                 } catch (IOException e1) {
  95.                 }
  96.             }
  97.         }
  98.     }

  99.     /**
  100.      * 以行为单位读取文件,常用于读面向行的格式化文件
  101.      */
  102.     public static void readFileByLines(String fileName) {
  103.         File file = new File(fileName);
  104.         BufferedReader reader = null;
  105.         try {
  106.             System.out.println("以行为单位读取文件内容,一次读一整行:");
  107.             reader = new BufferedReader(new FileReader(file));
  108.             String tempString = null;
  109.             int line = 1;
  110.             // 一次读入一行,直到读入null为文件结束
  111.             while ((tempString = reader.readLine()) != null) {
  112.                 // 显示行号
  113.                 System.out.println("line " + line + ": " + tempString);
  114.                 line++;
  115.             }
  116.             reader.close();
  117.         } catch (IOException e) {
  118.             e.printStackTrace();
  119.         } finally {
  120.             if (reader != null) {
  121.                 try {
  122.                     reader.close();
  123.                 } catch (IOException e1) {
  124.                 }
  125.             }
  126.         }
  127.     }

  128.     /**
  129.      * 随机读取文件内容
  130.      */
  131.     public static void readFileByRandomAccess(String fileName) {
  132.         RandomAccessFile randomFile = null;
  133.         try {
  134.             System.out.println("随机读取一段文件内容:");
  135.             // 打开一个随机访问文件流,按只读方式
  136.             randomFile = new RandomAccessFile(fileName, "r");
  137.             // 文件长度,字节数
  138.             long fileLength = randomFile.length();
  139.             // 读文件的起始位置
  140.             int beginIndex = (fileLength > 4) ? 4 : 0;
  141.             // 将读文件的开始位置移到beginIndex位置。
  142.             randomFile.seek(beginIndex);
  143.             byte[] bytes = new byte[10];
  144.             int byteread = 0;
  145.             // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
  146.             // 将一次读取的字节数赋给byteread
  147.             while ((byteread = randomFile.read(bytes)) != -1) {
  148.                 System.out.write(bytes, 0, byteread);
  149.             }
  150.         } catch (IOException e) {
  151.             e.printStackTrace();
  152.         } finally {
  153.             if (randomFile != null) {
  154.                 try {
  155.                     randomFile.close();
  156.                 } catch (IOException e1) {
  157.                 }
  158.             }
  159.         }
  160.     }

  161.     /**
  162.      * 显示输入流中还剩的字节数
  163.      */
  164.     private static void showAvailableBytes(InputStream in) {
  165.         try {
  166.             System.out.println("当前字节输入流中的字节数为:" + in.available());
  167.         } catch (IOException e) {
  168.             e.printStackTrace();
  169.         }
  170.     }

  171.     public static void main(String[] args) {
  172.         String fileName = "C:/temp/newTemp.txt";
  173.         ReadFromFile.readFileByBytes(fileName);
  174.         ReadFromFile.readFileByChars(fileName);
  175.         ReadFromFile.readFileByLines(fileName);
  176.         ReadFromFile.readFileByRandomAccess(fileName);
  177.     }
  178. }
复制代码
本文出自柠檬派http://www.lemonpai.com   请务必保留此出处 ,否则将追究法律责任!


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Java jav exception Available filename public return 二进制 null 图片

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-9 08:22