【经典】JAVA文件读取常用工具类

1、读取文件成字节数组
	//读取文件成字节数组
	public static byte[] file2byte(String path){
        try {
            FileInputStream in =new FileInputStream(new File(path));
            byte[] data=new byte[in.available()];
            in.read(data);
            in.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
2、将字节数组写入文件
	//将字节数组写入文件
	public static void byte2file(String path,byte[] data) {
        try {
            FileOutputStream outputStream  =new FileOutputStream(new File(path));
            outputStream.write(data);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
3、按行读取文件成list
	//按行读取文件成list
	public static ArrayList<String> file2list(String path,String encoder) {
        ArrayList<String> alline=new ArrayList<String>();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                alline.add(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alline;
    }
4、输出list到文件
	//输出list到文件
	public static void list2file(String path,ArrayList<String> data,String encoder) {
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            for (String str:data) {
                out.write(str);
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
5、从标准输入中读入
	//从标准输入中读入
	public static String system2str() throws IOException{
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        return stdin.readLine();
    }
6、读取文件成字符串
	//读取文件成字符串
	public static String file2str(String path,String encoder){
        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                sb.append(str);
            }
            in.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
7、输出字符串到文件
	//输出字符串到文件
	public static void str2file(String path,String data,String encoder){
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
8、读取文件成数据矩阵
	//读取文件成数据矩阵
	public static ArrayList<Double> file2matrix(String path){
        ArrayList<Double> alldata=new ArrayList<Double>();
        try {
            DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
            //利用DataInputStream来读数据
            while(true)
            {
                alldata.add(in.readDouble());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alldata;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值