我期待缓存的读取器和文件读取器关闭,如果异常抛出则释放资源.
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
{
return read(br);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,是否需要有catch
成功关闭的条款?
编辑:
从本质上讲,Java 7中的上述代码与Java 6中的代码相同:
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(filePath));
return read(br);
}
catch (Exception ex)
{
throw ex;
}
finally
{
try
{
if (br != null) br.close();
}
catch(Exception ex)
{
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 为了简化问题,我有一个图表,其中包含2D平面上的节点和边.
我想要做的是单击一个按钮,它会自动布局图形看起来干净.我的意思是边缘的最小交叉,节点之间的漂亮空间,甚至可能代表图形比例(加权边缘).
我知道这是一个看起来很干净的图表是完全主观的,但有没有人知道一个算法开始,而不是重新发明轮子?
谢谢.
基于阅读这个问题:SubscribeOn和ObserveOn之间有什么区别
ObserveOn
设置Subscribe
处理程序中代码的执行位置:
stream.Subscribe(_ => { // this code here });
该SubscribeOn
方法设置完成流的设置的线程.
我明白如果没有明确设置,那么使用TaskPool.
现在我的问题是,让我说我做这样的事情:
Observable.Interval(new Timespan(0, 0, 1)).Where(t => predicate(t)).SelectMany(t => lots_of(t)).ObserveOnDispatcher().Subscribe(t => some_action(t));
哪里有Where
predicate
和SelectMany
lots_of
执行,因为some_action
正在上调度执行?
我正在寻找一个等同于C#扩展方法功能的java.现在我一直在阅读Java 8的默认方法,但据我所知,我只能将它们添加到接口......
...是否有任何语言功能允许我为没有实现接口的最终类编写扩展方法?(我宁愿不用包装......)
试图src/main/resources/scripts/
从我的构建中驱逐一个文件夹,但以下内容不起作用:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我是Python的新手,这里是我正在看的一些代码:
try:
connection = getConnection(database)
cursor = connection.cursor()
cursor.execute("some query")
except:
log.error("Problem.")
raise
finally:
cursor.close()
connection.close()
Run Code Online (Sandbox Code Playgroud)
这是否正确清理?在我写的其他语言中,我习惯做这样的事情:
connection = None
cursor = None
try:
connection = getConnection(database)
cursor = connection.cursor()
cursor.execute("some query")
except:
log.error("Problem.")
raise
finally:
if cursor is not None:
cursor.close()
if connection is not None:
connection.close()
Run Code Online (Sandbox Code Playgroud) 我试图从JavaScript设置对象的边距.我可以在Opera和Firefox中执行此操作,但代码在Internet Explorer中不起作用.
这是我的JavaScript:
function SetTopMargin (ObjectID, Value)
{
document.getElementById(ObjectID).style.marginTop = Value.toString() + "px";
}
Run Code Online (Sandbox Code Playgroud)
它被称为这样:
SetTopMargin("test_div_id", 100);
Run Code Online (Sandbox Code Playgroud)
那么有谁知道一些可以在Internet Explorer中运行的代码?
当我调用mCamera = Camera.open()
它返回null时,可能是什么导致了这个?我的设备是Nexus 7.
我已经在我的权限中设置了权限AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
Run Code Online (Sandbox Code Playgroud) 我正在使用一些Google Web字体.我听说谷歌处理不同浏览器之间的所有问题,并根据请求标题中的浏览器提供不同的媒体.
我的问题是,它在什么时候这样做?
原因是,对于API,您只需包含一个包含@font-face
请求的CSS文件.我可以简单地将CSS包含在我自己的CSS文件中,从而保存HTTP请求,还是根据请求它的浏览器更改CSS?
我真的希望这是有道理的.
例如,Google建议您在CSS文件中包含以下内容:
@import url(http://fonts.googleapis.com/css?family=Exo);
其内容是:
@font-face {
font-family: 'Exo';
font-style: normal;
font-weight: 400;
src: local('Exo Regular'), local('Exo-Regular'), url('http://themes.googleusercontent.com/static/fonts/exo/v1/ZcGd2dvMSgl3mHN3lKAjNw.woff') format('woff');
}
Run Code Online (Sandbox Code Playgroud) 我正在使用bash.
如果已经设置了一个环境变量,我想要追加它:
PATH=$PATH":/path/to/bin"
或者,如果它还不存在,我想简单地设置它:
PATH="/path/to/bin"
这是否有一行声明?
显然PATH
环境变量几乎总是设置,但最简单的问题就是这样写.