如何读取同一包内的文件 [英] How to read in file that is inside the same package

查看:235
本文介绍了如何读取同一包内的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些代码,要求用户输入一个int值,然后将其传递给我的第一个名为parity()的方法。然后,Parity()将告诉用户它是奇数还是偶数。在该方法完成之后,我希望我的主程序打开与我的程序在同一个程序包内的文件,但是我的异常一直在设置终止我的程序,输出找不到文件或无法打开我觉得这样是一个简单的修复,但我正在尝试的大部分内容并没有改变它。这是我到目前为止的代码:

I have created some code that will ask a user to enter in a int value that will then be passed to my first method called parity(). Parity() will then tell the user if it is an odd or even int. After that method finished i want my main program to open my file that is within the same package as my program but my exception keeps getting set off terminating my program with the output "File was not found or could not be opened" I feel like this is a simple fix but much of what i am trying isn't changing it for the better. Here is my code so far:

package davi0030;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Assignment01 {

        static void parity(int value){
            int finalValue = value % 2;
            if (finalValue == 0){
                System.out.println(value + " is an even int!");
                }
            else{
                System.out.println(value + " is an odd int!");
            }

        }

        static void findPair(int value2, Scanner inputStream){
            int total;
            int n1 = inputStream.nextInt();
            while (inputStream.hasNextLine()){
                total = 0;
                int n2 = inputStream.nextInt();
                total = n1 + n2;
                if (total == value2){
                    System.out.println("a pair is found: " +n1 + " and " +value2+ " add to " + total);
                    System.exit(0);
                }
                System.out.println("no pair in the file adds to" + total);

            }

        }

        public static void main(String[] args) {

            int value1;
            int value2;
            Scanner inputStream = null;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a positive integer, 0 to end:");
            value1 = keyboard.nextInt();
            while (value1 != 0){
                parity(value1);
                System.out.println("Enter a positive integer, 0 to end:");
                value1 = keyboard.nextInt();
            }
            if (value1 == 0){
                System.out.println("Congratulations, you passed the first test");
                try{
                    inputStream = new Scanner(new FileInputStream("numbers.txt"));
                }
                catch (FileNotFoundException e)
                {
                    System.out.println("File was not found or could not be opened");
                    System.exit(0);
                }
                System.out.println("file opened successfully");
                System.out.println("Enter a positive integer");
                value2 = keyboard.nextInt();
                findPair(value2, inputStream);
            }
            keyboard.close();
            System.exit(0);


        }
    }


推荐答案

我不确定我是否理解这个问题,但是我可以告诉你,你可以阅读同一个包装中的文件:

I'm not sure I understood the question right, however I can tell you that you can read a file which is in your same package with:

BufferedReader reader = new BufferedReader(new InputStreamReader(Assignment01.class.getResourceAsStream("numbers.txt"), "UTF-8"));

这篇关于如何读取同一包内的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆