代码如下
基本上可以去除所有的注释了
package test;
import java.io.*;
public class test
{
/*编写一个能够整理“*.Java“源程序代码的程序,
* 生成新源代码文件名为 “*2.java”,
* 整理要求如下:
* ①删除*.java 文件中的空行;
* ②对注释行的处理: 删除“//”起始的整代码; 对非“//“起始的行,删除“//”之后的内容;
* ③给所有行依次在行首加入行号。
*/
public static void main(String[] args) throws Exception
{
RandomAccessFile file = new RandomAccessFile("test.java","r");
Writer file2 = new FileWriter("test2.java");
StringBuilder result = new StringBuilder();
String s = null;
byte b[];//字符数组
int nu = 1;//行号,满足要求3
while((s = file.readLine()) != null)
{
b = s.getBytes("iso-8859-1");
String str = new String(b,"utf-8"); //原始可读的一行
String one = str.trim(); //去除首尾空格
/*第一次去除:空行*/
if(one.equals(""))
continue;
/*第二次去除://或*开始的行*/
String two = one.substring(0, 1);//截取去除首位空格的字符串的第一个字符
if(two.equals("/") || two