小编Jan*_*nek的帖子

从资源文件夹中获取 Json 文件

现在我正在学习Maven。我在从应用程序的资源文件夹中读取 json 文件时遇到问题。我收到一条错误消息“系统找不到此文件”。更有趣的是,当我尝试读取 txt 文件时没有问题......

正如您在下图中看到的,这两个文件位于我的应用程序中的同一位置。为什么我的 json 文件无法正确读取?

在此输入图像描述

        //WORKING
        String filename = "./resources/data/init_data.txt";
        try (Stream<String> lines = Files.lines(Paths.get(filename))){
            lines.forEach(System.out::println);
        } catch (Exception e){
            e.printStackTrace();
        }

        //NOT WORKING
        Gson gson = new Gson();
        filename = "./resources/data/car.json";
        try (Reader reader = new FileReader(filename)){
            Car car3 = gson.fromJson(reader,Car.class);
            System.out.println(car3);
        } catch (IOException e){
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

java resources json maven

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

对于循环,而不是集合中的循环

我对Collections.class和"copy"方法有疑问.

1)为什么我们在下面的代码条件下检查源列表的大小,以及为什么它必须小于10?为什么这么重要?

2)更重要的是,为什么我们在这个条件中使用for循环而在 - while (hasNext())

public static <T> void copy(List<? super T> dest, List<? extends T> src) {
    int srcSize = src.size();
    if (srcSize > dest.size()) {
        throw new IndexOutOfBoundsException("Source does not fit in dest");
    } else {
        if (srcSize < 10 || src instanceof RandomAccess && dest instanceof RandomAccess) {
            for (int i = 0; i < srcSize; ++i) {
                dest.set(i, src.get(i));
            }
        } else {
            ListIterator<? super T> di = dest.listIterator();
            ListIterator<? extends T> si = src.listIterator(); …
Run Code Online (Sandbox Code Playgroud)

java collections

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

使CSS代码更清晰(删除重复行)

如何更清楚地制作此代码.我的意思是从头开始删除这个重复的部分.我想我应该添加到HTML和CSS <li>但是当我将它粘贴到这个标签时我<span>根本不起作用......

CSS:

.box i.popular {
    width: 60px;
    height: 60px;
    display:inline-block;
    vertical-align:text-bottom;
    margin-top: 10px;
    margin-left: 1px;
    background: url('../images/popular.png') no-repeat
}

.box i.upload {
    width: 60px;
    height: 60px;
    display:inline-block;
    vertical-align:text-bottom;
    margin-top: 10px;
    margin-left: 1px;
    background: url('../images/upload.png') no-repeat;
}

.box i.diary {
    width: 60px;
    height: 60px;
    display:inline-block;
    vertical-align:text-bottom;
    margin-top: 10px;
    margin-left: 1px;
    background: url('../images/diary.png') no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

    <a href="#"><div id="test"><div class="box"><p><i class="popular"></i><span>Inspiration</span></p></div></div></a>
    <a href="#"><div id="test"><div class="box"><p><i class="upload"></i><span>Upload photo</span></p></div></div></a>
    <a href="#"><div id="test"><div class="box"><p><i class="diary"></i><span>Go to diary</span></p></div></div></a>
Run Code Online (Sandbox Code Playgroud)

提前致谢!

html css

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

标签 统计

java ×2

collections ×1

css ×1

html ×1

json ×1

maven ×1

resources ×1