java 读取文件

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值