java中怎么把资料存放在磁盘里?
publicstaticvoidmain(String[]args){intchinese,manth,english,science,history,number;Sc...
public static void main(String[] args){
int chinese,manth,english,science,history,number;
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的语文成绩:");
chinese = sc.nextInt();
System.out.println("请输入您的数学成绩:");
manth = sc.nextInt();
System.out.println("请输入您的英语成绩:");
english = sc.nextInt();
System.out.println("请输入您的科学成绩:");
science = sc.nextInt();
System.out.println("请输入您的历史成绩:");
history = sc.nextInt();
if(chinese<=101&&chinese>=-1){
System.out.println("您的语文成绩:"+chinese);}
if(manth<=101&&manth>=-1){
System.out.println("您的数学成绩为:"+manth);}
if(english<=101&&english<=-1){
System.out.println("您的英语成绩为:"+english);}
if(science<=101&&science>=-1){
System.out.println("您的科学成绩为:"+science);}
if(history<=101&&history>=-1){
System.out.println("您的历史成绩为:"+history);}
if(chinese+manth+english+science+history==500);
System.out.println("您的综合成绩为:"+(chinese+manth+english+science+history));
System.out.println("您的成绩表:"+"语文:"+chinese+"数学:"+manth+"英语:"+english+"科学:"+science+"历史:"+history);
}
}
计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud " 中
这个怎么搞? 展开
int chinese,manth,english,science,history,number;
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的语文成绩:");
chinese = sc.nextInt();
System.out.println("请输入您的数学成绩:");
manth = sc.nextInt();
System.out.println("请输入您的英语成绩:");
english = sc.nextInt();
System.out.println("请输入您的科学成绩:");
science = sc.nextInt();
System.out.println("请输入您的历史成绩:");
history = sc.nextInt();
if(chinese<=101&&chinese>=-1){
System.out.println("您的语文成绩:"+chinese);}
if(manth<=101&&manth>=-1){
System.out.println("您的数学成绩为:"+manth);}
if(english<=101&&english<=-1){
System.out.println("您的英语成绩为:"+english);}
if(science<=101&&science>=-1){
System.out.println("您的科学成绩为:"+science);}
if(history<=101&&history>=-1){
System.out.println("您的历史成绩为:"+history);}
if(chinese+manth+english+science+history==500);
System.out.println("您的综合成绩为:"+(chinese+manth+english+science+history));
System.out.println("您的成绩表:"+"语文:"+chinese+"数学:"+manth+"英语:"+english+"科学:"+science+"历史:"+history);
}
}
计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud " 中
这个怎么搞? 展开
2个回答
展开全部
//写入文件方法,参数:文件名,待写入字符串数组,是否追加到文件
public void writeLinesToFile(String filename,String[] linesToWrite,boolean appendToFile)
{
PrintWriter pw = null;
try {
if (appendToFile) {
//If the file already exists, start writing at the end of it.
pw = new PrintWriter(new FileWriter(filename, true));
}
else {
pw = new PrintWriter(new FileWriter(filename));
//this is equal to:
//pw = new PrintWriter(new FileWriter(filename, false));
}
for (int i = 0; i < linesToWrite.length; i++) {
pw.println(linesToWrite[i]);
}
pw.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
//Close the PrintWriter
if (pw != null)
pw.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
文件流呀,你先把你从控制台得到的数据保存起来,再写到流里
追问
能给张截图吗?
追答
int chinese;
StringBuffer sb = new StringBuffer();
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的语文成绩:");
chinese = sc.nextInt();
System.out.println("您的语文成绩:"+chinese);
sb.append("Chinese:"+chinese);
File file = new File("d:\\stu.txt");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询