在Java中读取和写入.txt文件 [英] Reading and Writing to a .txt file in Java

查看:112
本文介绍了在Java中读取和写入.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的分配提示:
您的程序需要从文本文件中读取信息,而不是使用扫描程序从命令行读取。
您的程序还需要将消息写入文本文件,而不是在屏幕上显示消息。



我已编写并运行代码以提示使用CLI。但是,我完全不知道如何使用文本文件更改代码。有人可以向我解释一下吗?



到目前为止的代码:

  import java.util。*; 

public class parallelArrays {

public static void main(String [] args){
String [] names;
int [] exam1Grades;
int [] exam2Grades;
int n,sum1,sum2;
double avg1,avg2;

Scanner kb = new Scanner(System.in);

//从用户那里获取学生人数
System.out.print(输入学生人数:);
n = kb.nextInt();

//分配数组
names = new String [n];
exam1Grades = new int [n];
exam2Grades = new int [n];

//输入名称和等级
for(int i = 0; i< = names.length-1; i ++){
System.out.print(Enter学生姓名#+(i + 1)+:);
names [i] = kb.next();
System.out.print(输入考试#1年级:);
exam1Grades [i] = kb.nextInt();
System.out.print(输入考试#2等级:);
exam2Grades [i] = kb.nextInt();
}

//加起所有等级(可以在上面的循环中完成)
sum1 = 0;
sum2 = 0;
for(int i = 0; i< = names.length-1; i ++){
sum1 = sum1 + exam1Grades [i];
sum2 = sum2 + exam2Grades [i];
}

//计算并输出平均值
avg1 = sum1 / n;
System.out.println();
System.out.println(考试#1平均:+ avg1);

avg2 = sum2 / n;
System.out.println(考试#2平均:+ avg2);
System.out.println();

//将每个等级与平均
进行比较(int i = 0; i< = names.length-1; i ++){
if(exam1Grades [i]> ; avg1)
System.out.println(学生+姓名[i] +在考试1中高于平均水平);
else if(exam1Grades [i]< avg1)
System.out.println(Student+ names [i] +低于考试1的平均值);
else
System.out.println(Student+ names [i] +是考试1中的平均值);

if(exam2Grades [i]> avg2)
System.out.println(Student+ names [i] +在考试2中高于平均水平);
else if(exam2Grades [i]< avg2)
System.out.println(Student+ names [i] +低于考试2的平均值);
else
System.out.println(Student+ names [i] +是考试2中的平均值);

}
}
}


解决像@Shobit先前所说的那样,将BufferedWriter与FileWriter一起使用,并使用write()和newLine()方法,您可以将所需的行插入文件而不是使用Println。 / p>

  BufferedWriter writer = new BufferedWriter(new FileWriter(path-of-file)); //你不需要创建一个File对象,FileWriter也为文件路径获取一个字符串以及
writer.write(Student number ...);
writer.writeLine(); //对于文件中的新行

当你写完文件后,

  writer.close(); 


Here is my prompt for the assignment: Your program needs to read the information from a text file instead of using a scanner to read from command line. Your program also needs to write the messages into a text file instead of showing the messages on the screen.

I have the code written and running for it to prompt using the CLI. However, I have absolutely no idea how to change the code utilizing a text file. Can someone please explain this to me?

Code so far:

import java.util.*;

public class parallelArrays {

    public static void main(String[] args) {
       String[] names;
       int[] exam1Grades;
       int[] exam2Grades;
       int n, sum1, sum2;
       double avg1,avg2;

       Scanner kb = new Scanner(System.in);

       // Get the number of students from the user
       System.out.print("Enter # of students:");
       n = kb.nextInt();

       // Allocate the arrays
       names = new String[n];
       exam1Grades = new int[n];
       exam2Grades = new int[n];

       // Input the names and grades
       for (int i=0; i<=names.length-1; i++) {
          System.out.print("Enter name for student #" + (i+1) + ":");
          names[i] = kb.next();
          System.out.print("Enter exam #1 grade:");
          exam1Grades[i] = kb.nextInt();
          System.out.print("Enter exam #2 grade:");
          exam2Grades[i] = kb.nextInt();
       }

       // Add up all the grades (could have been done in the above loop)
       sum1 = 0;
       sum2 = 0;
       for (int i=0; i<=names.length-1; i++) {
          sum1 = sum1 + exam1Grades[i];
          sum2 = sum2 + exam2Grades[i];
       }

       // Calculate and output the averages
       avg1 = sum1/n;
       System.out.println();
       System.out.println("Exam #1 average: " + avg1);

       avg2 = sum2/n;
       System.out.println("Exam #2 average: " + avg2);
       System.out.println();

       // Compare each grade to the average
       for (int i=0; i<=names.length-1; i++) {
       if (exam1Grades[i] > avg1)
           System.out.println("Student " + names[i] + " is above average on exam 1");
           else if (exam1Grades[i] < avg1)
           System.out.println("Student " + names[i] + " is below average on exam 1");
           else
           System.out.println("Student " + names[i] + " is average on exam 1");

       if (exam2Grades[i] > avg2)
           System.out.println("Student " + names[i] + " is above average on exam 2");
           else if (exam2Grades[i] < avg2)
           System.out.println("Student " + names[i] + " is below average on exam 2");
           else
           System.out.println("Student " + names[i] + " is average on exam 2");

       }
    }
}

解决方案

like @Shobit said previously, use a BufferedWriter in conjunction with a FileWriter and with the methods write() and newLine() you can insert the lines you want to the file in stead of using Println.

BufferedWriter writer = new BufferedWriter(new FileWriter("path-of-file")); //you don't need to create a File object, FileWriter takes a string for the filepath as well
writer.write("Student number..."); 
writer.writeLine(); //for a new line in the file

and when you are done writing to the file,

writer.close();

这篇关于在Java中读取和写入.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆