【发布时间】:2014-03-11 07:17:38
【问题描述】:
我不知道为什么我正在写入的文件是空白的。我正在替换某些字符,然后写出新的转换文件。它应该包含我正在导入的文件中的所有行。但是,当我打开它时,它完全是空白的。谢谢。
//this method find the number of occurrences of a character find in a string l
public static int numberOccurances(String l, char find){
int count=0; //sets the number of occurrences to 0
for(int x=0; x<l.length();x++){ //searches throughout the string adding one to count
if(l.charAt(x)==find)
count++;
}
return count;
}
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
File file = new File("new.txt");
Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter("new.txt", "UTF-8");
while(scanner.hasNextLine()){
String line = scanner.nextLine();
for(int y=0; y<line.length(); y++){
if(line.charAt(y)=='M')
line=line.substring(0,y) + 'm' + line.substring(y+1);
if(line.charAt(y)=='m')
line=line.substring(0,y) + 'M' + line.substring(y+1);
}
int numberm=numberOccurances(line, 'm');
int numberM = numberOccurances(line, 'M');
line=line + "%:m" + numberm + ":M" + numberM + ":";
writer.println(line);
}
writer.close();
}
【问题讨论】:
标签: java printwriter