我刚刚开始学习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();
}
}
}
个高手情不吝赐教,帮我检查一下,谢谢,感激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();
}
}
}
解决方案 »
- 在线等Java登录界面连接数据库问题
- 两道外企面试题~求解
- java新手请教
- 谁有lucene.jar的包给我发一个吧!!
- J2SE学通大概要花多厂时间?
- 关于KeyStroke的问题,请高手指点
- String lineEnd = System.getProperty("line.separator")是什么意思如何处理??
- java 字符串replace 问题 在线等
- 编译不过去????????
- 哪里有JBUILDER6.0企业版下在?
- 请问大家Class.forName(classname)和ClassLoader.loadClass(classname)都是动态加载类,它们有何区别?
- 初学的迷惑啊~~希望高手指点下~~小弟刚进CSDN!没什么分~~
if(c=='\\' && myInput.read()=='\\'){
while(c!='\0'){};
}
使用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();}
}
}
改了一下不知道显示的效果会不会好些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();}
}
}
最后还应该加上
in.close();
out.close();