创建文件不写java

时间:2015-10-18 05:36:35

标签: java file-io writefile

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

        @SuppressWarnings("unused")
        Scanner in = new Scanner(System.in);
        System.out.println("enter filename");
        Filename=in.next();
        PrintWriter outputFile  =new PrintWriter(Filename);
        outputFile.println();
        outputFile.close();
        getInput();
        display();

    }

    public static void display() throws FileNotFoundException{
        for (int i = 0; i < genders.length; i++) {
            System.out.println(ages[i]+";"+genders[i]+";"+emails[i]+";"+salaries[i]);
    }}


    public static void getInput(){

        System.out.print("How many users do you wish to enter: ");
        int num = in.nextInt();
        ages= new int[num];
        genders = new String[num];
        emails = new String[num];
        salaries = new double[num];

        for (int i = 0; i < num; i++) {
            System.out.print("Please enter your age for person "+(i+1)+": ");
            ages[i] = in.nextInt();
            while (ages[i]<20 ||ages[i]>30){
                System.out.println("invalid age please re enter again");
                ages[i] = in.nextInt();}
            in.nextLine();
嘿伙计们我正在尝试将用户输入的内容写入文件。我的问题是它创建了文件,但没有写入它。我尝试了各种方法,但没有任何帮助吗?

2 个答案:

答案 0 :(得分:2)

    Filename=in.next();
    PrintWriter outputFile  =new PrintWriter(Filename);
    outputFile.println();

我认为您正在创建具有用户输入名称的文件,并打印一个空行

答案 1 :(得分:0)

您的问题在这里得到解答: https://stackoverflow.com/questions/18070629/how-to-read-from-users-input-in-jav‌​a-and-write-it-to-a-file

但总结一下:您只是创建一个以用户输入命名的文件。您需要将该信息实际写入文件:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class MainClass {
    private static String   fileName;
    private static String[] genders, emails;
    private static double salaries[];
    private static int    userCount;
    private static int[]  ages;
    private static Scanner in = new Scanner(System.in);

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

        System.out.println("enter filename");
        fileName = in.nextLine();
        File myFile = new File(fileName);
        getInput();
        display();
        FileWriter fWriter = null;
        BufferedWriter writer = null;
        try {
            fWriter = new FileWriter(myFile);
            writer = new BufferedWriter(fWriter);
            writer.write(display().toString());
            writer.newLine();
            writer.close();
        } catch (Exception e) {
            System.out.println("Error!");
        }
    }

    public static ArrayList<String> display() throws FileNotFoundException {
        ArrayList<String> data = new ArrayList<String>();

        for (int i = 0; i < genders.length; i++) {

            data.add(ages[i] + ";" + genders[i] + ";" + emails[i] +
                     ";" + salaries[i]);
        }

        for (int i = 0; i < genders.length; i++) {

            System.out.println(ages[i] + ";" + genders[i] + ";" + emails[i] +
                               ";" + salaries[i]);
        }
        return data;
    }

    private static void getInput() {
        System.out.print("How many users do you wish to enter: ");
        int userCount = in.nextInt();
        ages = new int[userCount];
        genders = new String[userCount];
        emails = new String[userCount];
        salaries = new double[userCount];

        for (int i = 0; i < userCount; i++) {
            System.out.print("Please enter your age for person " + (i + 1) +
                             ": ");
            ages[i] = in.nextInt();
            while (ages[i] < 20 || ages[i] > 30) {
                System.out.println("invalid age please re enter again");
                ages[i] = in.nextInt();
            }
            in.nextLine();
        }
    }
}