去掉代码前部编号

今天发先从JavaEye网站上粘贴下来的代码有一些前面会带有编号,在程序中无法使用,如果手动将其编号删除实在是太浪费时间了,所以自己谢了一个简单的程序用来自动去除编号。

1.只要将有编号的代码保存到一个文件中。

2.在程序中指定文件的路径。

3.直接运行就可以将转换好的文件自动保存到原来的文件中(也可以自从新指定路径)

 

主意:两个文件不在同一个包下,用时需要更改

 

package com.cui.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileIO {
 private String filePath;
 private File file = null;
 private FileReader fr = null;
 private FileWriter fw = null;
 private BufferedReader br = null;
 private BufferedWriter bw = null;
 
 public FileIO() {};
 /**
  * 得到指定文件路径
  * @param filePath 指定文件路径
  */
 public FileIO(String filePath) {
  this.filePath = filePath;
 }
 /**
  * 读取指定文件的内容并返回
  * @return 指定文件字内容
  * @throws IOException
  */
 public String readFileToStr() {
  StringBuffer sb = new StringBuffer();
  if(file == null) file = new File(filePath);
  try {
   fr = new FileReader(file);
   br = new BufferedReader(fr);
   String str = "";
   while(str != null) {
    str = br.readLine();
    if(str != null) sb.append("\n"+str);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   close();
  }
  return sb.toString();
 }
 /**
  * 读取指定文件的内容并返回
  * @param filePath 指定文件路径
  * @return 指定文件字内容
  * @throws IOException
  */
 public String readFileToStr(String filePath) {
  this.filePath = filePath;
  return readFileToStr();
 }
 /**
  * 将指定字符串写入指定文件
  * @param str 指定字符串
  * @throws IOException
  */
 public void writeStrToFile(String str) {
  if(file == null) file = new File(filePath);
  try {
   fw = new FileWriter(file);
   bw = new BufferedWriter(fw);
   bw.write(str);
   bw.flush();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   close();
  }

 }
 /**
  * 将指定字符串写入指定文件
  * @param str 指定字符串
  * @param filePath 指定文件路径
  * @throws IOException
  */
 public void writeStrToFile(String str,String filePath){
  this.filePath = filePath;
  writeStrToFile(str);
 }
 /**
  * 将所有的i/o流关闭
  */
 public void close() {
  if(br != null) {
   try {
    br.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if(fr != null) {
   try {
    fr.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if(bw != null) {
   try {
    bw.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if(fw != null) {
   try {
    fw.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

package com.cui.tool;

import com.cui.io.FileIO;

public class DeleteNumber {
 public String delNum(String str) {
  int len = str.length(); //得到字符的长度
  char ch1 = ' '; //创建一个相同长度的字符数组
  char ch2 = ' ';
  StringBuffer sb = new StringBuffer();
  
  for(int i=0;i<len-1;i++){
   ch1 = str.charAt(i); //赋值
   ch2 = str.charAt(i+1);
   if(ch1 >=48&&ch1 <= 57 && ch2 >=48&&ch2 <= 57){
    continue;
   }if(ch1 >=48&&ch1 <= 57 &&  ch2 =='.') {
    i += 1;
    continue;
   }else{
    sb.append(ch1);
   }
  }
  return sb.toString();
 }
 public static void main(String[] args) {
  DeleteNumber dn = new DeleteNumber();
  FileIO fio = new FileIO("D://a.txt");
  String str = null;
  str = fio.readFileToStr();
  str = dn.delNum(str);
  fio.writeStrToFile(str);
  
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值