当前位置: 首页 > 编程日记 > 正文

java读取文件

  1 java8读取文本文件
  2 
  3         
  4     public static void java8ReadFileLines(String fileName) throws IOException {
  5         List lineList = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
  6 
  7         for(String line:lineList){
  8             System.out.println(line);
  9         }
 10     }    
 11     
 12         
 13         
 14 一行一行地读取文件
 15 
 16 
 17     public static void readFileByLines(String fileName) {
 18         File file = new File(fileName);
 19         BufferedReader reader = null;
 20         try {
 21             reader = new BufferedReader(new FileReader(file));
 22             String line = null;
 23             while((line=reader.readLine())!=null ) {
 24                 System.out.println(line);
 25             }
 26         }catch (IOException e) {
 27 
 28         }finally {
 29             if(reader!=null) {
 30                 try{
 31                     reader.close();
 32                 }catch (IOException e) {
 33                     ;
 34                 }
 35             }
 36         }
 37     }    
 38     
 39     
 40     
 41 一次读取多个字符
 42 
 43 
 44     public static void readFileByMultiChars(String fileName) {
 45         File file = new File(fileName);
 46         try{
 47             InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
 48 
 49             char[] tempChars = new char[30];
 50 
 51             int readCount = 0;
 52             while((readCount=inputStreamReader.read(tempChars))!=-1) {
 53                 if(readCount==tempChars.length) {
 54                     System.out.println(tempChars);
 55                 }else{
 56                     System.out.println(Arrays.copyOf(tempChars, readCount));
 57                 }
 58             }
 59 
 60 
 61             inputStreamReader.close();
 62         }catch(Exception e) {
 63             e.printStackTrace();
 64         }
 65     }    
 66 
 67 
 68         
 69 一个字符一个字符地读取
 70 
 71 
 72     public static void readFileByChars(String fileName) {
 73         File file = new File(fileName);
 74         try{
 75             InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
 76 
 77             int tempChar;
 78 
 79             while((tempChar=inputStreamReader.read())!=-1) {
 80                 System.out.println((char)tempChar);
 81             }
 82 
 83 
 84             inputStreamReader.close();
 85         }catch(Exception e) {
 86             e.printStackTrace();
 87         }
 88     }        
 89 
 90 
 91         
 92 java8读取字节 超级简单
 93 
 94 
 95     public static byte[] java8ReadBytes(String fileName) throws IOException {
 96         return Files.readAllBytes(Paths.get(fileName));
 97     }
 98 
 99 
100 
101 一个字节一个字节地读取
102 
103 
104    public static void readFileByOneByte(String fileName) {
105         File file = new File(fileName);
106         InputStream inputStream = null;
107 
108         try{
109             inputStream = new FileInputStream(file);
110             int tempByte;
111             while( (tempByte=inputStream.read())!=-1) {
112                 System.out.println(tempByte);
113             }
114 
115             inputStream.close();
116         }catch (IOException e) {
117             System.out.println(e);
118         }
119 
120     }
121     
122     
123  
124 一个字节一个字节读取到ByteBuffer
125 
126 
127     public static byte[] readFileByOneByteToBuffer(String fileName) {
128 
129 
130         ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
131 
132         File file = new File(fileName);
133         InputStream inputStream = null;
134 
135         try{
136             inputStream = new FileInputStream(file);
137             int tempByte;
138             while( (tempByte=inputStream.read())!=-1) {
139                 byteBuffer.put((byte)tempByte);
140             }
141 
142             inputStream.close();
143         }catch (IOException e) {
144             System.out.println(e);
145         }
146 
147         byteBuffer.flip();
148         System.out.println("one limit:" + byteBuffer.limit());
149         byte[] result = new byte[byteBuffer.limit()];
150         byteBuffer.get(result);
151 
152         return result;
153 
154 
155     }
156     
157     
158     
159 多个字节进行读取
160 
161 
162     public static void readFileByMultiBytes(String fileName) {
163 
164         File file = new File(fileName);
165         InputStream inputStream = null;
166 
167         try {
168             byte[] bytes = new byte[50];
169             int byteRead = 0;
170             inputStream = new FileInputStream(fileName);
171 
172             while( (byteRead = inputStream.read(bytes))!=-1 ) {
173                 System.out.println(byteRead);
174             }
175         }catch(IOException e) {
176             System.out.println(e);
177         }finally {
178             if(inputStream!=null) {
179                 try{
180                     inputStream.close();
181                 }catch(IOException e){
182                     System.out.println("iput stream close exception" +  e);
183                 }
184             }
185         }
186     }
187     
188     
189   
190 读取多个字节到ByteBuffer
191 
192 
193     public static byte[] readFileByMultiBytesToBuffer(String fileName) {
194 
195         ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024);
196         InputStream inputStream = null;
197 
198         try {
199             byte[] bytes = new byte[50];
200             int byteRead = 0;
201             inputStream = new FileInputStream(fileName);
202 
203             int count = 0;
204             while( (byteRead = inputStream.read(bytes))!=-1 ) {
205                 byteBuffer.put(bytes, 0, byteRead);
206                 count+=byteRead;
207             }
208 
209             System.out.println("readCount:"+count);
210         }catch(IOException e) {
211             System.out.println(e);
212         }finally {
213             if(inputStream!=null) {
214                 try{
215                     inputStream.close();
216                 }catch(IOException e){
217                     System.out.println("iput stream close exception" +  e);
218                 }
219             }
220         }
221 
222         byteBuffer.flip();
223         System.out.println("multi limit:" + byteBuffer.limit());
224         byte[] result = new byte[byteBuffer.limit()];
225         byteBuffer.get(result);
226 
227         return result;
228     }            
229

相关文章:

常见 Datagrid 错误

Marcie Robillard DatagridGirl.comDatagrid 控件是 Microsoft ASP.NET 中功能最强、用途最广的 Web 控件之一,这一点已经得到了 ASP.NET 权威人士的认同。虽然 Datagrid 控件易于使用,但同样易于给使用者带来麻烦。以下是许多人所犯的一些错误&#xff…

干货!3 个重要因素,带你看透 AI 技术架构方案的可行性!

作者 | 房磊责编 | Carol出品 | AI 科技大本营(ID:rgznai100)人工智能这几年发展的如火如荼,不仅在计算机视觉和自然语言处理领域发生了翻天覆地的变革,在其他领域也掀起了技术革新的浪潮。无论是在新业务上的尝试&…

mysql从另一张获取数据的方法

方法一 CREATE TABLE tmp AS SELECT a.id FROM t_user t JOIN temp a ON t.email a.email; 方法二 INSERT INTO t_user (id,username,PASSWORD,email,user_type,STATUS) SELECT id,REPLACE(email,,_),PASSWORD,email,0,0 FROM temp; 这两个sql都是从另外一张中获取的数据插入…

手动创建Spring项目 Spring framework

之前学习框架一直是看的视频教程,并且在都配套有项目源码,跟着视频敲代码总是很简单,现在想深入了解,自己从官网下载文件手动搭建,就遇到了很多问题记载如下。 首先熟悉一下spring的官方网站:http://spring…

使用 ASP+ DataGrid 控件来创建主视图/详细资料视图

Nikhil Kothari Microsoft Corporation 2000年8月简介 Microsoft Visual Studio.NET 的下一发行版包括 DataGrid Web 控件 (作为服务器控件的 Active Server Page (ASP) 套件的一部分)。 该控件提供用以根据数据源的内容来表示 HTML 的功能。 DataGrid 控件可以用于若干个只…

如何将广告始终定位到网页右下角

body{margin:0;border:0;height:100%;overflow-y:auto;}#test{display:block; bottom:3px; right:3px; width:130px; position:fixed;}/* 以下是写给IE6的 */* html #test{position:absolute;right:18px} * html{overflow-x:auto; overflow-y:hidden;}转载于:https://www.cnblo…

如何用 Python 将 Excel 表格转成可视化图形?| 原力计划

作者 | Waao666责编 | 王晓曼出品 | CSDN 博客前言大家知道,考研很大一部分也是考信息收集能力。每年往往有很多人就是在这上面栽跟头了,不能正确分析各大院校往年的录取信息,进而没能选择合适的报考院校。至于很多院校的录取信息是以 PDF 形…

Mac OS X 下mysql配置备忘

从windows过渡到os x确实需要适应,对于开发人员来讲更是这样。从官网下载目前最新版本的mysql 5.7.13,下载地址:http://dev.mysql.com/downloads/mysql/刚开始非常顺利的安装完mysql,这时候我还没有意识到密码的问题,直…

为 ASP.NET Datagrid 创建自定义列

Marcie Robillard DatagridGirl.com 2003 年 9 月 简介 不得不承认,为 Microsoft ASP.NET 编写 Datagrid 代码包括大量的重复工作。尽管我深受该控件的困扰,但我还是不断寻找简化这类任务的捷径。谁都不愿意做重复的工作,对不对&#xff1…

不怕面试被问了!二叉树算法大盘点

作者 | BoCong-Deng题图 | 视觉中国出品 | CSDN博客树结构对于程序员来说应该不陌生,特别是二叉树,基本只要接触算法这一类的都一定会碰到的,所以我打算通过一篇文章,对二叉树结构的相关算法进行总结汇总,思路和代码实…

Field types

2019独角兽企业重金招聘Python工程师标准>>> Field types The generated Form class will have a form field for every model field. Each model field has a corresponding default form field. For example, a CharField on a model is represented as a CharFie…

【51CTO学院三周年】我的职业生涯有贵人相助--小强老师

个人认为功能测试做到一定年限之后,自然会遇到职业生涯中最大瓶颈——转型。对此,我的经历是这样的。话说那还是两年前,在搜索某问题的时候发现了51CTO,从中看到了很多大牛的博文和视频课程,顿时感觉自己找到宝了&…

在.NET中实现彩色光标,动画光标和自定义光标

作者:孟宪会 微软MVPTest.cs using System;using System.Drawing;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Reflection; namespace ColorCursor{ public class Form1 : System.Windows.Forms.Form { [DllImport("us…

magento模板区块--首页content区块

首页替换 自定义首页content内容 在cms-->>page 新建首页 在content 里加入 -------------------------- <div class"col-left side-col"> <p class"home-callout"><a href"{{store direct_url"apparel/shoes/womens/anash…

遮挡也能识别?地平线提出时序信息提升行人检测准确度|​CVPR 2020

来源 | 驭势科技行人检测作为计算机视觉领域最基本的主题之一&#xff0c;多年来被广泛研究。尽管最先进的行人检测器已在无遮挡行人上取得了超过 90% 的准确率&#xff0c;但在严重遮挡行人检测上依然无法达到满意的效果。究其根源&#xff0c;主要存在以下两个难点&#xff1…

通过响应式web设计,使本站支持手机浏览

2019独角兽企业重金招聘Python工程师标准>>> 2014-01-28 14:49:14 现在越来越多的人通过手机来上网&#xff0c;手机由于屏幕尺寸的原因&#xff0c;当浏览为PC端浏览器设计的网页的时候&#xff0c;往往会出现各种各样的问题。 糊涂僧的这个小博客也一样&#xff0…

在ASP.NET中跨页面实现多选

作者&#xff1a;孟宪会 微软MVP SelectMultiPages.aspx <% Page EnableViewState"true" CodeBehind"SelectMultiPages.aspx.cs" Language"c#" AutoEventWireup"false" Inherits"eMeng.Exam.SelectMultiPages" %><…

c#有多少种可能导致写文件失败?

1.路径中有非法字符 Path.GetInvalidPathChars() 2.文件名中有非法字符 Path.GetInvalidFileNameChars() 3.文件创建时&#xff0c;文件夹只读。 4.文件创建时&#xff0c;文件夹权限不足&#xff0c;如需要管理员权限。 5.文件创建时&#xff0c;文件夹不存在。 6.系统目录&am…

抖音、快手和直播行业的火爆究竟给了谁机会?

经常收到一些CSDN小伙伴的留言&#xff0c;反馈如下这样的困惑“短视频这么火爆&#xff0c;我该学些什么技术才能入行&#xff1f;”“我想从事音视频开发&#xff0c;该如何入门和进阶&#xff1f;真的像坊间传闻的那么难吗&#xff1f;”音视频的开发前景做一个不恰当的比喻…

android上line-height的问题

关于line-height大家应该非常熟悉了吧&#xff0c;就是用来做垂直居中的&#xff0c;屡试不爽&#xff0c;基本上没有什么问题&#xff0c;但是最近一个项目&#xff0c;测试提了一个bug&#xff0c;看图吧。 从别处窃的图&#xff0c;这个问题只有安卓上才能复现&#xff0c;做…

深入讲解 ASP+ 验证

Anthony Moore Microsoft Corporation 2000年10月简介 这篇文章详细讲解了 ASP 验证控件的工作方式。如果要生成其中包含验证控件的复杂页面&#xff0c;或是要扩展验证框架&#xff0c;建议您阅读本文。如果要学习使用验证控件&#xff0c;或是要决定是否使用验证控件&…

EditText和TextView出现中文、英文等string串的排版问题

默认EditText和TextView自动换行。如果在string中出现了中文字符&#xff0c;排版出现意外&#xff0c;如图所示&#xff1a; 这是因为软盘默认的是半角输入&#xff0c;而字母与数字的占位与汉字不同&#xff0c;所以在默认的情况下会出现如上的排版情况。 但是如果将默认的半…

阿里云蒋江伟:我们致力于为世界提供70%的算力 | 凌云时刻

导读&#xff1a;6月9日&#xff0c;2020阿里云峰会在云端召开&#xff0c;阿里巴巴合伙人、阿里云智能基础产品事业部高级研究员蒋江伟出席峰会并做了题为《新基建&#xff0c;新算力&#xff1a;阿里云基础设施算力全新升级》的重磅发布。&#xff08;以下内容为演讲实录&…

zabbix 微信报警( python 2.x )

python 2.x 微信报警脚本#!/usr/bin/python #_*_coding:utf-8 _*_ __author__ lvnianimport urllib,urllib2 import json import sysdef gettoken(corpid,corpsecret):gettoken_url https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid corpid &corpsecret corpsec…

利用 AssemblyAI 在 PyTorch 中建立端到端的语音识别模型

作者 | Comet译者 | 天道酬勤&#xff0c;责编 | Carol出品 | AI 科技大本营&#xff08;ID&#xff1a;rgznai100&#xff09;这篇文章是由AssemblyAI的机器学习研究工程师Michael Nguyen撰写的。AssemblyAI使用Comet记录、可视化和了解模型开发流程。深度学习通过引入端到端的…

PHP中的页面跳转

PHP页面跳转一、header()函数 点击按钮<input type"submit" name "submit" value"确定" /> 使用POST方式<form action"X.php" method"post"> X.php页面只做判断逻辑 处理完以后 <?php //isset函数 if…

Closure Compiler 使用

为什么80%的码农都做不了架构师&#xff1f;>>> 该项目首页&#xff1a;http://code.google.com/intl/zh-CN/closure/compiler/ 下载地址&#xff1a;http://closure-compiler.googlecode.com/files/compiler-latest.zip 下载后解压&#xff0c;即可看到compiler.j…

如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能

作者&#xff1a;未知 请作者速与本人联系步入 IT 业已经有几年的时间了 , 从最早接触 pb6.0 到现在 .Net 技术 , 计算机技术不论是从硬件还是软件都有巨大的进步 . 而中国程序员总体水平在世界上也是远远落后&#xff0c;其中缺乏完善的体系、必要的交流和程序员个人英雄主义…

Revit二次开发之“选择某一楼层的墙”

其实就是过滤器的用法。这里想要找到同一楼层中的风管&#xff0c;不可行。 要用&#xff1a;duct.ReferenceLevel//选择某一楼层上的墙[Transaction(TransactionMode.Manual)][Regeneration(RegenerationOption.Manual)]//[Journaling(JournalingMode.NoCommandData)]publiccl…

百变应用场景下,优酷基于图执行引擎的算法服务框架筑造之路!

作者| 阿里文娱高级专家 随方&#xff0c;阿里文娱开发专家 轩成责编 | 屠敏头图 | CSDN 下载自视觉中国背景在阿里的业务中&#xff0c;有广泛的算法应用场景&#xff0c;也沉淀了相关的算法应用平台和工具&#xff1a;基础的算法引擎部分&#xff0c;有成熟的召回和打分预估引…