将中的java对象转换为管道分隔字符串
假设有一个类:将中的java对象转换为管道分隔字符串,java,string,serialization,Java,String,Serialization,假设有一个类: public class Employee { private String name; private String age; private String birthdate; private String location; } 我们有一个值为name=John,age=25,birthdate=9月10日,location=houston的对象。有没有一种简单或有效的方法将该对象转换为管道分隔字符串,如下所示 John|25|September
public class Employee
{
private String name;
private String age;
private String birthdate;
private String location;
}
我们有一个值为name=John,age=25,birthdate=9月10日,location=houston的对象。有没有一种简单或有效的方法将该对象转换为管道分隔字符串,如下所示
John|25|September 10|houston
我只能想到一种解决方案——将对象成员的值追加到字符串中。我想知道是否有更好的方法来实现这一点。使用String.format
System.out.println (String.format ("%s|%s|%s|%S", name, age, birthdate, location));
将其放入重写的toString
方法中使用String.format
System.out.println (String.format ("%s|%s|%s|%S", name, age, birthdate, location));
将其放入覆盖的
toString
方法中这就是toString方法的用途。请注意age
和birthdate
实际上不应该是字符串。我只是给出了一个示例。这不是我的实际类。这是toString方法的用途。请注意,age
和birthdate
实际上不应该是字符串。我只是给出了一个示例。这不是我的实际类。这很可能不属于toString方法。它看起来很像一些接口/通信的东西,因此可能应该放在一个单独的类中。这很可能不属于toString方法。它看起来非常像一些接口/通信的东西,因此可能应该放在一个单独的类中。