java平台默认编码_关于Java:依赖默认编码

我正在使用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.

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

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

{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可用编码的更多信息。

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

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的英语版本。不同的语言环境显然具有不同的语言环境。)

从控制台读取时,可以忽略此警告:

@SuppressFBWarnings(

value ="DM_DEFAULT_ENCODING",

justification ="It's fine for console reads to rely on default encoding")

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值