java读取文件

简介:   1 java8读取文本文件  2   3           4     public static void java8ReadFileLines(String fileName) throws IOException {  5         List lineList = Files.
  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         
若转载请注明出处!若有疑问,请回复交流!
目录
相关文章
|
24天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
25 1
|
28天前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
29天前
|
Java API
Java中文件与输入输出
Java中文件与输入输出
|
30天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
1月前
|
Java
java程序导出堆文件
java程序导出堆文件
|
1月前
|
SQL Oracle Java
sql文件批处理程序-java桌面应用
sql文件批处理程序-java桌面应用
25 0
|
1月前
|
存储 Java 文件存储
如何用 Java 压缩 ZIP 文件?
【2月更文挑战第21天】
29 1
|
1月前
|
Java
Java实现文件和目录的管理
Java实现文件和目录的管理
24 0
|
26天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
90 3
|
29天前
|
Java
使用java将字符串写入到指定的文件中
使用java将字符串写入到指定的文件中
11 0