Java-流的简单使用:读取文件、写入文件(面试题:删除注释代码)
摘要:前天面试Android时遇到机试,题目是删除注释中的代码。机试开始时不能上网,我恰好忘了读写字符流的相关代码API,幸好后来能上网查关于流的API,最终得以把程序写出来。
题目:请编程将Example.c文件中的所有注释删除,将结果写入Example.i文件中。
输入用例Example.c:
struct a { char a /*2392839238923 /*kasdlfjasld*/; char b; //23232392839 }; /* askdashfla' asdfalskdfhlksafdhla fasdjfhalsdjfhlasdf askjdfhlkajds * / */ void Function(int param /*232323* / */) { //*2323234234l int i = 1;//2923939 char str[] = "/*232323"; int j = 0; i = i / j * i; strcpy(str, "/*lakjslkdflksd*/"); }
我编写的程序:
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 Test { public static void main(String args[]) { String InputPath="../Example_1.c"; String OutputPath="../Example_1.i"; String Data=LoadFile(InputPath); Data=Handle(Data); WriteFile(Data,OutputPath); } //把文件中的数据转化为字符串 private static String LoadFile(String path) { File file = new File(path); BufferedReader reader=null; String temp; String result=""; try { reader =new BufferedReader(new FileReader(file)); while((temp=reader.readLine())!=null) { result=result+temp+"\r\n"; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(result); return result; } //把数据写进文件 private static void WriteFile(String data,String path) { try { FileWriter fw=new FileWriter(path); BufferedWriter bw=new BufferedWriter(fw); bw.write(data); bw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return; } //删掉注释的方法 private static String Handle(String data) { StringBuffer sb= new StringBuffer(); int posStar = 0,posEnd=0,i=0; while(i<data.length()) { System.out.print(data.charAt(i)); if(data.charAt(i)=='"') { do { sb.append(data.charAt(i)); i++; }while(data.charAt(i)!='"'); sb.append(data.charAt(i)); i++; continue; } else if(data.charAt(i)=='/') { if(data.charAt(i+1)=='/') { do { i++; }while(data.charAt(i)!='\r'); sb.append(data.charAt(i)); i++; continue; } else if(data.charAt(i+1)=='*') { do { i++; }while(data.charAt(i)!='*'||((data.charAt(i)=='*')&&(data.charAt(i-1)=='/'))); do { i++; }while(data.charAt(i)!='/'); i++; posStar=i; continue; } } else if(data.charAt(i)=='*') { if(data.charAt(i+1)=='/') { posEnd=i; i=i+2; System.out.println(posStar+" "+posEnd+" "+sb.length()); sb.delete(sb.length()-(posEnd-posStar), sb.length()); continue; } else { while(data.charAt(i+1)==' ') { sb.append(data.charAt(i)); i++; } if(data.charAt(i+1)=='/') { posEnd=i; i=i+2; sb.delete(sb.length()-(posEnd-posStar), sb.length()); continue; } else{ continue; } } } sb.append(data.charAt(i)); i++; } return new String(sb); } }
输出结果:
struct a { char a ; char b; }; void Function(int param ) { int i = 1; char str[] = "/*232323"; int j = 0; i = i / j * i; strcpy(str, "/*lakjslkdflksd*/"); }
还有更好的答案吗?望共同探讨。
一个人无梦想,同条咸鱼有咩分别?!