Java - 方法 "printDuplicates"中的 SecurityException

标签 java securityexception

我是 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)

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

最佳答案

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

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

关于Java - 方法 "printDuplicates"中的 SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34281189/

相关文章:

java - 按表从 ResultSet 中删除列

java - 如何在java中更好地编写开关代码(精确数字的平滑移动)

java - 线程 "main"java.lang.SecurityException : Invalid signature file digest for Manifest main attributes 中的异常

silverlight-4.0 - Silverlight 安全异常

java - 使用 Java 解析 CSV 时管理文件列名称和位置

java - 如何为没有主键或唯一键列的表创建 JPA 实体

java - jackson 不会下课

android-7.0-nougat - 无法在android 7中更改当前密码

silverlight-4.0 - Silverlight 无法与 HTTPS Web 服务对话?

Android 应用内结算 SecurityException "Binder invocation to an incorrect interface"