我刚刚开始学习JAVA,希望大家不要见笑,
个高手情不吝赐教,帮我检查一下,谢谢,感激import java.io.*;
public class FileTest{
public static void main(String args[]) throws IOException{
try{
InputStream myInput=new FileInputStream("SystemTest.java");
OutputStream myOutput=new FileOutputStream("SystemTest.txt");
int c;
int LowNum=1;
while((c=myInput.read())!=-1){
if(c=='\' && myInput.read()=='\'){
while(c!='\0'){};
}
myOutput.write(c);
}
myInput.close();
myOutput.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    注意JAVA中的转义字符:
    if(c=='\\' && myInput.read()=='\\'){
    while(c!='\0'){};
    }
      

  2.   

    这样写就可以了
    使用RandomAccessFile类读写文件
    每次操作一行,添加行号比较简单,主要的就是去掉注释时的操作。import java.io.RandomAccessFile;public class Test{ //测试注释代码1
    public static void main(String args[])
    {
    try
    {
    /*
    测试注释代码2
    */
    RandomAccessFile in = new RandomAccessFile("H:\\Test.java","r");
    RandomAccessFile out = new RandomAccessFile("H:\\Test.txt","rw");

    int line=0;

    String tmp;
    while(null != (tmp=in.readLine()))
    {
    int index = tmp.indexOf("//");
    if(-1 != index)
    {
    tmp = tmp.substring(0,index);
    }

    index = tmp.indexOf("/*");
    if(-1 != index)
    {
    tmp = tmp.substring(0,index);

    line++;
    tmp = line+" "+tmp+"\r\n";
    out.writeBytes(tmp);

    String tmp2;
    int index2;
    while(null != (tmp2=in.readLine()))
    {
    index2 = tmp2.indexOf("*/");
    if(-1 != index2)
    {
    tmp2 = tmp2.substring(index2+2);

    line++;
    tmp2 = line+" "+tmp2+"\r\n";
    out.writeBytes(tmp2);
    break;
    }
    }
    }
    else
    {
    line++;
    tmp = line+" "+tmp+"\r\n";
    out.writeBytes(tmp);
    }
    }
    }catch(Exception e){e.printStackTrace();}
    }
    }
      

  3.   

    不好意思,缩进控制得不好,机子上的Tab设置的是8个空格
    改了一下不知道显示的效果会不会好些import java.io.RandomAccessFile;public class Test{  //测试注释代码1
      public static void main(String args[])
      {
        try
        {
          /*
          测试注释代码2
          */
              RandomAccessFile in = new RandomAccessFile("H:\\Test.java","r");
              RandomAccessFile out = new RandomAccessFile("H:\\Test.txt","rw");
              
              int line=0;
              
              String tmp;
              while(null != (tmp=in.readLine()))
              {
                int index = tmp.indexOf("//");
                if(-1 != index)
                {
                  tmp = tmp.substring(0,index);
                }
                
                index = tmp.indexOf("/*");
                if(-1 != index)
                {
                  tmp = tmp.substring(0,index);
                  
                  line++;
                  tmp = line+" "+tmp+"\r\n";
                  out.writeBytes(tmp);
                  
                  String tmp2;
                  int index2;
                  while(null != (tmp2=in.readLine()))
                  {
                    index2 = tmp2.indexOf("*/");
                    if(-1 != index2)
                    {
                      tmp2 = tmp2.substring(index2+2);
                      
                      line++;
                      tmp2 = line+" "+tmp2+"\r\n";
                      out.writeBytes(tmp2);
                      break;
                    }
                  }
                }
                else
                {
                  line++;
                  tmp = line+" "+tmp+"\r\n";
                  out.writeBytes(tmp);
                }
              }
        }catch(Exception e){e.printStackTrace();}
      }
    }
      

  4.   

    sorry
    最后还应该加上
    in.close();
    out.close();