尝试使用扫描仪打开文件时找不到文件?

问题描述 投票:0回答:0

我正在尝试解决从多个输入文件中读取的问题。这些文件包含形状的名称以及与每个形状关联的参数。我应该从每个文件中获取形状名称并创建一个扩展名为 .out 的新文件并打印形状。但是,当我运行我的代码时,我总是收到“找不到文件”。输入文件已经创建并在我的 src 文件夹中。

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

public class FileHandler 
{
    public static void main(String[] args) throws FileNotFoundException 
    {
        writeToFile("bigInput1");
        writeToFile("bigInput2");
        writeToFile("bigShapes");
        writeToFile("greatestToLeast");
        writeToFile("mixedShapes1");
        writeToFile("mixedShapes2");
        writeToFile("rectangles");
        writeToFile("sampleTest");
        writeToFile("rhombi");
        writeToFile("sampleTest");
        writeToFile("triangles");
    }
    public static void writeToFile(String filename)
    {
        String triangle = "triangle";
        String rectangle = "rectangle";
        String rhombus = "rhombus";
        try 
        {
            File file = new File(filename+".in");
            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) 
            {
                String line = scanner.nextLine();
                if (line.contains(triangle)) 
                {
                    inputTriangle(filename);
                }
                if (line.contains(rectangle)) 
                {
                    inputRectangle(filename);
                }
                if (line.contains(rhombus))
                {
                    inputRhombus(filename);
                }
            }
            scanner.close();
        } 
        catch (FileNotFoundException e) 
        {
            System.out.println("File not found");
        }
    }

    public static void inputTriangle(String path) 
    {
        File newFile = new File(path+".in");
        Scanner input;
        try 
        {
            input = new Scanner(newFile);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        int length = input.nextInt();
        Triangle triangle = new Triangle(length);
        try 
        {
            FileWriter myWriter = new FileWriter("rectangle.out");
            myWriter.write(triangle.getTriangle());
            myWriter.close();
            System.out.println(triangle.getTriangle());
        } 
        catch (Exception e) 
        {
            System.out.println("An error occurred!");
        }
    }

    public static void inputRectangle(String path) 
    {
        File newFile = new File(path+".in");
        Scanner input = null;
        try 
        {
            input = new Scanner(newFile);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        int length = input.nextInt();
        int width = input.nextInt();
        Rectangle rectangle = new Rectangle(length, width);

        try 
        {
            FileWriter myWriter = new FileWriter(path+".out");
            myWriter.write(rectangle.getRectangle());
            myWriter.close();
            System.out.println(rectangle.getRectangle());
        } 
        catch (Exception e) 
        {
            System.out.println("An error occurred!");
        }
    }
    public static void inputRhombus(String path) 
    {
        File newFile = new File(path+".in");
        Scanner input = null;
        try 
        {
            input = new Scanner(newFile);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        int height = input.nextInt();
        Rhombus r1 = new Rhombus(height);

        try 
        {
            FileWriter myWriter = new FileWriter(path+".out");
            myWriter.write(r1.getRhombus());
            myWriter.close();
            System.out.println(r1.getRhombus());
        }
        catch (Exception e) 
        {
            System.out.println("An error occurred!");
        }
    }
}
file java.util.scanner filenotfoundexception illegalargumentexception
© www.soinside.com 2019 - 2024. All rights reserved.