English 中文(简体)
Java从其代表处获得名单?
原标题:Load List in Java from its String representation?

我知道,可以通过撰写一份职能来做到这一点,但我很想知道,是否迅速和方便地从驻 Java的代表处装出清单。

我举一个小例子:

List<String> atts = new LinkedList<String>();
atts.add("one"); atts.add("two"); atts.add("three”);
String inString = atts.toString()); //E.g. store string representation to DB
...
//Then we can re-create the list from its string representation?
LinkedLisst<String> atts2 = new LinkedList<String>();

Thnx!

最佳回答

你们不想为此而使用。 相反,你们想要使用java的序列化方法。 例如:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(stream);
out.writeObject(list);
stream.close();
// save stream.toByteArray() to db

// read byte
ByteArrayInputStream bytes = ...
ObjectInputStream in = new ObjectInputStream(bytes);
List<String> list = in.readObject();

这是一种更好的解决办法,因为你不必分裂或 par。 您也可以使用其他方法,如json或xml。 我在贾瓦建造了上述东西。

问题回答

//Then we can re-create the list from its string representation?

一种选择是商定使用一种已知格式,既转换成代表,又从代表制上调。 我会在这里谈谈特别志愿人员,除非你在你最初的座右上 com。

String csvString = "one,two,three";
List<String> listOfStrings = Arrays.asList(csvString.split(","));

There is not a reliable way to do this. The toString() method for Lists is not designed to output something that can be reliably used as a way to serialise the list.

这样,它就能够打工。 以你为例审视这一小变化:

public static void main(String[] args) {
    List<String> atts = new LinkedList<String>();
    atts.add("one");
    atts.add("two");
    atts.add("three, four");
    String inString = atts.toString();
    System.out.println("inString = " + inString);
}

产出

inString = [one, two, three, four]

This looks like the list contains four elements. But we only added three. There is not feasible way to determine the original source list.

您可以从其代表处编制一份名单。 如果《指令》载有“、”或“《指令》”栏目,则该清单可能并不完全相同,但你可以这样做,并且将努力处理许多使用案例。

List<String> strings = Arrays.asList("one", "two", "three");
String text = strins.asList();
// in this case, you will get the same list.
List<String> strings2=Arrays.asList(text.substring(1,text.length()-1).split(", "));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签