将数据写入文件无效

时间:2016-10-05 02:20:19

标签: java file

我只是想知道我做错了什么。我试图将数据写入文件,它不断给我错误。如何将数据写入文件。我究竟做错了什么。任何帮助都会很棒。

import java.util.Scanner;
import java.io.*;

public class DistanceFile {

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

        String filename;
        int time; //hours traveled
        int hour;//for the formula
        String FileName;
        Scanner scanner= new Scanner(System.in);
        System.out.println("Enter the speed of the vehicle");
        int speed = scanner.nextInt();
        while (speed<=0){
            System.out.println("Please enter a valid speed");
            speed = scanner.nextInt();
        }
        System.out.println("Enter the number of hours traveled");
        time = scanner.nextInt();
        while (time<=0){
            System.out.println("Please enter a valid time");
            time = scanner.nextInt();
        }
        hour=0;
        for (hour = 1; hour <= time; hour++)
            System.out.println(hour + " " + (hour * speed));
        System.out.print("Enter the filename:");
        filename = scanner.nextLine();
        File f = new File("C:\\test.txt");
        f.mkdirs();
        f.createNewFile();
        PrintWriter outputFile = new PrintWriter("C:\\test.txt");
        outputFile.println(hour + " " + (hour * speed));

        outputFile.flush();
        outputFile.close();
    }
}

我一直收到这个错误:

 Exception in thread "main" java.io.FileNotFoundException:  (Result too large)
    at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at DistanceFile.main(DistanceFile.java:32)

0 个答案:

没有答案