小编Ste*_*n C的帖子

Windows上的Java:防止文件名中的"/"斜杠充当分隔符

我必须根据提供给我的字符串创建一个文件.对于此示例,假设文件名为"My file w/stuff.txt".当Java使用创建文件时

 File file = new File("My file w/ stuff.txt")
Run Code Online (Sandbox Code Playgroud)

即使默认的Windows分隔符是'\',它也假定'/'斜杠是文件分隔符.所以未来的电话file.getName()会回来" stuff.txt".这会导致我的程序出现问题.

有什么方法可以防止这种行为吗?

java windows file slash

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

关于断言启用

谁可以告诉我如何在日食Helios中启用断言?我正在使用java 6

java eclipse assert

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

Collections.copy问题

我想要它b1b2拥有自己的元素集,那么b1和b2应该在内存中有自己的元素,这样当b1/b2被修改时,其他元素不应该受到影响.

buffer是一个ArrayList包含许多元素

List<Integer>  b1 = new ArrayList<Integer>(buffer.size()) ;
List<Integer>  b2 = new ArrayList<Integer>(buffer.size()) ) ;
Collections.copy(b1, buffer);
Collections.copy(b2, buffer);
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

Exception in thread "main"
java.lang.IndexOutOfBoundsException: Source does not fit in dest
    at java.util.Collections.copy(Collections.java:531)
    at Trees.containsSumPrint(Trees.java:243)
    at Trees.main(Trees.java:125)

java collections

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

即使一个spring bean初始化失败,也继续加载webapp

因此,如果在webapp中弹出初始化失败,那么webapp本身就不会出现.为了防止这种情况,我可能不会从我的代码中重新抛出任何异常的bean初始化,并且webapp将继续加载,对吧?

有没有其他方法可以告诉spring在特定的bean初始化失败时不要让webapp本身失败?

java spring

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

Executor服务重启

我可以ExecutorService在关闭后重新启动以开始接受新任务吗?

或者我必须始终创建新ExecutorService实例?

java android executorservice

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

Java ArrayList:contains()方法在arraylist包含给定对象时返回false

我有一个contains()方法的问题,即使ArrayList包含给定的方法返回false Object.我的代码如下:

String [] transaction = dbConnection.getPendingTransaction(username);
if (!onlineConnection.getSentTransactionRequests().contains(transaction)) {
    onlineConnection.getSentTransactionRequests().add(transaction);

    String packet = "RTR" + "_" + transaction[0] + "_" + transaction[2] + 
                        "_" + transaction[3] + "_" + transaction[4];
    onlineConnection.send(packet);
}
Run Code Online (Sandbox Code Playgroud)

Thread.sleep()在迭代之间尝试过,所以ArrayList没有成功就没有加载.

java arrays evaluation contains arraylist

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

复杂的代码和分支预测器

分支预测逻辑有多"粘性"?如果从指令缓存中删除代码,统计数据是否保持不变?

换句话说,如果代码很复杂或者没有批量处理,那么分支预测是否会有所帮助?

让我们假设商品英特尔服务器硬件比2011年更新.

intel branch-prediction

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

项目无法识别黄瓜-picocontainer依赖性

我目前正在使用Cucumber,JUnit和Selenium开发Java测试框架。我已经从事过类似的项目,但是在这个项目上遇到了问题。

我正在尝试创建一个Singleton的Context类。我想使用Cucumber-picocontainer在每个步骤定义类中都可以访问此类。我在pom.xml中添加了依赖项,但是每次尝试执行测试时,都会有一个异常消息:“ NewLoginSteps没有空的构造函数。如果您需要DI,则在类路径上放置cumul-picocontainer” 。我尝试手动导入罐子,但没有帮助。

这是我的配置示例:

  • pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
    
        <properties>
            <cucumber.version>1.2.4</cucumber.version>
            <selenium-java.version>2.39.0</selenium-java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-picocontainer</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    </project>
    
    Run Code Online (Sandbox Code Playgroud)

TestContext.java:

public class TestContext {
    private static Map<String, String> siteLocations = new HashMap<String, String>();
    private static boolean initialized = false;
    private static boolean firstInitDone = false;
    private static WebDriver driver;
    private static boolean testsToRun = …
Run Code Online (Sandbox Code Playgroud)

java selenium cucumber picocontainer maven

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

systemd错误“无法启动服务:单元服务未正确加载:exec格式错误”

我可以ExecStart从Shell 执行确切的命令,并且可以运行,但是由于某些原因,在此服务文件中,该命令不起作用-有什么想法吗?

错误:

Failed to start previewapi.service: Unit previewapi.service is not loaded properly: Exec format error.
See system logs and 'systemctl status previewapi.service' for details.
Run Code Online (Sandbox Code Playgroud)

systemd .service文件:

[Unit]
Description = preview-api
After       = network.target

[Service]
WorkingDirectory=/srv/previewapi
ExecStart   = /usr/bin/java -jar /srv/previewapi/gn-preview-api-0.1.0-SNAPSHOT-standalone.jar

ExecStop    = kill -INT $MAINPID
ExecReload  = kill -TERM $MAINPID

# In case if it gets stopped, restart it immediately
Restart     = always

Type        = simple


[Install]
# multi-user.target corresponds to run level 3
# roughtly meaning …
Run Code Online (Sandbox Code Playgroud)

ubuntu systemd

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

为什么Java在这种情况下会将double类型转换为float类型呢?

今天,我定义了两个浮点变量f1f2。然后我执行加法“+”算术运算并分配给浮点变量f

\n
float f1 = 0.5048076923076923F;\nfloat f2 = 0.5048076923076923F;\nfloat f = f1 + f2;\n
Run Code Online (Sandbox Code Playgroud)\n

这是输出:
\n在此输入图像描述

\n

根据这张图,算术运算中的所有浮点值( floatdouble )( +, \xe2\x88\x92, *, /)都转换为double类型:\n图片来源:http://www.mathcs。 emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
\n图像

\n

我发现了一个相同的问题,但它没有解释原因。为什么eclipse没有任何问题提示?这就是“f1 + f2”的值为float类型的原因吗?而且,如果像上图所示,为什么 Java 会自动将 double 类型转换为 float 类型呢?\n图片来源:https://www.homeandlearn.co.uk/java/short_float_variables.html

\n

java eclipse

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