交作業可否在優化

来源:3-7 自由编程

胖達

2019-10-14 22:11:13

package com.imooc.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamHome {

	public static void main(String[] args) {

		// TODO Auto-generated method stub
		try {
			FileInputStream fi = new FileInputStream("speech.txt");
			int n = 0;
			String[] st = new String[100];
			int count = 0 ;
			System.out.print("文本內容:");
			while ((n = fi.read()) != -1) {
				
				System.out.print((char)n);
				count++;				
			}
			System.out.println();
			System.out.println("統計結果:speech.txt文件中共有"+count+"個字節");
			
			

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}


写回答

1回答

好帮手慕小尤

2019-10-15

同学你好,测试代码是可以的哦!棒棒哒!继续加油!不过有一个小建议,建议使用完后,将fi输入流关闭。如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 11489 问题

查看课程