---
title: "去掉代码前部编号"
author: "yongjiucui"
date: "2010-12-06T19:24:56Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-12-06T19:24:56Z"
id: 834859
url: "https://cuiyj3.iteye.com/blog/834859"
---

今天发先从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&lt;len-1;i++){  
   ch1 = str.charAt(i); //赋值  
   ch2 = str.charAt(i+1);  
   if(ch1 &gt;=48&&ch1 &lt;= 57 && ch2 &gt;=48&&ch2 &lt;= 57){  
    continue;  
   }if(ch1 &gt;=48&&ch1 &lt;= 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);  
    
 }  
}