读取文件,并修改里面的内容

5、读取文件流进行全局替换,适合小文件
	public static void main(String[] args){
		String filePath = "D:\\sql\\UT_PERSON_INFO.sql";
		String outPath = "D:\\sql\\UT_PERSON_INFO.sql";
		try {
			autoReplace(filePath,outPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


		public static  void autoReplace(String filePath, String outPath) throws IOException {
			File file = new File(filePath);
			Long fileLength = file.length();
			byte[] fileContext = new byte[fileLength.intValue()];
			FileInputStream in = new FileInputStream(filePath);
			in.read(fileContext);
			in.close();
			String str = new String(fileContext);
			str = str.replace("\"PUBLIC\".", " ");
			str = str.replace("\"", "'");

			PrintWriter out = new PrintWriter(outPath);
			out.write(str.toCharArray());
			out.flush();
			out.close();
		}

读取文件,每行读取,然后替换指定的内容:

public static  void ChangeFile (String filePath, String outPath) throws IOException {
			try {
				BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));//数据流读取文件

				StringBuffer strBuffer = new StringBuffer();
				String empty = "";
				String tihuan = "";
				for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
//					if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在
//						tihuan = temp.substring(0, 10);
//						temp = temp.replace(tihuan, empty);//替换为你想要的东东
//					}
					if(temp.contains("\"PUBLIC\".")) {
						temp = temp.replace("\"PUBLIC\"."," ");
					}
					if(temp.contains("\"")) {
						temp = temp.replace("\"","'");
					}
					strBuffer.append(temp);
					strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
				}
				bufReader.close();
				PrintWriter printWriter = new PrintWriter(outPath);//替换后输出的文件位置
				printWriter.write(strBuffer.toString().toCharArray());
				printWriter.flush();
				printWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值