文件读取的方法
2015-09-12 23:45
281 查看
<span style="font-size:24px;">1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容</span>
<span style="font-size:18px;">public class ReadFromFile { /** * <span style="color:#ff0000;">以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 </span> */ public static void readFileByBytes(String fileName) { File file_read = new File(fileName); File file_write = new File(fileName); InputStream in = null; OutputStream out=null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); <span style="color:#ff0000;">// 一次读一个字节,如果文件略大~~~速度很慢 </span> in = new FileInputStream(file_read); out=new FileOutputStream(file_write); //读取数据 int tempbyte; while ((tempbyte = in.read()) != -1) { out.write(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字节为单位读取文件内容,一次读多个字节:"); // 一次读多个字节 byte[] tempbytes = new byte[100]; int len = 0; in = new FileInputStream(fileName1); out=new FileOutputStream(fileName2); ReadFromFile.showAvailableBytes(in); // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((len = in.read(tempbytes)) != -1) { out.write(tempbytes, 0, len); //这句话,len是当前数组里数据的长度,这样写出,可以保证数据的准确性 //如果直接out.write(tempbytes)可能导致写出了多余的数据出去,例如写完后图片不能正确显示,音频大小比原大小大很多 } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 if (((char) tempchar) != '\r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符为单位读取文件内容,一次读多个字节:"); // 一次读多个字符 char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); // 读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars)) != -1) { // 同样屏蔽掉\r不显示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) { System.out.print(tempchars); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == '\r') { continue; } else { System.out.print(tempchars[i]); } } } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 以行为单位读取文件,常用于读面向行的格式化文件 */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 随机读取文件内容 */ public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try { System.out.println("随机读取一段文件内容:"); // 打开一个随机访问文件流,按只读方式 randomFile = new RandomAccessFile(fileName, "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) { } } } } /** * 显示输入流中还剩的字节数 */ private static void showAvailableBytes(InputStream in) { try { System.out.println("当前字节输入流中的字节数为:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; ReadFromFile.readFileByBytes(fileName); ReadFromFile.readFileByChars(fileName); ReadFromFile.readFileByLines(fileName); ReadFromFile.readFileByRandomAccess(fileName); } } </span>
相关文章推荐
- 程序员如何做到『编程速度又快,Bug 数量又少』?
- SQLAlchemy中设置Column的默认值
- LeetCode122——Best Time to Buy and Sell Stock II
- 海量数据处理面试题
- UDP协议实例分析
- windows下eclipse远程连接hadoop错误“Exception in thread"main"java.io.IOException: Call to Master.Hadoop/172.20.145.22:9000 failed ”
- lintcode-搜索旋转排序数组-62
- 【Node.js基础篇】(九)使用readline模块实现Node.js的输入输出
- django复习笔记3:urls/views/templates三板斧
- Android之AsyncTask异步任务详解总结
- 异常值(outlier)的判别与剔除
- 《剑指offer》左旋转字符串
- 一点概念
- ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets(转)
- 如何在windows中使用cmd命令去编译,运行C++程序
- 31. 匿名与闭包
- 2015第37周六
- Linux下mail命令【邮件的收发】
- 哈密顿回路的非暴力解法(转自CSDN大神GDTZX)
- 用DELPHI工具编制开发生成二维条码