使用Filewriter将哈希集写入txt [英] Write hashset to txt using filewriter

查看:85
本文介绍了使用Filewriter将哈希集写入txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless.

有关该类的背景知识:它是具有2个字段的ComputerScientist对象的哈希集;名称和研究领域(是的,我知道我填写的哈希集不算什么,我只是想测试一下是否可以使它起作用).

A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the hashset does not count, I was only trying to test to see if I could get this to work).

我知道使用文件编写器将字符串保存到哈希集的基本设置,这是我在SO上发现的许多类似问题所解决的问题,因此这些问题并没有真正帮助我.

I know the basic setup to use filewriter to save strings to hashset which is what a lot of the similar questions which I found on SO dealt with, so those did not really help me.

我很想学习,如果您不屑任何粗俗或侮辱性的评论,我将不胜感激.并且,如果已经存在一个类似的问题,涉及将对象的哈希集写入txt文件,我对此表示歉意.

I am eager to learn, and would appreciate it if snide or insulting comments were left out. And if there is already a similar question which deals with writing hashsets of objects to txt file, I apologize for not seeing it.

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()行括号中的内容.我的大脑炸了:/

Update: Would it help if I include the following code? I am still working out what would go in the parentheses by the .write() line. My brain is fried :/

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

推荐答案

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

Since I assume you want to write the set to a file to read it in later, the best way would be not to reinvent the wheel and to use serialization instead.

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();
     }
}

这篇关于使用Filewriter将哈希集写入txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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