简体   繁体   English

如何在文本文件中写入数组的内容?

[英]How to write the contents of my array in a text file?

I have an array which has objects of people in a football team. 我有一个足球队中有人的对象的数组。 It holds information such as their first name, second name and address. 它包含诸如名字,姓氏和地址之类的信息。 When i use the code shown below the text file contains values like this: member@29086037 当我使用下面显示的代码时,文本文件包含以下值:member @ 29086037

The code is shown below: 代码如下所示:

try
{
    PrintWriter pr = new PrintWriter ("memberDetails.txt");
    for (int i = 0; i < collection.length; i++)
    {
    pr.println(collection[i]); 
    }
    pr.close();
} catch (FileNotFoundException ex)
{
    System.out.println(ex.getMessage());
    System.out.println("in" + System.getProperty("user.dir"));
    System.exit(1);
}

What am I doing wrong? 我究竟做错了什么?

When you see that malarky with the numbers and class name like that, it means you haven't overriden your toString() method, so it defaults to Object.toString() . 当您看到带有类似数字和类名的恶意代码时,表示您尚未覆盖toString()方法,因此它默认为Object.toString()

So, override the public String toString() method on your member class. 因此,在member类上重写public String toString()方法。

When you do pr.println(collection[i]); 当你做pr.println(collection[i]); as you didn't override it, you print Object::toString which represents the object in this way by default: 因为没有覆盖它,所以您默认打印Object :: toString ,它表示这种对象:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“ @”以及该对象的哈希码的无符号十六进制表示形式。 In other words, this method returns a string equal to the value of: 换句话说,此方法返回的字符串等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

To print each field, use properties of the object, for example: 要打印每个字段,请使用对象的属性,例如:

collection[i].getName(); 
collection[i].getAddress(); 

Other option, is to override toString() method of member . 另一种选择是重写 member toString()方法。

You have to provide the path to the file correctly. 您必须正确提供文件的路径。 I would suggest creating a file object and then pass it to Printwriter. 我建议创建一个文件对象,然后将其传递给Printwriter。 This way you can also make sure if File exist before assigning it to printwriter. 这样,您还可以在将File分配给Printwriter之前确保File是否存在。

As others have pointed out, assuming you have a class that models the players, you should provide a toString() implementation in the class. 正如其他人指出的那样,假设您有一个对玩家进行建模的类,则应在该类中提供toString()实现。 For example: 例如:

public class Player {
    private String firstName;
    private String lastName;
    private String address;
    ...
    @Override
    public String toString() {
        return String.format("Name: %s, %s. Address: %s", lastName, firstName, address);
    }
}

After that's been done, it becomes trivial to write the player information into a file. 完成之后,将播放器信息写入文件变得很简单。 Using an utility library such as Google's Guava , the solution simplifies into a one-liner: 使用诸如Google的Guava之类的实用程序库,该解决方案可简化为单行代码:

Files.write(Joiner.on(StandardSystemProperty.LINE_SEPARATOR.value())
                  .join(collection),
            new File("memberDetails.txt"),
            Charsets.UTF_8);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM