Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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
将中的java对象转换为管道分隔字符串_Java_String_Serialization - Fatal编程技术网

将中的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方法。它看起来非常像一些接口/通信的东西,因此可能应该放在一个单独的类中。