public String jiexi(String url) { String result=null; FileInputStream fis =null; try { fis = new FileInputStream("E:\\nohup.txt"); /*用字节数组,减少了内存和硬盘的交互,提高了运行效率 一次最多读取bytes.length个字节*/ byte[] bytes = new byte[4]; int readCount=0; /*readCount是-1时,说明已经读完了*/ /* fis.read(bytes)的返回值是读取到的字节的数量,而且返回的是字节的ascll码*/ while( (readCount= fis.read(bytes)) !=-1){ /*读到多少个,就转换输出多少个,不会出现重复读取的问题*/ result= new String(bytes,0,readCount); /*String(byte[] bytes, int offset, int length) 构建了一种新的 String通过解码指定的字节数组使用平台的默认字符集。*/ } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fis !=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
File读文件操作
最新推荐文章于 2024-05-14 06:37:18 发布