【Java-IO】Java常用IO流操作实现本地文件的读写

下面示例包括使用 BufferedReader 读,FileWriterFileOutputStream 写。

package gaei.cn.x5l.x5lhive2cos.utils;

import java.io.*;

//历史数据自动生成sql修复分区
public class HdfsTboxAddPartitionSql {
    public static void main(String[] args) {
        String dbName = "database";
        String tableName = "table_name";
        String filePathInput = "C:/Users/MECHREVO/Desktop/新建文件夹/history_partitions/分区详情/" + tableName + ".txt";
        String filePathOutput = "C:/Users/MECHREVO/Desktop/新建文件夹/history_partitions/分区修改至s3-sql/" + tableName + ".txt";
        File fileIn = new File(filePathInput);
        File fileOut = new File(filePathOutput);
        BufferedReader reader = null;
        FileWriter fw = null;
//        FileOutputStream fos = null;
        String partitionDir = null;
        int line = 1;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileIn), "GBK"));
            fw = new FileWriter(fileOut);
//            fos = new FileOutputStream(fileOut);
            while ((partitionDir = reader.readLine()) != null) {
                StringBuilder alterSql = new StringBuilder();
                alterSql.append("alter table ");
                String sampleDateDir = partitionDir.split("/")[0];//sample_date=20230510
                String partitionNameDir = partitionDir.split("/")[1];//partition_name=101
                String sample_date = sampleDateDir.split("=")[0];//sample_date
                String sample_date_yyyyMMdd = sampleDateDir.split("=")[1];//20230510
                String partition_name = partitionNameDir.split("=")[0];//partition_name
                String partition_name_num = partitionNameDir.split("=")[1];//100
                alterSql.append(dbName)
                        .append(".")
                        .append(tableName)
                        .append(" add if not exists partition(")
                        .append(sample_date)
                        .append("='")
                        .append(sample_date_yyyyMMdd)
                        .append("', ")
                        .append(partition_name)
                        .append("='")
                        .append(partition_name_num)
                        .append("') ")
                        .append("location '")
//                        .append("hdfs://prdns/warehouse/tablespace/external/hive/ods_x5l.db/")
                        .append("s3a://buceketname/prd/data/")
                        .append(dbName)
                        .append("/")
                        .append(tableName)
                        .append("/")
                        .append(partitionDir)
                        .append("';");
                fw.write(alterSql + "\n");
//                fos.write((alterSql + "\n").getBytes());
                System.out.println(alterSql);
                line++;
            }
            fw.flush();
            fw.close();
//            fos.flush();
//            fos.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    fw.close();
//                    fos.flush();
//                    fos.close();
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值