Java读取文件内容

以行为单位读取文件,常用于读面向行的格式化文件

import java.io.*;

public class Main {
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}

以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件

    /**
     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字节为单位读取文件内容,一次读一个字节:");
            // 一次读一个字节
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read())!=-1) {
                System.out.println(tempbyte);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        try {
            System.out.println("以字节为单位读取文件内容,一次读多个字节:");
            // 一次读多个字节
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 读入多个字节到字节数组中,byteread为一次读入的字节数
            while ((byteread = in.read(tempbytes)) != -1) {
                System.out.println(tempbytes, 0, byteread);//好方法,第一个参数是数组,第二个参数是开始位置,第三个参数是长度
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }
    /**
     * 显示输入流中还剩的字节数
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
 
/*
 * 读取文件:
 * 1、找到指定的文件
 * 2、根据文件创建文件的输入流
 * 3、创建字节数组
 * 4、读取内容,放到字节数组里面
 * 5、关闭输入流
 */
public class FileRead {
 
    public static void main(String[] args) {
        // 构建指定文件
        File file = new File("E:" + File.separator + "hello.txt");
        InputStream in = null;
        try {
            // 根据文件创建文件的输入流
            in = new FileInputStream(file);
            // 创建字节数组
            byte[] data = new byte[1024];
            // 读取内容,放到字节数组里面
            in.read(data);
            System.out.println(new String(data));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭输入流
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
}

java FileWritter写入文件

import java.io.File;
 
import java.io.FileWriter;
 
import java.io.BufferedWriter;
 
import java.io.IOException;
 
public class AppendToFileTest
 
{
 
public static void main( String[] args )
 
{
 
    try{
 
        String content = "A cat will append to the end of the file";
 
        File file =new File("test_appendfile.txt");
 
        if(!file.exists()){
            file.createNewFile();
        }
 
        //使用true,即进行append file 
 
        FileWriter fileWritter = new FileWriter(file.getName(),true);
 
 
        fileWritter.write(content);
 
        fileWritter.close();
 
        System.out.println("finish");
 
    }catch(IOException e){
 
        e.printStackTrace();
 
    }
 
}
 
}

java BufferedWriter写入文件


import java.io.BufferedWriter;
 
import java.io.File;
 
import java.io.FileWriter;
 
import java.io.IOException;
 
public class WriteToFileTest2 {
 
    public static void main(String[] args) {
        
        try {
                String content = "a dog will be write in file";
                File file = new File("test_appendfile2.txt");
                if(!file.exists()){
                    file.createNewFile();
                }
                FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fileWriter);
                bw.write(content);
                bw.close();
                System.out.println("finish");
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
 
}
Last modification:September 1st, 2020 at 03:29 pm
如果觉得我的文章对你有用,请随意赞赏