Java - 方法中的 SecurityException "printDuplicates"

Java - SecurityException in Method "printDuplicates"

我是 Java 的新手,正在尝试提交工作项目,在本例中为 printDuplicates。说明如下:

Write a method named printDuplicates that accepts as its parameter a Scanner for an input file containing a series of lines. Your method should examine each line looking for consecutive occurrences of the same token on the same line and print each duplicated token along how many times it appears consecutively. Non-repeated tokens are not printed. Repetition across multiple lines (such as if a line ends with a given token and the next line starts with the same token) is not considered in this problem.

For example, if the input file contains the following text:

hello how how are you you you you I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge bow wow wow yippee yippee yo yippee yippee yay yay yay one fish two fish red fish blue fish It's the Muppet Show, wakka wakka wakka Your method would produce the following output for the preceding input file:

how*2 you*4 I*3 Jack's*2 smirking*5 wow*2 yippee*2 yippee*2 yay*3

wakka*3 Your code prints only the repeated tokens; the ones that only appear once in a row are not shown. Your code should place a single space between each reported duplicate token and should respect the line breaks in the original file. This is why a blank line appears in the expected output, corresponding to the fourth line of the file that did not contain any consecutively duplicated tokens. You may assume that each line of the file contains at least 1 token of input.

这是我的代码,可以提交了。

import java.io.*;
import java.util.*;
Scanner input;
public static void printDuplicates(Scanner input) throws Exception {
    String word = "";
    String word2 = "";
    input = new Scanner(new File("idontknowwhattodo.txt"));
    while(input.hasNextLine()) {
        Scanner line = new Scanner(input.nextLine());
        int repeat = 1;
        word = line.next();
        while(line.hasNext()) {
word2 = line.next();
while(word.equals(word2)) {
    repeat++;
    if(line.hasNext()){
        word2 = line.next();
    } else {
        break;
    }
}
if(repeat!=1) {
    System.out.print(word + "*" + repeat + " ");
}
repeat = 1;
word = word2;
}
        System.out.println();
    }
}

但是,每当我尝试提交我的项目时,它都会返回此错误:

(no output was produced!)
SecurityException on line 5:
You are not allowed to read the file       /usr/share/tomcat7/temp/idontknowwhattodo.txt

java.lang.SecurityException: You are not allowed to read the file   /usr/share/tomcat7/temp/idontknowwhattodo.txt
at java.io.FileInputStream.<init>(FileInputStream.java:135)
at java.io.FileReader.<init>(FileReader.java:72)
at Scanner.<init>(Scanner.java:330)
at printDuplicates (Line 5)

这是什么意思?我有多个工作项目,但由于这个错误,我似乎无法提交它们。任何专家都可以帮助我解决这个问题吗?谢谢。

按照说明,您已经将 Scanner 对象(引用输入文件)作为方法的参数。所以,你不应该重新初始化它。 应删除此行:

 input = new Scanner(new File("idontknowwhattodo.txt"));

问题描述说您正在获取 Scanner 对象作为参数。您不必重新创建它,您可能正在尝试将您的项目提交给一些在线竞赛。服务器上的程序将加载您的 class 并以 Scanner 对象作为参数调用方法 printDuplicates(),您不必担心它是如何创建的。好好用吧,万事大吉

只需如下注释扫描仪分配行

String word = "";
String word2 = "";
/*input = new Scanner(new File("idontknowwhattodo.txt"));*/
 while(input.hasNextLine()) {
 ...

您似乎在使用路径中的 Tomcat。 Tomcat 需要特殊的安全权限才能读取或写入文件。这是防止恶意代码访问 OS 上的敏感文件的基本保护。您可以配置这些目录或坚持读写默认目录:

https://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html

由于信誉点无法添加评论,因此请使用“答案”部分。

同意楼上的意见,与权限有关

  1. 在 /usr/share/tomcat7/temp/idontknowwhattodo.txt
  2. 上执行 ls -ltr
  3. 检查您 运行 您 java 应用程序的用户(例如 myuser)是否具有 /usr/share/tomcat7/temp/idontknowwhattodo.txt.
  4. 的必要权限
  5. 以下两个选项:
  6. 使用 chmod 授予用户 "myuser" 对 idontknowwhattodo.txt 的必要权限。
  7. 或将idontknowwhattodo.txt复制到"myuser"有权限的位置。