java 读取文件
来源:互联网 发布:怎样手机做淘宝客赚钱 编辑:程序博客网 时间:2024/06/08 15:34
java8读取文本文件 public static void java8ReadFileLines(String fileName) throws IOException { List lineList = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8); for(String line:lineList){ System.out.println(line); } } 一行一行地读取文件 public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; while((line=reader.readLine())!=null ) { System.out.println(line); } }catch (IOException e) { }finally { if(reader!=null) { try{ reader.close(); }catch (IOException e) { ; } } } } 一次读取多个字符 public static void readFileByMultiChars(String fileName) { File file = new File(fileName); try{ InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file)); char[] tempChars = new char[30]; int readCount = 0; while((readCount=inputStreamReader.read(tempChars))!=-1) { if(readCount==tempChars.length) { System.out.println(tempChars); }else{ System.out.println(Arrays.copyOf(tempChars, readCount)); } } inputStreamReader.close(); }catch(Exception e) { e.printStackTrace(); } }一个字符一个字符地读取 public static void readFileByChars(String fileName) { File file = new File(fileName); try{ InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file)); int tempChar; while((tempChar=inputStreamReader.read())!=-1) { System.out.println((char)tempChar); } inputStreamReader.close(); }catch(Exception e) { e.printStackTrace(); } }java8读取字节 超级简单 public static byte[] java8ReadBytes(String fileName) throws IOException { return Files.readAllBytes(Paths.get(fileName)); }一个字节一个字节地读取 public static void readFileByOneByte(String fileName) { File file = new File(fileName); InputStream inputStream = null; try{ inputStream = new FileInputStream(file); int tempByte; while( (tempByte=inputStream.read())!=-1) { System.out.println(tempByte); } inputStream.close(); }catch (IOException e) { System.out.println(e); } } 一个字节一个字节读取到ByteBuffer public static byte[] readFileByOneByteToBuffer(String fileName) { ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024); File file = new File(fileName); InputStream inputStream = null; try{ inputStream = new FileInputStream(file); int tempByte; while( (tempByte=inputStream.read())!=-1) { byteBuffer.put((byte)tempByte); } inputStream.close(); }catch (IOException e) { System.out.println(e); } byteBuffer.flip(); System.out.println("one limit:" + byteBuffer.limit()); byte[] result = new byte[byteBuffer.limit()]; byteBuffer.get(result); return result; } 多个字节进行读取 public static void readFileByMultiBytes(String fileName) { File file = new File(fileName); InputStream inputStream = null; try { byte[] bytes = new byte[50]; int byteRead = 0; inputStream = new FileInputStream(fileName); while( (byteRead = inputStream.read(bytes))!=-1 ) { System.out.println(byteRead); } }catch(IOException e) { System.out.println(e); }finally { if(inputStream!=null) { try{ inputStream.close(); }catch(IOException e){ System.out.println("iput stream close exception" + e); } } } } 读取多个字节到ByteBuffer public static byte[] readFileByMultiBytesToBuffer(String fileName) { ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024); InputStream inputStream = null; try { byte[] bytes = new byte[50]; int byteRead = 0; inputStream = new FileInputStream(fileName); int count = 0; while( (byteRead = inputStream.read(bytes))!=-1 ) { byteBuffer.put(bytes, 0, byteRead); count+=byteRead; } System.out.println("readCount:"+count); }catch(IOException e) { System.out.println(e); }finally { if(inputStream!=null) { try{ inputStream.close(); }catch(IOException e){ System.out.println("iput stream close exception" + e); } } } byteBuffer.flip(); System.out.println("multi limit:" + byteBuffer.limit()); byte[] result = new byte[byteBuffer.limit()]; byteBuffer.get(result); return result; }http://blog.csdn.net/jiangxinyu/article/details/7885518
0 0
- java读取ini文件
- JAVA读取XML文件
- java读取中文文件
- Java读取文件
- JAVA读取XML文件
- java读取中文文件
- java读取pdf文件
- JAVA读取word文件
- JAVA读取PDF文件
- java读取word文件
- java读取excel文件
- java读取excel 文件
- java 读取文件(中文)
- java读取文件源代码
- java 读取文件
- Java读取Properties文件
- java读取.properties文件
- Java读取文件
- 对最小二乘法的概率解释.
- C++重载函数的实现小结(实战篇)
- AQS源码分析
- C++基础:异常机制的处理原理
- Git用户手册--其它环境中的 Git
- java 读取文件
- CodeForces - 560A Currency System in Geraldion
- 深入解析MySQL分区(Partition)功能
- mysql cluster
- 不可变数组NSArray和可变数组NSMutableArray
- setSceneRect
- android studio tabhost基础 多功能时钟app 实现时钟功能
- 结构体和方法
- 链接三维空间中n个点的最短距离 最小生成树(krukal算法)