小编Nik*_*las的帖子

依赖于默认编码,我应该使用什么以及为什么?

FindBugs报告了一个错误:

依赖于默认编码找到对将执行字节到String(或String to byte)转换的方法的调用,并假设默认平台编码是合适的.这将导致应用程序行为在平台之间变化.使用备用API并显式指定charset名称或Charset对象.

我像这样使用FileReader(只是一段代码):

public ArrayList<String> getValuesFromFile(File file){
    String line;
    StringTokenizer token;
    ArrayList<String> list = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        list = new ArrayList<String>();
        while ((line = br.readLine())!=null){
            token = new StringTokenizer(line);
            token.nextToken();
            list.add(token.nextToken());
    ...
Run Code Online (Sandbox Code Playgroud)

要纠正我需要改变的错误

br = new BufferedReader(new FileReader(file));
Run Code Online (Sandbox Code Playgroud)

br = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.defaultCharset()));
Run Code Online (Sandbox Code Playgroud)

当我使用PrintWriter时,发生了同样的错误.所以现在我有一个问题.当我可以(应该)使用FileReader和PrintWriter时,如果不是很好的做法依赖于默认编码?第二个问题是正确使用Charset.defaultCharset()?我决定使用这种方法自动定义用户操作系统的字符集.

java encoding findbugs internationalization

23
推荐指数
2
解决办法
3万
查看次数

如何从java 8中的LocalTime中删除毫秒

使用java.time框架,我想以格式打印时间hh:mm:ss,但是以格式LocalTime.now()给出时间hh:mm:ss,nnn.我试着用DateTimeFormatter:

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime time = LocalTime.now();
String f = formatter.format(time);
System.out.println(f);
Run Code Online (Sandbox Code Playgroud)

结果:

22:53:51.894
Run Code Online (Sandbox Code Playgroud)

如何从时间中删除毫秒?

java rounding localtime datetime-format java-time

14
推荐指数
4
解决办法
2万
查看次数

Java Junit测试HTTP POST请求

我需要测试以下方法而不改变方法本身.该方法向服务器发出POST方法.但我需要创建一个独立于服务器的测试用例.

我在将其重定向到本地文件之前测试了类似的方法.但那是因为我把协议作为文件,主机名作为localhost,端口作为-1.

我的问题是这个方法做了一个帖子并转换为HttpURLConnection和wr = new DataOutputStream(conn.getOutputStream()); 不会通过http在本地txt文件上工作.

//构造函数

public HTTPConnector(String usr, String pwd, String protocol,
            String hostname, int port) {
        this.usr = usr;
        this.pwd = pwd;
        this.protocol = protocol;
        this.hostname = hostname;
        this.port = port;

        // connect();
    }
Run Code Online (Sandbox Code Playgroud)

//我需要测试的方法

public String doPost(String reference, String data) throws IOException {
        URL url = null;
        HttpURLConnection conn = null;
        BufferedReader rd = null;
        DataOutputStream wr = null;
        InputStream is = null;
        String line = null;
        StringBuffer response = null;

        url = new URL(protocol, …
Run Code Online (Sandbox Code Playgroud)

java testing post junit http

12
推荐指数
2
解决办法
3万
查看次数

AnnotationConfigApplicationContext尚未刷新 - 出了什么问题?

我的基本春季应用程序停止工作,我无法理解发生了什么. pom.xml中:

<properties>
    <spring.version>4.1.1.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

配置类:

@Configuration
public class MyConfig {

@Bean
public HelloWorld helloWorld() {
         return new HelloWorld();
    }
}
Run Code Online (Sandbox Code Playgroud)

Bean类:

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
         return message;
    }
}
Run Code Online (Sandbox Code Playgroud)

申请入境点:

public class MainApp {
public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(MyConfig.class);
    HelloWorld bean = ctx.getBean(HelloWorld.class);
    bean.setMessage("ladjfaj"); …
Run Code Online (Sandbox Code Playgroud)

java configuration spring

11
推荐指数
2
解决办法
2万
查看次数

处于分离头状态时如何保存更改?

与Git合作,我不得不回到特定的提交.我做了一些更改,现在我想提交它们.这样做的正确方法是什么?

我的项目现在处于分离-HEAD状态.如果我提交,我的更改是否会被保存

git commit
Run Code Online (Sandbox Code Playgroud)

?否则,我该怎么做才不会丢失我的更改?

git git-commit git-detached-head

10
推荐指数
3
解决办法
5539
查看次数

如何测试Guice AbstractModule的实现?

如何在一个大项目中测试Guice AbstractModule的实现而不创建虚假实现?是否可以测试bind()和inject()方法?

configuration unit-testing bind inject guice

9
推荐指数
1
解决办法
5966
查看次数

spring警告:不支持请求方法'HEAD'

在tomcat 7上成功部署后,我每秒都会收到警告消息:

org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported警告:不支持请求方法'HEAD'

应用程序工作.如何避免这个恼人的消息?

java spring spring-mvc tomcat7

7
推荐指数
2
解决办法
6213
查看次数

如何撤消 git stash apply

我还没有在我的分支中提交更改。我决定应用我的一些藏匿处。Stash 应用了自动合并和冲突。我意识到,隐藏的更改不适合我,并且想要取消隐藏更改,但不要在存储之前松开我的更改。试图做

git stash show -p | git apply -R
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用。我有错误消息:错误:补丁失败,...错误:补丁不适用

如何撤消存储应用而不丢失我未提交的更改?

git

7
推荐指数
1
解决办法
5740
查看次数

无法开始使用泽西用户指南

请帮帮我.我试了很长时间才开始休息应用程序示例,但我不能这样做.使用泽西用户指南我会被它困住.这是例子:

package com.example;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.glassfish.grizzly.http.server.HttpServer;

...

public class MyResourceTest {

    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        server = Main.startServer();

        Client c = ClientBuilder.newClient();
        target = c.target(Main.BASE_URI);
    }

    @After
    public void tearDown() throws Exception {
        server.stop();
    }

    /**
     * Test to see that the message "Got it!" is sent in the response.
     */
    @Test
    public void testGetIt() {
        String responseMsg = target.path("myresource").request().get(String.class);
        assertEquals("Got it!", responseMsg);
    } …
Run Code Online (Sandbox Code Playgroud)

java rest jersey user-guide grizzly

6
推荐指数
1
解决办法
1763
查看次数

为什么Java中的Object类包含受保护的方法?

为什么在Object类中的Java包含受保护的方法,如clone()finalize(),如果使用或写所有的类继承对象的实例方法?

java oop inheritance protected object

3
推荐指数
2
解决办法
1717
查看次数