java 解析一个文件内容并写入到另一个文件里。上传到指定目录里

#厂家信息文件地址     source源文件地址   target生成新的文件地址
yml
manufactor:
  source: F://configs//x_cloud_definitions.txt
  target: F://target//
@Value("${manufactor.source}")
private  String source;

@Value("${manufactor.target}")
private  String target;
 public boolean uploadFile(String id) {
        ManuFactor manuFactor = baseMapper.findById(id);
        String s ="";
        File file = new File(source);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                //根据实际情况替换文件的内容
                if(tempStr.startsWith("OSK_CREDENTIALS")){
                    tempStr="OSK_CREDENTIALS = "+manuFactor.getAccount()+"-"+manuFactor.getPassword()+"-"+manuFactor.getAccount();
                }
                // \n 是文件换行
                sbf.append(tempStr+"\n");
            }
            reader.close();
            s  = sbf.toString();
            //待写入文件及新的文件名称
            File newfile=new File(target+manuFactor.getAccount()+"_cloud_definitions.txt");
            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newfile),"UTF-8"));//顺便练习下匿名类的使用
            bufferedWriter.write(s);
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("厂家文件上传失败!!!", e);
            return false;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
//        s = sbf.toString();
        return true;
    }

你可能感兴趣的:(笔记,java,p2p,开发语言)