简体   繁体   English

Java不接受文件作为输入

[英]Java not accepting File as Input

I am trying to read in a file of integers that will be sorted and then output into a different file. 我正在尝试读取将被排序的整数文件,然后输出到另一个文件中。 It can do everything except take the input file in terminal. 它可以执行所有操作,除了将输入文件放在终端中。 I am not sure why, I have included the entire code and the commented portion is the part that doesn't seem to work (since when I hard code an array into the code it works fine). 我不知道为什么,我包括了整个代码,而注释部分是似乎无效的部分(因为当我将数组硬编码到代码中时,它可以正常工作)。 Thanks in advance. 提前致谢。

class Quicksort {
    public void qSort(int[] a, int p, int r) {
        if (p < r) {
            int q = Partition(a, p, r);
            qSort(a, p, q - 1);
            qSort(a, q + 1, r);
        }
    }

    private static int Partition(int[] A, int p, int r) {
        int x = A[p];
        System.out.println(x + " ");
        int i = p;
        int j = r;
        int temp = 0;

        while (true) {
            while (A[j] > x) {
                j--;
            }

            while (A[i] < x) {
                i++;
            }

            if (i < j) {
                temp = A[j];
                A[j] = A[i];
                A[i] = temp;
            } else {
                return j;
            }
        }

    }
}

public class QuickSortTest {
    public static void main(String[] args) throws IOException {
        long time1 = System.nanoTime();
        Quicksort qsort = new Quicksort();

        //          ArrayList<Integer> dataReadIn = new ArrayList();
        //          FileReader fileToBeRead = new FileReader("test.txt");
        //          Scanner src = new Scanner(fileToBeRead);
        //          src.useDelimiter("[,\\s*]");
        //          int p = 0;
        //               while (src.hasNext()) 
        //               {
        //                   if (src.hasNext()) 
        //                   {
        //                       dataReadIn.add(src.nextInt());
        //                       System.out.println(p);
        //                       p++;
        //                     } 
        //                     else {
        //                         break;
        //                     }
        //                 }
        //           
        //      int[] array = new int[dataReadIn.size()];
        //     for(int a = 0; a < array.length; a++)
        //     {
        //     array[a] = dataReadIn.get(a);
        //     }

        int[] array = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
                79, 80, 81, 82, 83, 26, 28 };
        int length = array.length;
        System.out.println(length);
        System.out.println("Pivot Element: ");
        qsort.qSort(array, 0, length - 1);

        PrintWriter out = new PrintWriter(new FileWriter("OutputArray"));
        for (int i = 0; i < array.length; i++) {
            out.println(array[i] + " ");
        }

        out.close();
        long time2 = System.nanoTime();
        long time = (time2 - time1) / 1000;
        System.out.println("Total time: " + time + "ms");
    }
}

The root cause is your file is not found by the code. 根本原因是代码找不到您的文件。

There are 2 solutions for your problem : 有两种解决方案来解决您的问题:

  1. Pass in the absolute path to the file when instantiating FileReader 实例化FileReader时将绝对路径传递给文件
  2. Make sure the file is in the classpath and use getClass().getResourceAsStream and pass that to instantiate the Scanner (you won't need the FileReader class) 确保文件在类路径中,并使用getClass()。getResourceAsStream并将其传递给实例化Scanner(您不需要FileReader类)

I'm pretty sure * doesn't work inside [] in a regex (by "doesn't work" I mean it's treated as the actual character, not 1-or-more) (assuming you meant 1-or-more white-space characters). 我很确定*不能在正则表达式的[]内使用(“不起作用”是指它被视为实际字符,而不是1或更多)(假设您是指1或更多的白色) -空格字符)。 You can put * outside: (matches one or more white-space characters or commas) 您可以将*放在外面:(匹配一个或多个空格字符或逗号)

src.useDelimiter("[,\\s]+");

or do something like this: (match either a comma or one or more white-space characters) 或执行以下操作:(匹配逗号或一个或多个空格字符)

src.useDelimiter("(,|\\s+)");

I'm not entirely sure either of the above will actually work, it may be simpler just to use: (one or more non-digit characters) 我不完全确定以上两种方法都能正常工作,只是使用起来可能会更简单:(一个或多个非数字字符)

src.useDelimiter("[^0-9]+");

Note I changed the * to a + , otherwise the scanner will match on length 0 strings and may return digit by digit rather than a whole number. 注意我将*更改为+ ,否则扫描程序将匹配长度为0的字符串,并且可能逐位而不是整数地返回。

Also, your while-loop is somewhat overcomplicated. 另外,您的while循环有些复杂。 This would be simpler: 这会更简单:

int p = 0;
while (src.hasNext()) 
{
  dataReadIn.add(src.nextInt());
  System.out.println(p++);
}

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

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