你问的不明白,我在代码里给你写了,你自己看吧
import java.io.FileWriterimport java.io.IOException
import java.util.Date
import java.util.Scanner
public class Test {
public static void main(String[] args) {
// 100,101,102,103,104,105
// 你问的不明不白,这6个数是一个字符串还是说是要求一个一个输入然后保存
侍腔塌 // step1:字符串
String str = "100,101,102,103,104,105"
str = "[" + str + "]\n"
append2File("D:\\test.txt", str)
// step2: 手动输入
Scanner sc = new Scanner(System.in)
StringBuilder str2 = new StringBuilder("[")
int i = 0
System.out.println("输入6个数")
圆伏 while (i < 6) {
int number = sc.nextInt()
str2.append(number).append(",")
i++
}
str2 = str2.deleteCharAt(str2.length() - 1).append("]\n")
append2File("D:\\test.txt", str2.toString())
}
public static void append2File(String path, String content) {
FileWriter writer = null
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(path, true)
writer.write(content)
} catch (IOException e) {
e.printStackTrace()
} finally {
if (writer != null) {
try {
writer.close()
老圆 } catch (IOException e) {
e.printStackTrace()
}
}
System.out.println(" *** 作完成.." + new Date())
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)