Java 单个文件操作(去掉空行,去掉注释,替换引号等)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


/**
 * 文件处理
 * 待完善
 */
public class Test {
	
	//被处理的文件绝对路径
	static String fromFile = "C:\\1.txt";
	//读取被处理文件的读取编码
	static String fromFileCharset = "UTF-8";
	//处理后文件的编码格式
	static String toFileCharset = "UTF-8";
	//被处理文件的路径(不包括文件名称)
	static String fromFilePathWithOutFile = fromFile.substring(0,fromFile.lastIndexOf("\\")+1);
	//被处理文件的文件名称(不包括路径)
	static String fromFileWithOutFilePath = fromFile.substring(fromFile.lastIndexOf("\\")+1,fromFile.length());
	
	
	public static void main(String args[]){
		Test test = new Test();
		test.excute(fromFile);
		System.out.println(fromFileWithOutFilePath);
	}

	public void excute(String filePath){
		File file = new File(filePath);
		if(file.exists()){
			System.out.println("---文件存在正在处理---");
			String tempString = null; 
			String writeString = null;
			InputStreamReader isReader = null;
			FileInputStream fiStream = null;
			BufferedReader bReader = null;
			try {
				
				fiStream = new FileInputStream(filePath);
				isReader = new InputStreamReader(fiStream,fromFileCharset);
				bReader = new BufferedReader(isReader);
				
				String toFilePathWithFileName = fromFilePathWithOutFile
					+ "New_"
					+ fromFileWithOutFilePath;
					
				File wFile = new File(toFilePathWithFileName);
				wFile.createNewFile();
				OutputStreamWriter os = null;
				FileOutputStream fos = null;
				fos = new FileOutputStream(toFilePathWithFileName);
				os = new OutputStreamWriter(fos,toFileCharset);
				while((tempString = bReader.readLine()) != null){
					writeString = tempString;
					//处理方式
					writeString = this.delKongHang(writeString);
					writeString = this.delHangHouZhuShi(writeString);
					writeString = this.replaceShuangyinhaoToDanyinhao(writeString);
					writeString = this.replaceDanyinhaoToShuangyinhao(writeString);
					
					if(!writeString.equals("")){
						os.write(writeString);
						os.write("\n");
					}
					
				}
				if (os != null) {
					try {
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					os = null;
				}
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					fos = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				
			}
			
		}else{
			System.out.println("---文件不存在!");
		}
	}
	
	/**
	 * 去掉空行
	 */
	public String delKongHang(String str){
		if(str.trim()!= null && !str.trim().equals("")){
			return str;
		}else{
			return "";
		}
	}
	
	/**
	 * 去掉行后注释
	 * //和/*和<!--
	 */
	public String delHangHouZhuShi(String str){
		if(str.contains("//")){
			return str.substring(0,str.indexOf("//"));
		}else if(str.contains("/*"))
		{
			return str.substring(0,str.indexOf("/*"));
		}else if(str.contains("<!--"))
		{
			return str.substring(0,str.indexOf("<!--"));
		}
		else{
			return str;
		}
	}
	
	/**
	 * 双引号换单引号
	 */
	public String replaceShuangyinhaoToDanyinhao(String str){
		return str.replaceAll("\"", "\'");
	}
	
	/**
	 * 单引号换双引号
	 */
	public String replaceDanyinhaoToShuangyinhao(String str){
		return str.replaceAll("\'", "\"");
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值