java从键盘中输入100,101,102,103,104,105等五个数,并写入到dest.txt文件中,并以逆序读出显示在屏幕上

发布于2022-01-13 06:35:50
3个回答
admin
网友回答2022-01-13
BufferedWriter没有writeIndocument函数

strToArray函数定义有问题:
import java.io.*;

public class Demo5 {

public static void main(String args[]) throws IOException {

int arr[] = { 100, 101, 102, 103, 104, 105 };

BufferedWriter bw = new BufferedWriter(new FileWriter(Dest.txt)); // 写入数组
for (int i = 0; i arr.length; i++) {
//////////////////////////////////////////////////////
bw.write(Integer.toString(arr[i]) + ','); // 以,隔开,为下面部分的字符串转换为数组提供切割点
//////////////////////////////////////////////////////
}
bw.close();

BufferedReader br = new BufferedReader(new FileReader(Dest.txt)); // 读取Dest.txt

String begin = br.readLine(); // 输出原始数组
System.out.print(begin);

int newArray[] = strToArray(begin.trim()); // 将字符串转换为数组,并用trim函数去除字符串两端空白
for (int i = newArray.length - 1; i = 0; i--) // 将数组逆向输出
{
System.out.println(newArray[i]);
}

System.out.println();
br.close();

}
//////////////////////////////////////////////
private static int[] strToArray(String str) // 定义一个将字符串转换为数组的函数
//////////////////////////////////////////////
{
String strArray[] = str.split(,);

int arr[] = new int[strArray.length];
for (int i = 0; i strArray.length; i++) {
arr[i] = Integer.parseInt(strArray[i]);
}
return arr;
}
}
admin
网友回答2022-01-13
java从键盘中输入100,101,102,103,104,105等五个数,并写入到dest.txt文件中,并以逆序读出显示在屏幕上://programme name IODemo.java import java.io.*; public class IODemo { public static void main( String args[] ) { int data[] = {100,101,102,103,104,105}; int t; try { DataOutputStream out = new DataOutputStream (new FileOutputStream(“dest.txt”)); for(int i=0;idata.length;i++) out.WriteInt(data[i]); out.close(); DataInputStream in = new DataInputStream (new FileInputStream(“dest.txt”)); for(int i= data.length-1;i= 0;i--) { t=in.readInt(data[i]); System.out.print(“ ”+t); } System.out.println( ); in.close(); }catch(IOException e) {//倒序打印数组 System.out.println(e.getMessage());} } }
admin
网友回答2022-01-13
import java.util.*;import java.io.*;import java.lang.*;public class Test{public static void main(String[] args) throws IOException{Scanner sc = new Scanner(System.in);String[] Buffer = new String[6];for (int i=0;i6;i++){Buffer[i]=sc.nextLine();}FileWriter file = new FileWriter(d:/test.txt);BufferedWriter bw = new BufferedWriter(file);for (int i=0;i6 ;i++ ){bw.write(Buffer[i]+',');}bw.close();BufferedReader br =new BufferedReader(new FileReader(d:/test.txt)); String content = br.readLine();System.out.println(content);int[] newAry = strToArray(content.trim());for (int i =5; i=0; i-- ){System.out.println(newAry[i]);}}private static int[] strToArray(String str){String[] strAry = str.split(,);int[] ary = new int[strAry.length];for(int i =0; i strAry.length; i++){ary[i]= Integer.parseInt(strAry[i]);}return ary;}} 测试结果如图:

希望对你有帮助

回到
顶部