在java中将字符串拆分为字符

时间:2015-03-12 14:00:39

标签: java

我想阅读一个包含以下文字的大文件:

QSSTNAFDQVS, h
SSTNAFDQVSA, h
STNAFDQVSAQ, h
TNAFDQVSAQG, h
NAFDQVSAQGK, h
AFDQVSAQGKV, h
FDQVSAQGKVI, h
EAILGDQFLKQ, h
AILGDQFLKQQ, h
ILGDQFLKQQY, h
LGDQFLKQQYV, h
VGKMAKSVMGH, h
GKMAKSVMGHN, h
KMAKSVMGHNW, h
MAKSVMGHNWV, h
....

我的代码:

BufferedReader br = new BufferedReader(file);
        while((line = br.readLine()) != null) {
            String[] values = line.split(",");
            String atributes = values[0];
            String cls = values[1].trim();
            for (int i=0; i<atributes.length(); i++) {
                char atribute = atributes.charAt(i);
                System.out.print(atribute + " ");
            }
            System.out.println();
        }

我的问题是,在我分割字符串atributes并打印它们之后,它们会以随机顺序打印出来:

S S D I T A S V N C A 
S D I T A S V N C A K 
D I T A S V N C A K K 
I T A S V N C A K K I 
T A S V N C A K K I V 
A S V N C A K K I V S 
.....

如果我进行打印,我不会像这样进行拆分:

BufferedReader br = new BufferedReader(file);
            while((line = br.readLine()) != null) {
                String[] values = line.split(",");
                String atributes = values[0];
                String cls = values[1].trim();
                System.out.println(atributes);
            }

我按照正确的顺序得到它们:

QSSTNAFDQVS
SSTNAFDQVSA
STNAFDQVSAQ
TNAFDQVSAQG
NAFDQVSAQGK
AFDQVSAQGKV
FDQVSAQGKVI
EAILGDQFLKQ
AILGDQFLKQQ
.....

我如何修复此问题,以便在拆分后以与文本文件中相同的顺序获取这些值?

3 个答案:

答案 0 :(得分:0)

我不认为问题是属性是随机顺序的,我在代码中看不到任何会对此负责的内容。你确定是这样的吗?这是完整的代码吗?

我运行了以下代码:

String line = "ZYXABCDEFGHIJ, h";
String[] values = line.split(",");
String atributes = values[0];
String cls = values[1].trim();
for (int i=0; i<atributes.length(); i++) {
    char atribute = atributes.charAt(i);
    System.out.print(atribute + " ");
}

我觉得这很好: Z Y X A B C D E F G H I J

答案 1 :(得分:0)

你的问题在别的地方 - 这段代码 - 显然与你的相同:

static final String[] data = {
    "QSSTNAFDQVS, h",
    "SSTNAFDQVSA, h",
    "STNAFDQVSAQ, h",
    "TNAFDQVSAQG, h",
    "NAFDQVSAQGK, h",
    "AFDQVSAQGKV, h",
    "FDQVSAQGKVI, h",
    "EAILGDQFLKQ, h",
    "AILGDQFLKQQ, h",
    "ILGDQFLKQQY, h",
    "LGDQFLKQQYV, h",
    "VGKMAKSVMGH, h",
    "GKMAKSVMGHN, h",
    "KMAKSVMGHNW, h",
    "MAKSVMGHNWV, h",};

public void test() {
    for (String line : data) {
        String[] values = line.split(",");
        String atributes = values[0];
        String cls = values[1].trim();
        for (int i = 0; i < atributes.length(); i++) {
            char atribute = atributes.charAt(i);
            System.out.print(atribute + " ");
        }
        System.out.println();

    }
}

打印

Q S S T N A F D Q V S 
S S T N A F D Q V S A 
S T N A F D Q V S A Q 
T N A F D Q V S A Q G 
N A F D Q V S A Q G K 
A F D Q V S A Q G K V 
F D Q V S A Q G K V I 
E A I L G D Q F L K Q 
A I L G D Q F L K Q Q 
I L G D Q F L K Q Q Y 
L G D Q F L K Q Q Y V 
V G K M A K S V M G H 
G K M A K S V M G H N 
K M A K S V M G H N W 
M A K S V M G H N W V 

该文件是否可被多个线程读取?

答案 2 :(得分:0)

似乎你在第一段代码中输入错误的文件。

您可以发布以下结果:

BufferedReader br = new BufferedReader(file);
            while((line = br.readLine()) != null) {
                String[] values = line.split(",");
                String atributes = values[0];
                String cls = values[1].trim();
                System.out.println(atributes);
                System.out.println();
                for (int i=0; i<atributes.length(); i++) {
                   char atribute = atributes.charAt(i);
                   System.out.print(atribute + " ");
                }
                System.out.println();
            }