从键盘上输入10个整数,写入到一个文件number.txt中,然后从此文件读出数据,显示在屏幕上。
import java.io.*; public class Test_8_1 { public static void main(String[] args) { int[] numbers = new int[10]; try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入10个整数:"); for (int i = 0; i < 10; i++) { String input = reader.readLine(); numbers[i] = Integer.parseInt(input); } reader.close(); FileWriter writer = new FileWriter("src/allTest/Test_8_1File"); for (int i = 0; i < 10; i++) { writer.write(numbers[i] + " "); } writer.close(); BufferedReader fileReader = new BufferedReader(new FileReader("src/allTest/Test_8_1File")); System.out.println("从文件中读取的数据:"); String line; while ((line = fileReader.readLine()) != null) { System.out.print(line+" "); } fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } }