Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HashMap到txt文件-写入密钥&;javaoop中的值_Java_Oop_Hashmap_Filewriter_Bluej - Fatal编程技术网

HashMap到txt文件-写入密钥&;javaoop中的值

HashMap到txt文件-写入密钥&;javaoop中的值,java,oop,hashmap,filewriter,bluej,Java,Oop,Hashmap,Filewriter,Bluej,我正在创建一个聊天盒程序,该程序响应Java程序中键入的关键字。我有一个响应列表,存储在一个外部txt文件中,作为HashMap。我想创建一个方法,使我能够向HashMap添加新的关键字(key):响应(值),从而添加到外部txt文件 我已经在这个文本文件中插入了key:values,但是我很难以正确的格式向其中添加新的key:values,然后检索它们 我有一个名为“addtoresponses()”的方法,它应该从该方法中获取字符串参数,并将它们插入到hashMap中。然后,hashMap将

我正在创建一个聊天盒程序,该程序响应Java程序中键入的关键字。我有一个响应列表,存储在一个外部txt文件中,作为HashMap。我想创建一个方法,使我能够向HashMap添加新的关键字(key):响应(值),从而添加到外部txt文件

我已经在这个文本文件中插入了key:values,但是我很难以正确的格式向其中添加新的key:values,然后检索它们

我有一个名为“addtoresponses()”的方法,它应该从该方法中获取字符串参数,并将它们插入到hashMap中。然后,hashMap将使用另一种方法从另一个名为“listMap”的类写入txt文件

当我运行代码时,hashMap确实会写入外部文本文件,因此这似乎是可行的(它会将其写入外部文本文件,以便稍后可以被其他方法使用)。然而,当我再次运行我的程序时,我立即收到一条错误消息,说明:

文件Missing-map.txt中缺少对Hello的响应(Hello是插入的键)。

似乎我要添加的key:value就是我在方法中输入的内容中丢失了“value”部分。有人告诉我,这可能是我的格式,也就是说,它应该以以下方式存储

    **key
    response all on one line of length
    key
    another response all on one line
    etc.**
但我不清楚如何才能做到这一点。我很确定将HashMap插入txt文件的不是这个方法。我相信这就是我将值放入HashMap的方式,但我不是很有信心

如果有人能检查一下我的代码,看看我哪里出了问题,以及为什么会出现此错误消息以及我如何修复它,我将不胜感激

我已经在下面发布了代码

讲师模式的构造函数>>

public InstructorMode()
{
    helper = new FileAssistance();
    edits = new HashMap<String, String>();
}
public void addtoresponse(String key, String value)
{
    edits.put(key, value); 
    helper.listaMap(edits, "missing-map.txt");
}
    public void listaMap(HashMap<String, String> map, String filename)
     {
    if(map != null) {
        try (FileWriter writer = new FileWriter(filename, true)) {
            for(String key : map.keySet()) {
                String value = map.get(key);
                if(value == null) {
                    System.out.println("Warning: " +
                                       key + " in listaMap.");
                    value = "Not sure";
                }
                writer.write(key.trim());
                writer.write('\n');
                writer.write(value.trim());
                writer.write('\n');
            }                    
            writer.close();
        }
        catch(IOException e) {
            System.out.println("Issue: " + filename +
                               " in listaMap");
        }
    }
    else {
        System.out.println("Null map passed to listaMap.");
    }
}
添加到外部txt文件的方法(称为FileAssistance的类)>>

public InstructorMode()
{
    helper = new FileAssistance();
    edits = new HashMap<String, String>();
}
public void addtoresponse(String key, String value)
{
    edits.put(key, value); 
    helper.listaMap(edits, "missing-map.txt");
}
    public void listaMap(HashMap<String, String> map, String filename)
     {
    if(map != null) {
        try (FileWriter writer = new FileWriter(filename, true)) {
            for(String key : map.keySet()) {
                String value = map.get(key);
                if(value == null) {
                    System.out.println("Warning: " +
                                       key + " in listaMap.");
                    value = "Not sure";
                }
                writer.write(key.trim());
                writer.write('\n');
                writer.write(value.trim());
                writer.write('\n');
            }                    
            writer.close();
        }
        catch(IOException e) {
            System.out.println("Issue: " + filename +
                               " in listaMap");
        }
    }
    else {
        System.out.println("Null map passed to listaMap.");
    }
}
public void listaMap(HashMap映射,字符串文件名)
{
if(map!=null){
try(FileWriter=newfilewriter(文件名,true)){
for(字符串键:map.keySet()){
字符串值=map.get(键);
如果(值==null){
System.out.println(“警告:+
在listaMap中输入+);
value=“不确定”;
}
writer.write(key.trim());
writer.write('\n');
writer.write(value.trim());
writer.write('\n');
}                    
writer.close();
}
捕获(IOE异常){
System.out.println(“问题:”+文件名+
“在listaMap中”);
}
}
否则{
System.out.println(“传递给listaMap的空映射”);
}
}
非常感谢您在这方面的帮助

编辑-读取哈希映射的代码>>

  public HashMap<String, String> lookatM(String filename)
    {
    HashMap<String, String> map = new HashMap<>();
    try (BufferedReader reader =
            new BufferedReader(new FileReader(filename))) {
        String word;
        word = reader.readLine();
        while(word != null) {
            String response = reader.readLine();
            if(response != null) {
                response = response.trim();
                if(response.length() != 0) {
                    map.put(word, response);
                }
                else {
                    System.out.println("Blank response for " +
                                       word + " in file " +
                                       filename);
                }
            }
            else {
                System.out.println("Missing response for " +
                                   word + " in file " +
                                   filename);
            }
            word = reader.readLine();
        }
    }
    catch(IOException e) {
        System.out.println("Problem reading file: " + filename +
                           " in LookatM");
    }
    return map;
}
public HashMap lookatM(字符串文件名)
{
HashMap=newHashMap();
try(BufferedReader读取器=
新的BufferedReader(新的文件读取器(文件名))){
字符串字;
word=reader.readLine();
while(word!=null){
字符串响应=reader.readLine();
if(响应!=null){
response=response.trim();
if(response.length()!=0){
地图。放置(单词,响应);
}
否则{
System.out.println(“空白响应”+
word+“在文件中”+
文件名);
}
}
否则{
System.out.println(“缺少对的响应”+
word+“在文件中”+
文件名);
}
word=reader.readLine();
}
}
捕获(IOE异常){
System.out.println(“读取文件时出现问题:”+文件名+
“在ATM中”);
}
返回图;
}

您在循环中执行了两次
reader.readLine
,这就是输出消息的原因。你只需要使用“标准”

while((word = reader.readLine()) != null){
 //loop body
}

您可以使用
java.lang.Properties
,它基本上是一个
HashMap
,可以与
.Properties
文件交互

反过来,属性文件只不过是由“=”分隔的键和值的列表

例如,您可以拥有包含以下内容的属性文件:

Name=Sauron    
Attitude=Nice in a peculiar way
[...]
您可以使用
Properties.load()

如果要添加更多键/值对,只需使用方法Properties.setProperty()

如果要将属性对象的状态保存到备份属性文件,只需使用
Properties.store

有关更多信息,请参阅


此外,您还可以找到一个关于如何在代码中使用属性的教程

我还没有尝试在我的机器上运行您的代码,但通过查看您的代码片段,这里有一些建议-
1.请检查文件中包含键值行的任何隐藏字符。还有一件事要检查文件末尾是否有空换行符。
2.还有一件事要检查您是否仅在文件中的最后一个键值对中出现此错误。
3.我知道这是显而易见的,但在IDE中调试也会很有帮助!!

最后一件事,若您必须将映射存储到文件中并从文件重建,那个么您可以将映射对象序列化到文件中并从该序列化文件重建对象。这是我在网上找到的简单教程。
属性文件也可以完成上述工作。

您的问题在哪里?您提到有一条错误消息,它是什么?您能从文件中包含填充地图的代码吗?@Perdomoff。。。这是错误消息-