读取和写入文件Java

时间:2017-11-29 15:13:30

标签: java file-writing

今天我带着一个问题来到这个网站。我搜索了网站,但找不到任何远程接近回答我的问题。

所以我写这个程序应该有4个选项,其中2个是&#34; Read&#34; &#34; Write&#34; < / strong>到文件。我已经完成了大部分代码,但我很难将程序&#34; Read&#34; &#34; Write&#34; 添加到文件。我相信这可能是一个扫描仪的问题,但我尝试了各种小技巧,没有一个可悲的工作。我愿意接受任何可能的帮助,这让我发疯,我确信我忽略了一些事情,这就是为什么我今天请求你的帮助。下面是我的整个代码,然后是我遇到麻烦的确切部分。

(对于网站来说还是新手,对不起,如果格式不正确)

我有错误的错误代码:

Lab9_5.java:67: error: ';' expected
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
                             ^
Lab9_5.java:67: error: <identifier> expected
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
                                 ^
Lab9_5.java:83: error: <identifier> expected
        inFile = new (new FileReader("E:/savings.txt"));
                    ^
3 errors

CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab9_5 {

    //Declare global variable and Scanner object to read from keyboard
    static double[] notGreenCost = new  double[6];
    static double[] goneGreenCost = new double[6];
    static double[] savings = new double[6];
    static String[] months = new String[6];
    static Scanner in = new Scanner(System.in);


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

        String endProgram = "no";
        int option = 0;
        initMonths();

        while(endProgram.equals("no")){
            System.out.println("What do you like to do? Type:");
            System.out.println("1 to enter data");
            System.out.println("2 to display data");
            System.out.println("3 to write data to a file");
            System.out.println("4 to read data from a file");
            option = in.nextInt();

            if(option == 1){
                getNotGreen(notGreenCost, months);
                getGoneGreen(goneGreenCost, months);
                energySaved(notGreenCost, goneGreenCost, savings);
            }
            else if(option == 2){
                displayInfo(notGreenCost, goneGreenCost, savings, months);
            }
            else if(option == 3){
                writeToFile(months, savings);
            }
            else if(option == 4){ 
                readFromFile(months, savings);
            }

            System.out.println("Do you want to end the program (enter yes or no): ");
            endProgram = in.next();
            while(!endProgram.equals("no") && !endProgram.equals("yes")){ 
                System.out.println("Please enter a valid value of yes or no");
                endProgram = in.next();
            }

        }


    }

// Write to file function
    public static void writeToFile(String[] months, double[] savings) throws IOException{
        PrintWriter savings = null;
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
        outFile.write("Savings");
        outFile.newLine();
        int counter = 0;

        while(counter<6){
            outFile.write(months[counter]);
            outFile.newLine();
            outFile.write(savings[counter]+"");
            outFile.newLine();
            counter++;
        }
        outFile.close();
    }
// Read from file function
    public static void readFromFile(String[] months, double[] savings) throws IOException{
        inFile = new (new FileReader("E:/savings.txt"));
        String str1 = "";

        while((str1=inFile.readLine())!=null){
            System.out.println(str1);
        }
        inFile.close();
    }
// InitMonth function
    public static void initMonths(){
        months = new String[]{"January", "February", "March", "April", "May", "June"};
    }
// Get not green function
    public static void getNotGreen(double[] notGreenCost, String[] months){
        int counter = 0;
        while(counter<6){
            System.out.print("Enter NOT GREEN energy cost for "+months[counter]+": ");
            notGreenCost[counter] = in.nextDouble();
            counter++;
        }
    }
// Get gone green function
    public static void getGoneGreen(double[] goneGreenCost, String[] months){
        int counter = 0;
        while(counter<6){
            System.out.print("Enter GONE GREEN energy cost for "+months[counter]+": ");
            goneGreenCost[counter] = in.nextDouble();
            counter++;
        }
    }
// Energy saved function
    public static void energySaved(double[] notGreenCost, double[] goneGreenCost, double[] savings){
        int counter = 0;
        while(counter<6){ 
            savings[counter] = notGreenCost[counter] - goneGreenCost[counter];
            counter++;
        }
    }
// Display info function
    public static void displayInfo(double[] notGreenCost, double[] goneGreenCost, double[] savings, String[] months){
        System.out.println("SAVINGS     NOT GREEN    GONE GREEN        MONTH");
        System.out.println("_________________________________________________");
        int counter = 0;
        while(counter<6){
            System.out.println("$" + savings[counter] + "      $" + notGreenCost[counter] + 
        "        $" + goneGreenCost[counter] + "        " +  months[counter]);
            counter++;
        }
    }
} 

该守则的问题领域:

// Write to file function
    public static void writeToFile(String[] months, double[] savings) throws IOException{
        PrintWriter savings = null;
        outFile = PrintWriter new (new FileWriter("E:/savings.txt"));
        outFile.write("Savings");
        outFile.newLine();
        int counter = 0;

        while(counter<6){
            outFile.write(months[counter]);
            outFile.newLine();
            outFile.write(savings[counter]+"");
            outFile.newLine();
            counter++;
        }
        outFile.close();
    }
// Read from file function
    public static void readFromFile(String[] months, double[] savings) throws IOException{
        inFile = new (new FileReader("E:/savings.txt"));
        String str1 = "";

        while((str1=inFile.readLine())!=null){
            System.out.println(str1);
        }
        inFile.close();
    }

0 个答案:

没有答案