如何获取压缩文件(通过索引)并重新创建原始文件? (JAVA) [英] How do I take a compressed file (through indexes) and re-create the original file? (Java)

查看:145
本文介绍了如何获取压缩文件(通过索引)并重新创建原始文件? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题背景

我一直在开发一些代码,主要关注于读取字符串和创建文件。其次,将一个字符串拆分成一个数组。然后获取数组中每个单词的索引,最后删除重复项并将其打印到不同的文件中。
我目前已经在这里为此提供了一个链接 https://pastebin.com/gqWH0x0(也有一个菜单系统),但它很长,所以我没有在这个问题上实现它。



压缩方法通过hashmaps完成,获取数组的索引并将它们映射到相关单词。下面是一个例子:

原文:海上见海看见


输出:见[2 ,4,5],海[0,1,3],

问题

下一阶段是将输出恢复到原始状态。我目前对java比较陌生,所以我不知道所需的技术。代码应该能够将输出文件(如上所示)放回到原始文件中。



我目前的想法是,你只需要重写这个hashmap(下面)。我想这是否正确?我以为我应该先检查堆栈溢出!

 地图< String,Set< Integer>> seaMap = new HashMap<>(); // new hashmap 
for(int seaInt = 0; seaInt< sealist.length; seaInt ++){
if(seaMap.keySet()。contains(sealist [seaInt])){
设置<整数> index = seaMap.get(sealist [seaInt]);
index.add(seaInt);
} else {
Set< Integer> index = new HashSet<>();
index.add(seaInt);
seaMap.put(sealist [seaInt],index);
}
}
System.out.print(Compressed:); $(b)b seaMap.forEach((seawords,seavalues) - > System.out.print(seawords + seavalues +,));
System.out.println(\\\
);

如果有人有任何好的想法/答案,请让我知道,我非常渴望解决方案!



链接到当前代码: https://pastebin.com / gqWH0x0K

解决方案

首先,您必须使用压缩行中的index(es)你的例子:

$ p $ 见[2,4,5],sea [0,1,3],

可获得以下字符串:

 see [2,4,5]andsea [0,1,3]

你必须阅读索引,例如第一:

  2,4和5 

现在只需在指定索引的ArrayList(或数组)中写入单词。



在前两步中,您可以使用正则表达式来查找每个单词和索引列表。然后使用String.split和Integer.parseInt获取所有索引。

  Pattern pattern = Pattern.compile((。*? )\\ [(*)\\。?],); 
String line =参见[2,4,5],sea [0,1,3],;
Matcher matcher = pattern.matcher(line);
while(matcher.find()){
String word = matcher.group(1);
String [] indexes = matcher.group(2).split(,);
for(String str:indexes){
int index = Integer.parseInt(str);

现在只需检查结果列表是否足够大,并将字设置为找到的索引。

Background of question

I have been developing some code that focuses on firstly, reading a string and creating a file. Secondly, spliting a string into an array. Then getting the indexes for each word in the array and finally, removing the duplicates and printing it to a different file. I currently have made the code for this here is a link https://pastebin.com/gqWH0x0 (there is a menu system as well) but it is rather long so I have refrained from implementing it in this question.

The compression method is done via hashmaps, getting indexes of the array and mapping them to the relevant word. Here is an example:

Original: "sea sea see sea see see"

Output: see[2, 4, 5],sea[0, 1, 3],

Question

The next stage is getting the output back into the original state. I am currently relatively new to java so I am not aware of the techniques required. The code should be able to take the output file (shown above) and put it back into the original.

My current thinking is that you would just rewrite this hashmap (below). Would I be correct in thinking this? I thought I should check with stack overflow first!

Map<String, Set<Integer>> seaMap = new HashMap<>(); //new hashmap
                    for (int seaInt = 0; seaInt < sealist.length; seaInt++) {
                        if (seaMap.keySet().contains(sealist[seaInt])) {
                            Set<Integer> index = seaMap.get(sealist[seaInt]);
                            index.add(seaInt);
                        } else {
                            Set<Integer> index = new HashSet<>();
                            index.add(seaInt);
                            seaMap.put(sealist[seaInt], index);
                        }                
                    }
                    System.out.print("Compressed: ");
                    seaMap.forEach((seawords, seavalues) -> System.out.print(seawords + seavalues + ","));
                    System.out.println("\n");

If anyone has any good ideas / answers then please let me know, I am really desperate for a solution!

Link to current code: https://pastebin.com/gqWH0x0K

解决方案

first you will have to separate the words with index(es) from your compressed line, using your example:

"see[2, 4, 5],sea[0, 1, 3],"

to obtain following Strings:

"see[2, 4, 5]" and "sea[0, 1, 3]"

for each you must read the indexes, e.g. for first:

2, 4 and 5

now just write the word in an ArrayList (or array) at the given index.

For the first two steps you can use a regular expression to find each word and the index list. Then use String.split and Integer.parseInt to get all indexes.

Pattern pattern = Pattern.compile("(.*?)\\[(.*?)\\],");
String line = "see[2, 4, 5],sea[0, 1, 3],";
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    String word = matcher.group(1);
    String[] indexes = matcher.group(2).split(", ");
    for (String str : indexes) {
        int index = Integer.parseInt(str);

Now just check that the result List is big enough and set the word at the found indexes.

这篇关于如何获取压缩文件(通过索引)并重新创建原始文件? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆