java 依赖默认编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29943128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:09:27  来源:igfitidea点击:

Reliance on default encoding

javafindbugs

提问by Reem

I'm using FindBugs and this error keeps generating:

我正在使用 FindBugs 并且此错误不断生成:

Reliance on default encoding:

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms.Use an alternative API and specify a charset name or Charset object explicitly.

依赖默认编码:

找到了一个方法调用,该方法将执行字节到字符串(或字符串到字节)的转换,并假定默认平台编码是合适的。这将导致应用程序行为因平台而异。使用替代 API 并明确指定字符集名称或字符集对象。

I think it has something to do with the Scanner here is my code:

我认为这与扫描仪有关,这是我的代码:

package mystack;

 import java.util.*;

  public class MyStack {

    private int maxSize;
    private int[] stackArray;
    private int top;

    public MyStack(int s) {
       maxSize = s;
       stackArray = new int[maxSize];
       top = -1;
    }
    public void push(int j) {
       stackArray[++top] = j;
    }
    public int pop() {
       return stackArray[top--];
    }
    public int peek() {
       return stackArray[top];
    }
    public int min() {
       return stackArray[0];
    }
    public boolean isEmpty() {
       return (top == -1);
    }
    public boolean isFull() {
       return (top == maxSize - 1);
    }


     public static void main(String[] args) {

       Scanner read = new Scanner(System.in);

         char end;

         System.out.println("Please enter the size of the Stack: ");
            int size=read.nextInt();

            MyStack stack = new MyStack(size);

         do{

            if(stack.isEmpty()){
              System.out.println("Please fill the Stack: (PUSH) \nBecause Stack is Empty.");
                 int add;
                 for(int i=0; i<size; i++)
                 {add=read.nextInt();
                   stack.push(add);}
                      }//End of if

          else if(stack.isFull()){
              System.out.println("Do you want to 1)POP 2)Know the Peek 3)Know the Min");
               int option=read.nextInt();
                if(option==1) 
                 stack.pop();

                else if (option==2)
                 System.out.println("The Peek= "+stack.peek());

                else if (option==3)
                 System.out.println("The Min= "+stack.min());

                else System.out.println("Error, Choose 1 or 2 or 3");
                      }//End of if
         else 
            { System.out.println("Do you want to 1)POP 2)Know the Peek 3)Know the Min 4)PUSH");
               int option=read.nextInt();

                 if(option==1)
                     stack.pop();

                 else if (option==2)
                  System.out.println("The Peek= "+stack.peek());

                 else if (option==3)
                  System.out.println("The Min= "+stack.min());

                 else if(option==4)
                   {int add=read.nextInt();
                    stack.push(add);}
                   }//end else

         System.out.print("Stack= ");
          for(int i=0; i<=stack.top; i++)
                 { System.out.print(stack.stackArray[i]+" ");}

         System.out.println();
         System.out.println();

         System.out.println("Repeat? (e=exit)");
          end=read.next().charAt(0);

         System.out.println();
         }while(end!='e');   
    System.out.println("End Of Program");
    }//end main

    }//end MyStack

It is a stack obviously, works fine.

它显然是一个堆栈,工作正常。

回答by tucuxi

FindBugs is worried about default character encodings. If you are under Windows, your default character encoding is probably "ISO-8859-1". If you are under Linux, it is probably "UTF-8". And if you are under MacOS, you may be using "MacRoman". You may want to read more on charset encodingsand find out more on available encodings in Javaby following clicking on the links.

FindBugs 担心默认字符编码。如果您使用的是 Windows,您的默认字符编码可能是“ISO-8859-1”。如果你在 Linux 下,它可能是“UTF-8”。如果您使用的是 MacOS,您可能正在使用“MacRoman”。您可能希望阅读有关字符集编码的更多信息,并通过单击链接了解有关Java 中可用编码的更多信息

In particular, this line uses the default platform encoding for reading in text from the console:

特别是,这一行使用默认平台编码从控制台读取文本:

   Scanner read = new Scanner(System.in);

To make sure that the code works the same in different environments, FindBugs suggests that you change this to

为了确保代码在不同的环境中工作相同,FindBugs 建议您将其更改为

   Scanner read = new Scanner(System.in, "UTF-8");

(or your favorite encoding). This would guarantee that, given an input file that uses encoding "UTF-8", it will be parsed in the same way regardless of what machine you are executing your program in.

(或您最喜欢的编码)。这将保证,给定使用编码“UTF-8”的输入文件,无论您在哪台机器上执行程序,它都会以相同的方式解析。

In your case, you can safely ignore this warning, unless you are interested in feeding text files into your application.

在您的情况下,您可以放心地忽略此警告,除非您有兴趣将文本文件输入到您的应用程序中。

回答by mies

FindBugs detects that your Scanner object uses default encoding to parse InputStream. Generally it is preferable to set encoding explicitly like new Scanner(someInputStreamFromFile, "UTF-8")to have the same parsing results in different environments. But not in your case, because you read System.io and there is no reliable way to detect console encoding. See details here: java console charset translation.

FindBugs 检测到您的 Scanner 对象使用默认编码来解析 InputStream。通常最好显式设置编码,new Scanner(someInputStreamFromFile, "UTF-8")以便在不同环境中具有相同的解析结果。但不是在你的情况下,因为你阅读 System.io 并且没有可靠的方法来检测控制台编码。在此处查看详细信息:java 控制台字符集翻译

回答by Michal Kordas

As you read from console, you can ignore this warning:

当您从控制台阅读时,您可以忽略此警告

@SuppressFBWarnings(
        value = "DM_DEFAULT_ENCODING",
        justification = "It's fine for console reads to rely on default encoding")

This suppression should be concious though. There might be cases when we use e.g. UTF-8 console in Windows, and then we shouldn't rely on the default encoding in new Scanner(System.in);.

不过,这种压制应该是有意识的。在某些情况下,我们可能会在 Windows 中使用例如 UTF-8 控制台,然后我们不应该依赖new Scanner(System.in);.