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
原创粉丝点击