从文件number.txt中读取数据,让用户修改数据的内容,然后用PrintWriter方法存入文件numbers.txt中,再从文件numbers.txt中用FileInputStream方法把数据读取出来显示在屏幕上。用帮我补充以下代码: package test8_0324; import java.util.Scanner; import java.io.*; public class test8_two { public static void main(String args[]) throws IOException { Scanner s1=new Scanner(System.in); System.out.println("请输入10个数据:"); PrintWriter s2=new PrintWriter("number.txt"); for(int i=0;i<10;i++) { int x=s1.nextInt(); s2.print(x+" "); } s1.close(); s2.close(); BufferedReader xk=new BufferedReader(new InputStreamReader(new FileInputStream("number.txt"))); String line; while ((line=xk.readLine())!=null) { System.out.println("读取的数据为"+line); } xk.close(); } }
时间: 2023-07-15 17:13:40 浏览: 203
java 读取文件数据
package test8_0324;
import java.util.Scanner;
import java.io.*;
public class test8_two {
public static void main(String args[]) throws IOException {
Scanner s1=new Scanner(System.in);
System.out.println("请输入10个数据:");
PrintWriter s2=new PrintWriter("number.txt");
for(int i=0;i<10;i++) {
int x=s1.nextInt();
s2.print(x+" ");
}
s1.close();
s2.close();
BufferedReader xk=new BufferedReader(new InputStreamReader(new FileInputStream("number.txt")));
String line;
while ((line=xk.readLine())!=null) {
System.out.println("读取的数据为"+line);
String[] numbers = line.split(" "); // 将一行字符串按空格分隔成数组
Scanner scannerUpdate = new Scanner(System.in);
System.out.println("请选择要修改的数据的下标:");
int index = scannerUpdate.nextInt(); // 获取要修改的数据的下标
System.out.println("请输入新的数据:");
int newValue = scannerUpdate.nextInt(); // 获取新的数据
numbers[index] = String.valueOf(newValue); // 将修改后的数据存入数组
scannerUpdate.close();
PrintWriter s3=new PrintWriter("numbers.txt");
for(int i=0;i<numbers.length;i++) {
s3.print(numbers[i]+" ");
}
s3.close();
}
xk.close();
BufferedReader xk2=new BufferedReader(new InputStreamReader(new FileInputStream("numbers.txt")));
String line2;
while ((line2=xk2.readLine())!=null) {
System.out.println("修改后的数据为"+line2);
}
xk2.close();
}
}
阅读全文