需求:读取一个.java文件,并打印到控制台上。
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("E:\\FileReaderDemo.java");
char[] buf = new char[1024];
int num;
while ((num = reader.read(buf)) != -1) {
System.out.print(new String(buf, 0, num));
}
reader.close();
}
}
网友评论