我在网上和“Effective Java”(Joshua Bloch 着)一书中看到了这个例子。
try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
writer.write(str); // do something with the file we've opened
}
catch(IOException e){
// handle the exception
}
Run Code Online (Sandbox Code Playgroud)
这个例子没有问题,BufferedWriter它会自动关闭,然后关闭FileWriter; 但是,在其他情况下,如果我们以这种方式声明 2 个嵌套资源:
try (AutoClosable res = new Impl2(new Impl1())) {... }
Run Code Online (Sandbox Code Playgroud)
我想可能会发生new Impl1()性能良好但new Impl2()崩溃的情况,在这种情况下,Java 将没有引用Impl1, 以关闭它。
像这样始终独立声明多个资源(即使在这种情况下不需要)难道不是更好的做法吗?
try(FileWriter fw = new FileWriter(fileName);
BufferedWriter writer = new BufferedWriter(fw)){ ... }
Run Code Online (Sandbox Code Playgroud) 我已经读过,建议在Java 7中添加多行字符串文字.
虽然我找不到任何文件肯定说他们已经.我想知道它们是否是,因为这是我考虑切换版本的东西.
我正在使用Java 1.7,Eclipse 3.7和市场上的FindBugs插件.这个例子和天堂一样好:
class Application
{
public static void main( String[] args )
{
System.out.println( "Bla" );
}
}
Run Code Online (Sandbox Code Playgroud)
此消息在过去不存在,内部实现始终在系统中:
public final static PrintStream out = null;
Run Code Online (Sandbox Code Playgroud)
所以Findbugs是对的,但现在消息发生了什么改变?
Java 7已经存在了一段时间了.现在,如果要将应用程序迁移到Java 7而不进行任何更改(代码/配置),是否存在任何固有的优点或缺点?我很想知道这种迁移过程中遇到的问题.
编辑:通过迁移我的意思是代码将保持不变但运行时将更改为Java 7因为我提到没有代码/配置更改,所以我认为应该影响应用程序的是新的编译器/ VM级别默认优化.所以我一直在寻找任何会影响整体应用程序行为的东西.
大家好,我在创建JAXB解析器时遇到了一个非常奇怪的问题.当我试图从eclipse生成JAXB类时,在一个类中它显示了一个非常奇怪的错误
Access restriction: The type QName is not accessible due to restriction on required library /usr/lib/jvm/jdk1.7.0_02/jre/lib/rt.jar
Run Code Online (Sandbox Code Playgroud)
这是我的班级
package generated;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
//import javax.xml.namespace.QName;
import javax.xml.namespace.*;
@XmlRegistry
public class ObjectFactory {
Run Code Online (Sandbox Code Playgroud)
在这条线上,我正在收到错误消息
private final static QName _ExpenseReport_QNAME = new QName("","expenseReport");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link ExpenseT }
* …Run Code Online (Sandbox Code Playgroud) 我Files.WalkFileTree()用来导航文件夹和计算音频文件,但遇到tar文件时出现问题,它似乎将它视为一个实际的文件夹我期望它只是跳过它.
我看不到任何让我控制这种行为的选项
码:
package com.jthink.songkong.fileloader;
import com.jthink.songkong.cmdline.SongKong;
import com.jthink.songkong.ui.MainWindow;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.Callable;
import java.util.logging.Level;
/**
* Count the number of files that can be loaded, for information purposes only
*/
public class CountFilesinFolder implements Callable<Boolean> {
public static class CountFiles
extends SimpleFileVisitor<Path> {
private int fileCount = 0;
private final PathMatcher matcher;
CountFiles(String pattern) {
matcher =
FileSystems.getDefault()
.getPathMatcher("regex:" + pattern);
}
/**
* Find Music file
*
* @param file
* @param attr
* …Run Code Online (Sandbox Code Playgroud) 我有Java 8 SDK和运行时,以及只使用Java 7功能的项目.我想知道我是否可以设置intellij来构建一个Java-7级jar,这绝对可以从命令行获得?
到目前为止,我尝试将项目语言级别设置为7作为实验,但这显然是不够的,而不是我真正需要的.
我也在SO上看到了这个问题:" 当我将Java7编译器配置为使用Java6时,使用java7编译器编译Intellij IDEA ",但我的项目SDK已经设置为最新的Java版本,我只需要一个Java-7-兼容的工件/ jar编译.
编辑:我在本地系统上只有Java 6和Java 8 JRE/SDK,因为遗憾的是Java 7在我的系统上存在问题并且不兼容.
我正在从Java 7迁移到Java 8,我已经遇到了这种语言的变化.
我有一个带有注释方法的Superinterface:
public interface SuperInterface {
@X
SuperInterface getSomething();
}
Run Code Online (Sandbox Code Playgroud)
我有一个SubInterface与相同的注释方法,但返回一个子接口:
public interface SubInterface extends SuperInterface {
@X
SubInterface getSomething();
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,它在Java 8中失败但在Java 7中失败:
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
final Method[] methods = SubInterface.class.getMethods();
for (Method method : methods) {
if (method.getAnnotations().length == 0) {
throw new RuntimeException("No annotations found for " + method);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
接口方法的注释在Java 7中继承,但在Java 8中没有,是真的吗?
@X定义为:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) …Run Code Online (Sandbox Code Playgroud)