【问题标题】:Write hashset to txt using filewriter使用 filewriter 将 hashset 写入 txt
【发布时间】:2015-05-25 18:11:53
【问题描述】:

我正在尝试将哈希集写入文本文件。通常,我对写入 txt 没有任何问题,因为这对我来说很简单。但是,在这种情况下,我绝对一无所知。

关于该类的一些背景知识:它是具有 2 个字段的 ComputerScientist 对象的哈希集;名称和研究领域(是的,我知道我用来填充哈希集的内容不算数,我只是想测试一下我是否可以让它工作)。

我知道使用 filewriter 将字符串保存到 hashset 的基本设置,这是我在 SO 上发现的许多类似问题所处理的,所以这些并没有真正帮助我。

我渴望学习,如果不包括讽刺或侮辱性的 cmets,我将不胜感激。如果已经有类似的问题涉及将对象的哈希集写入 txt 文件,我很抱歉没有看到它。

import java.util.HashSet;
import java.io.FileWriter;
import java.io.IOException;
/**
* Write a description of class ComputerScientistSet here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class ComputerScientistSet
{
private HashSet<ComputerScientist> computerScientistSet;
private FileWriter computerScientistWriter;
private String fileName;
/**
 * Constructor for objects of class ComputerScientistSet
 */
public ComputerScientistSet(){
    computerScientistSet = new HashSet<ComputerScientist>();
    fileName = "scientist-names.txt";
    setComputerScientistSet();
}

private void setComputerScientistSet(){
    computerScientistSet.add (new ComputerScientist("Bob", "Robotics"));
    computerScientistSet.add (new ComputerScientist("Tim", "VR"));
    computerScientistSet.add (new ComputerScientist("Jake", "Nuclear Fision"));
    computerScientistSet.add (new ComputerScientist("Joe", "Snapple"));
    computerScientistSet.add (new ComputerScientist("Jane", "Magnets"));
    computerScientistSet.add (new ComputerScientist("Mary", "PC"));
}

public void writeNames(){
    try{
        computerScientistWriter = new FileWriter(fileName, true);
        computerScientistWriter.write(computerScientistSet);
        computerScientistWriter.close();
    }
    catch(IOException ioException){
        System.out.println("Error.");
    }
}
}

更新: 如果我包含以下代码会有所帮助吗?我仍在研究 .write() 行括号中的内容。我的大脑被炸了:/

for (int i = 0; i < computerScientistSet.size(); i++) {
            computerScientistWriter.write();
        }

【问题讨论】:

  • 我以前从未使用过FileWriter,但它似乎没有接受HashSetwrite 方法。您可以尝试computerScientistWriter.write(computerScientistSet.toString());,但我不知道您希望HashSet 保存的表单。
  • 当你问我希望它以什么形式保存时,你的意思是输出文件类型吗?
  • 那种。我的意思是你想把它保存在文本文件还是二进制文件中?如果要将其保存在文本文件中,您要将HashSet 保存为什么String?这些问题很难,所以我会选择@maklemenz 的答案。

标签: java hashset bluej


【解决方案1】:

由于我假设您想将集合写入文件以供稍后读取,因此最好的方法不是重新发明轮子,而是使用序列化。

public class Person implements java.io.Serializable {
    private String name;
    // constructor, setter, getter, etc
}

public static void main(String[] args) {
    Set<Person> persons = new HashSet<Person>();
    persons.add(new Person("foo");
    try {
        FileOutputStream fileOut = new FileOutputStream("/tmp/persons.data");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(e);
        out.close();
        fileOut.close();
     } catch(IOException e) {
         e.printStackTrace();
     }
     try {
         FileInputStream fileIn = new  FileInputStream("/tmp/persons.data");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         Set<Person> persons = (Set<Person>) in.readObject();
         in.close();
         fileIn.close();
     } catch(Exception e) {
         e.printStackTrace();
     }
}

【讨论】:

  • 第二个close() 在每种情况下都是多余的。第一个close() 关闭嵌套流。
【解决方案2】:

最终使用了这种编码:

public void writeNames(){
 try{
        computerScientistWriter = new FileWriter(fileName, true);                       
        for(ComputerScientist list: computerScientistSet){
               computerScientistWriter.write(list.getName() + " , " + "\n" + list.getField() + System.lineSeparator() );
        }
        computerScientistWriter.close();
    }
    catch(IOException ioException){
        System.out.println("Error.");
    }
}

【讨论】:

  • 变量名的选择很差。 ComputerScientist 不是 list. Setlist 这里。 item 或者确实是 computerScientist 会是更好的选择。
  • 感谢您指出这一点。我拥有的许多变量名称以及“计算机科学家”及其“领域”的列表都需要更改。我现在所拥有的大部分只是一个粗略的草稿,以便我可以掌握如何做所有事情。
  • 在我相当丰富的经验中,“草稿”的意思是“有缺陷的版本”。如果你不把bug放进去,或者像这样的出现bug的场合,你以后也不必把它们拿出来。
【解决方案3】:

根据您的 cmets here,您的输出当前显示许多条目为:ComputerScientist@7e384e79

我强烈建议您不要依赖该链接,而是使用您的代码当前输出的示例来更新此问题。

当您看到类似 ComputerScientist@7e384e79 的输出时,这意味着 ComputerScientist 类没有覆盖 toString() 方法来显示其状态。您可以这样做以使您的技术发挥作用。您可以手动完成,也可以使用提供重构的 IDE 以减少繁琐,但您不是免费获得的。您需要为希望以这种方式输出的每个类实现它。

【讨论】:

  • 你会推荐哪个 IDE?
  • Eclipse 和 InteliJ 是我使用的,但还有很多其他的。 Eclipse 就是这些屏幕截图的来源。无论如何,软件推荐更像是 superuser.com 的事情。
  • 请勿在此处发布屏幕截图。完全浪费每个人的带宽,一旦 UI 改变就过时了。您只需要说“实现toString() 方法”。
  • @EJP 感谢您解释反对意见。我不明白你的截图问题。定义“这里”。
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 2020-09-29
  • 2020-08-28
  • 1970-01-01
相关资源
最近更新 更多