【问题标题】:Reliance on default encoding依赖默认编码
【发布时间】:2015-07-08 16:52:41
【问题描述】:

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

依赖默认编码:

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

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

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

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

【问题讨论】:

    标签: java findbugs


    【解决方案1】:

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

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

       Scanner read = new Scanner(System.in);
    

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

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

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

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

    【讨论】:

    • 感谢您的帮助!!我试过 Scanner (System.in, "UTF-8") 成功了!!
    • 如果你在 Windows 下,你的默认字符编码可能实际上是 windows-1252。 (假设通常是英文版的 Windows。不同的语言环境显然有不同的语言环境。)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      当你从控制台读取时,你可以ignore this warning:

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

      这种压制应该是有意识的。在某些情况下,我们使用例如Windows 中的 UTF-8 控制台,那么我们不应该依赖 new Scanner(System.in); 中的默认编码。

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 2014-04-02
        • 1970-01-01
        相关资源
        最近更新 更多