繁体   English   中英

尝试创建文件时出现FileNotFound异常

[英]I'm getting FileNotFound exception while trying to create a file

我试图提示用户输入要写入的文件名,创建该.txt文件,然后将符合条件的文本行写入该文件并保存。 在do while内,似乎跳过了用户输入以获取他们想要保存到的文件的名称,然后循环返回并得到FileNotFoundException,它甚至不应该在寻找文件。

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

public class Main {

    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        Scanner docInName = null;
        PrintWriter docOutName = null;

        do {
            System.out.println("Please enter the filename of the file you 
                                would like to read from: ");
            try {
                docInName = new Scanner(new File(user.nextLine()));
            } catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }
        } while (docInName == null);

        int lineNum = docInName.nextInt();
        BikePart[] bp = new BikePart[lineNum];
        System.out.println("please enter the max cost for a part: ");
        int cost = user.nextInt();

        do {
            System.out.println("please enter a name for the file to write to 
                                (end with .txt): ");
            String out = user.nextLine();  //PROBLEM HERE! SKIPS USER INPUT
            try {
                docOutName = new PrintWriter(out);
                for (int i = 0; i < lineNum; i++) {
                    String line = docInName.nextLine();
                    String[] elements = line.split(",");
                    bp[i] = new BikePart(elements[0], 
                            Integer.parseInt(elements[1]), 
                            Double.parseDouble(elements[2]),
                            Double.parseDouble(elements[3]), 
                            Boolean.parseBoolean(elements[4]));
                    double temp = Double.parseDouble(elements[3]);
                    if ((temp < cost && bp[i].isOnSale() == true)
                            || (bp[i].getListPrice() < cost && 
                                bp[i].isOnSale() == false)) {
                        docOutName.write(line);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } while (docOutName == null);
        user.close();
    }

}

我只需要在循环开始之前跳过一行即可。

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

public class Main {

    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        Scanner docInName = null;
        PrintWriter docOutName = null;

        do {
            System.out.println("Please enter the filename of the file you would like to read from: ");
            try {
                docInName = new Scanner(new File(user.nextLine()));
            } catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }
        } while (docInName == null);

        int lineNum = docInName.nextInt();
        BikePart[] bp = new BikePart[lineNum];
        System.out.println("please enter the max cost for a part: ");
        int cost = user.nextInt();
        user.nextLine();    //SOLUTION HERE

        do {
            System.out.println("please enter a name for the file to write to (end with .txt): ");
            String out = user.nextLine();
            try {
                docOutName = new PrintWriter(out);
                for (int i = 0; i < lineNum; i++) {
                    String line = docInName.nextLine();
                    String[] elements = line.split(",");
                    bp[i] = new BikePart(elements[0], Integer.parseInt(elements[1]), Double.parseDouble(elements[2]),
                            Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4]));
                    double temp = Double.parseDouble(elements[3]);
                    if ((temp < cost && bp[i].isOnSale() == true)
                            || (bp[i].getListPrice() < cost && bp[i].isOnSale() == false)) {
                        docOutName.write(line);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } while (docOutName == null);
        user.close();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM