存入数组,完整的代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Du01 {
public static void main(String[] args) throws IOException {
int temp[]={10,2,3,4,6,8,9,3,7,-8};
String aryStr = aryToString(temp);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\test.txt"));
bw.write(aryStr);
bw.close();
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt"));
String content = br.readLine();
System.out.println(content);
int[] newAry =strToArray(content.trim());
for(int i = 0; i < newAry.length; i++){
System.out.println(newAry[i] + "\t");
}
}
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;
}
private static String aryToString(int[] ary){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < ary.length; i++){
sb.append(ary[i]).append(",");
}
sb.deleteCharAt(sb.length() -1);
return sb.toString();
}
}