Java 无法解析为一种类型

huangapple go评论76阅读模式
英文:

Java Cannot be resolved as a type

问题

以下是您要翻译的部分:

Trying to follow tuto but seems not working for me. It's Maven tuto, you can go to 11min to see the original code:

[Tuto Link Maven ][1]

Please find below my code:

package org.example.demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.io.input.ClosedInputStream;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws IOException
    {
        System.out.println( "Hello World!" );
        
        Properties vProp = new Properties();
        InputStream vInputStream = null;
        try {
            vInputStream = App.class.getResourceAsStream("/info.properties");
            vProp.load(vInputStream);
            
        } finally {
            if (vInputStream != null) {
                vInputStream.close();
            }
        }
    
        System.out.println("Application version :"+vProp.getProperty("org.example.demo.version+?"));
    }
}

<details>
<summary>英文:</summary>

Trying to follow tuto but seems not working for me. It&#39;s Maven tuto, you can go to 11min to see the orignal code:

[Tuto Link Maven ][1]


Please find below my code:

        package org.example.demo;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    import org.apache.commons.io.input.ClosedInputStream;
    
    /**
     * Hello world!
     *
     */
    public class App {
    	public static void main( String[] args ) throws IOException
        {
            System.out.println( &quot;Hello World!&quot; );
            
            Properties vProp = new Properties();
            InputStream vInputStream = null;
            try {
            	vInputStream = App.class.getResourceAsStream(&quot;/info.properties&quot;);
            	vProp load(vInputStream);
            	
            } finally {
    			if (vInputStream != null) {
    				vInputStream close();
    }}
    
    System.out.println(&quot;Application version :&quot;+vProp.getProperty(&quot;org.example.demo.version+&quot;+&quot;?&quot;));}
    
    }

[![enter image description here][2]][2]
[![enter image description here][3]][3]


  [1]: https://www.youtube.com/watch?v=EMiAFLQp8ow&amp;list=PLxhnp_qsD8EOkX0_RagnbUITXircj-XLC&amp;index=3
  [2]: https://i.stack.imgur.com/OHJXl.png
  [3]: https://i.stack.imgur.com/IowKL.jpg

</details>


# 答案1
**得分**: 1

`vProp.load(vInputStream)` 和 `vInputStream.close()`。

我还建议使用 try-with-resources 处理 vInputStream,无需调用 close 方法:

```java
try (InputStream vInputStream = App.class.getResourceAsStream("/info.properties")) {
    vProp.load(vInputStream);
}
英文:

You have missing dots there...
vProp.load(vInputStream) and vInputStream.close()

I would also suggest using try-with-resources for the vInputStream, no need for calling close then:

try (InputStream vInputStream = App.class.getResourceAsStream(&quot;/info.properties&quot;)) {
    vProp.load(vInputStream);
}

huangapple
  • 本文由 发表于 2020年8月8日 07:40:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63310412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定