首页 > 代码库 > JAVA多种方式读取文件

JAVA多种方式读取文件

package com.file;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
 
public class TextReadFile {
    /*
     * 一字节为单位读取
     */
    public static void readFileByBytes(String fileName){
        File file = new File(fileName);     //创建文件
        InputStream in = null;
         
        System.out.println("!.以字节为单位读取文件内容:一次读取一个");
        try {
            in = new FileInputStream(file);
            int tempbyte;
            while((tempbyte = in.read())!=-1){
                System.out.write(tempbyte);
            }
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }
         
        System.out.println("2.以字节为单位读取文件内容:一次读取多个字节:");
        try {
            byte[] temptypes = new byte[1024];
            int byteread = 0;
             
            in = new FileInputStream(fileName);
            TextReadFile.showAvailableBytes(in);
            while((byteread = in.read(temptypes))!=-1){
                System.out.write(temptypes, 0, byteread);
            }
             
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    /*
     * 显示输入六中还剩的字节数
     */
    public static void showAvailableBytes(InputStream in){
        try {
            System.out.println("当前字节输入流中的字节数为:"+in.available());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*
     * 义字符为单位读取
     */
    public static void readFileByChar(String fileName){
        File file = new File(fileName);
        Reader read = null;
         
        System.out.println("3、以字符为单位读取文件内容,一次读一个字节:");
        try {
            read = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while((tempchar= read.read())!=-1){
                if((char)tempchar!=‘r‘){
                    System.out.print((char)tempchar);
                }
            }
            read.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        System.out.println("4.一字符为单位读取文件内容,一次读取多个:");
        char[] tempchars = new char[1024];
        int charread = 0;
        try {
            read = new InputStreamReader(new FileInputStream(file));
            while((charread=read.read(tempchars))!=-1){
                if((charread == tempchars.length)&&(tempchars[tempchars.length-1]!=‘r‘)){
                    System.out.print(tempchars);
                }else{
                    for(int i = 0;i<charread;i++){
                        if(tempchars[i]==‘r‘){
                            continue;
                        }else{
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(read != null){
                try {
                    read.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
         
    }
    /*
     * 读一行
     */
    public static void readFileByLine(String fileName){
        File file = new File(fileName);
        BufferedReader reader = null;
         
        System.out.println("6.以行为为单位读取文件内容,一次读一行:");
         
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while((tempString = reader.readLine())!=null){
                System.out.println("line"+line+":"+tempString);
                line++;
            }
            reader.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(reader!= null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        String fileName = "D:/Desktop/JarCode.txt";
        System.out.println("1.按字节为单位读取文件:");
        readFileByBytes(fileName);
        System.out.println("1.按字符为单位读取文件:");
        readFileByChar(fileName);
        System.out.println("1.按行为单位读取文件:");
        readFileByLine(fileName);
    }
}

JAVA多种方式读取文件