小编Aar*_*ron的帖子

尝试/多次捕捉与单次捕捉

在Eclipse中添加try/catch块时,它给了我"Surround with try/multi-catch"或"Surround with try/catch"的选项.

这是try/multi-catch:

try {
    save.load(new FileInputStream(file.getAbsolutePath()));
}
catch (FileNotFoundException | IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这是单个try/catch:

try {
    save.load(new FileInputStream(file.getAbsolutePath()));
}
catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

使用其中一个或哪个有什么好处/后果?如果我是正确的,第一个示例将在抛出异常的ECEER并生成SAME CATCH时执行catch块,而第二个示例将在启用单独的catch块时基于异常抛出catch.

还有什么我应该知道的吗?我以前从未使用它们,也不知道它们是否值得使用.

java try-catch

10
推荐指数
1
解决办法
1万
查看次数

Vector.remove()for循环

我正在编写游戏,几乎完成了保存文件系统.我有两个Vector(一个包含savegame 的名称,一个包含sessionID).

在启动时,程序将从文件中读取数据并将该信息添加到向量中.然后调用另一个方法来检查Vector中显示的文件是否存在.如果没有,它们将从矢量中删除.最后,Vector被打印到并重写文件.

我遇到的问题是for循环不检查Vector中的每个项目,因为当项目被删除时Vector.size()正在减少.是否有更好的方法来形成for循环,或者是否有解决方法我可以使用?

private static void slistCleanup() throws IOException {

          private static Vector<String> saveNames = new Vector<String>();
          private static Vector<Integer> sessionIDs = new Vector<Integer>();

    Scanner slistReader = new Scanner(new FileReader(sessionList));
    File tempSave;
    String path;
    int run = 1;
    String tempName = " ";
    int tempID = 0;

    for (int x = 0; x < saveNames.size(); x++) {

        path = currentDir + "\\saves\\" + sessionIDs.elementAt(x) + ".sav";
        tempSave = new File(path);

        System.out.println("-----------------------"); //debug
        System.out.println("current …
Run Code Online (Sandbox Code Playgroud)

java for-loop file vector

3
推荐指数
1
解决办法
4070
查看次数

简化条件运算符

我的朋友在他的编程类中编写了这个代码:

public class test {
   public static void main(String args[]) {
      double x = 0.9;
      double y = 0.1;
      boolean truth = x < 1 && x > 0 && y < 1 && y > 0;
      System.out.println(truth);
   }
}
Run Code Online (Sandbox Code Playgroud)

我想(对我自己)是否有办法简化这一行中的条件运算符:

boolean truth = x < 1 && x > 0 && y < 1 && y > 0;
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
1
解决办法
130
查看次数

属性文件未按正确顺序存储

我在一个属性文件中保存了几个值,但是每当我打开文件时,这些值都没有按照我编码的顺序写入。

save.setProperty("SN", "foo");
save.setProperty("ID", "bar");
save.setProperty("TN", "example");
save.setProperty("TC", "generic");
save.setProperty("SW", "incorrect");
save.setProperty("NW", "order");
save.store(new FileOutputStream(file.getAbsolutePath(), null);
Run Code Online (Sandbox Code Playgroud)

这是我打开文件时的样子:

#Sat Jul 13 19:28:59 EDT 2013
ID=bar
SW=incorrect
TC=generic
TN=example
NW=order
SN=foo
Run Code Online (Sandbox Code Playgroud)

如您所见,订单完全脱离了编码。我很好奇为什么会发生这种情况。它显然不是按字母顺序排序的,我看不出有任何其他原因为什么它会像这样发生。

java properties

2
推荐指数
1
解决办法
516
查看次数

Jersey 2.0客户端的文件上载失败

我正在尝试使用Jersey 2.0为Dropbox实现REST Client.

为了上传文件,我使用以下代码:

WebTarget target = client.target(targetUrl);
final FileDataBodyPart filePart = new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
final MultiPart multipart = new FormDataMultiPart().bodyPart(filePart);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE).put(Entity.entity(multipart, multipart.getMediaType()));
Run Code Online (Sandbox Code Playgroud)

但是,此代码不起作用并MessageBodyProviderNotFoundException发生在该put方法中.

我怎样才能避免MessageBodyProviderNotFoundExceptionput方法中.

发生异常时,这是堆栈跟踪:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:287)
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:200)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
    at …
Run Code Online (Sandbox Code Playgroud)

java rest file-upload jax-rs jersey

2
推荐指数
1
解决办法
5747
查看次数

为什么在IOException覆盖时使用FileNotFoundException

当IOException覆盖FileNotFoundException时捕获FileNotFound和IOException的目的是什么?

例子:

try {
    pref.load(new FileInputStream(file.getAbsolutePath()));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

而不是:

try {
    pref.load(new FileInputStream(file.getAbsolutePath()));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

是否只是在抛出FileNotFoundException时启用不同的代码?或者有不同的原因吗?

编辑:可以抛出什么IOException的几个例子?(除了FileNotFoundException)

java try-catch filenotfoundexception ioexception

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

Python Dictionary获取Key的值

有没有一种方法来获得关键的价值,而不是键值在字典?例如:

d = {"testkey": "testvalue"}
print(d.get("testkey"))
#OUTPUT: "testvalue"
Run Code Online (Sandbox Code Playgroud)

有没有办法获得字符串"testkey"?我无法知道String返回的内容到底是什么.使用列表而不是字典会更有益吗?

python dictionary

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

HTML 动态更新滑块输入

我以前从未使用过 HTML,现在我正在创建一个简单的网站作为学术项目的一部分。我正在使用typed.js并让用户选择输出的速度。目前,我正在使用一个下拉框,其中包含与 javascript 代码中使用的值相关的“慢/中/快”选项,但我一直在尝试实现范围输入。我希望滑块的标签能够在滑块更改时自动更新所选数字,但我不知道如何实现这一点。任何帮助表示赞赏!

html javascript

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

Python Random.Choice除了一个选项

我试图用来random.choice()从字典中选择一个项目,但是,我希望其中一个项目完全被忽略.例如:

mutationMarkers = {0: "original", 1: "point_mutation", 2: "frameshift_insertion", 
                   3: "frameshift_deletion", 4: "copy_number_addition", 
                   5: "copy_number_subtraction"}

mutator = choice(list(markers)) # output is 0, 1, 2, 3, 4, 5
Run Code Online (Sandbox Code Playgroud)

是否可以使用random.choice和忽略{0: "original"}

python random dictionary

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