import java.io.*;
public class experiment11_1 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try (
FileInputStream fin = new FileInputStream("D:/study/temp/bigbook.txt");
FileOutputStream fout = new FileOutputStream(FileDescriptor.out);
) {
int nums = 0;
while(fin.available()>0){
if(fin.read()=='A')
nums++;
}
System.out.println("A的个数有 " + nums + " 个");
} catch (FileNotFoundException e) {
System.out.println("bigbook.txt can't fount");
} catch (IOException e) {
}
long end = System.currentTimeMillis();
System.out.println("共耗时:"+(end-start)+"ms");
}
}
你的写法是jdk1.7之后引入的try-with-resource,()里面放的是实现了AutoCloseable的资源,这个接口里面的唯一方法是close方法,即在try代码块结束后会调用close方法自动关闭资源。你又把FileOutputStream放在()里面,结束时就会被关闭,而System.out调用的时PrintStream对象,这个对象继承FileOutputStream,这个输出流就被关了,在打印肯定就没东西。
什么编辑软件,myeclipse,eclipse,sts这些软件都得按ctrl+s保存的;断点一下,一步一步走看看。
使用try catch的标准结构
别忘了点击采纳哟
import java.io.*;
/**
* @author Tellsea
* @date 2021/11/17
*/
public class TestDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
FileInputStream fin = new FileInputStream("D:/aaa.txt");
FileOutputStream fout = new FileOutputStream(FileDescriptor.out);
int nums = 0;
while (fin.available() > 0) {
if (fin.read() == 'A')
nums++;
}
System.out.println("A的个数有 " + nums + " 个");
} catch (FileNotFoundException e) {
System.out.println("bigbook.txt can't fount");
} catch (IOException e) {
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "ms");
}
}
这个可能是try(){}和try{}有区别,我猜测try()会自动关闭System的流,所以后面的代码不会打印输出。使用try{}是正常的。你这个要打出执行时间可以在try(){}的大括号内逻辑执行完的时候打印
你后面的代码都定义到函数外面去了。好好把括号整理整理。
你这代码左右括号数量都不一致,编译都过不去
编译的时候会提示你,编译错误,要执行之前的exe文件吗,你选了是,那你后面写的代码当然不会执行