Reliance on default encoding
我正在使用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.
我认为这与扫描仪有关,这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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) Because 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 |
显然是一个堆栈,工作正常。
FindBugs担心默认字符编码。如果您使用Windows,则默认字符编码可能是" ISO-8859-1"。如果您使用的是Linux,则可能是" UTF-8"。如果您使用的是MacOS,则可能使用的是" MacRoman"。您可能想要阅读更多有关字符集编码的信息,并通过单击链接来查找有关Java可用编码的更多信息。
特别是,此行使用默认平台编码从控制台读取文本:
1 |
为了确保代码在不同的环境下均能正常工作,FindBugs建议您将其更改为
1 |
(或您喜欢的编码)。这样可以保证,在给定使用" UTF-8"编码的输入文件的情况下,无论在什么计算机上执行程序,都将以相同的方式进行解析。
就您而言,除非您有兴趣将文本文件输入到应用程序中,否则可以安全地忽略此警告。
从控制台读取时,可以忽略此警告:
1 2 3 | @SuppressFBWarnings( value ="DM_DEFAULT_ENCODING", justification ="It's fine for console reads to rely on default encoding") |
这种压制应该是有意识的。在某些情况下,我们可能会使用Windows中的UTF-8控制台,那么我们不应该依赖
FindBugs检测到您的Scanner对象使用默认编码来解析InputStream。通常,最好像