从txt中的每一行获取制表符分隔的变量

时间:2015-02-12 10:57:03

标签: java string file

你可以帮我分解文本文件中每行的制表符分隔词。

例如文件包含以下数据:

26317273105      77016080517    2015-02-11 04:33:37      2015-02-11 04:33:39    2015-02-11 04:39:00 
26317273123      77715354350    2015-02-11 04:33:37      2015-02-11 04:33:39    2015-02-11 04:33:00  
26317273125      77715354350    2015-02-11 04:33:37      2015-02-11 04:33:42    2015-02-11 04:33:00  
26317273127      77715354350    2015-02-11 04:33:37      2015-02-11 04:33:43    2015-02-11 04:33:00  

我需要将这些值变为不同的变量。例如,number = 26317273105,phone = 77016080517,date1 = 2015-02-11 04:33:37等。

Plz帮助。

编辑:这是我尝试的:

public static void GetLines() {

    try {
    File fileDir = new File("c:\\download\\tmpfile.txt");

    BufferedReader in = new BufferedReader(
       new InputStreamReader(
                  new FileInputStream(fileDir), "windows-1251"));

    String str;

    while ((str = in.readLine()) != null) {
                String[] array = str.split("\t");
                System.out.println(array[0]);
                System.out.println(array[1]);
                System.out.println(array[2]);
                System.out.println(array[3]);
                System.out.println(array[4]);
    }

            in.close();
    } 
    catch (UnsupportedEncodingException e) 
    {
        System.out.println(e.getMessage());
    } 
    catch (IOException e) 
    {
        System.out.println(e.getMessage());
    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());
    }
}

但是在输出中,我只有第一行的值。

26317273105 
77016080517 
2015-02-11 04:33:37 
2015-02-11 04:33:39
2015-02-11 04:39:00

3 个答案:

答案 0 :(得分:0)

假设你有一个像这样的制表符分隔的字符串:

String a = "String1\tString2";

您可以使用以下方法将其拆分为字符串数组:

String[] array = a.split("\t");     // "\t" denotes a tab

并通过遍历数组将其分配给变量:

String foo = array[0]   // 0 is the first element in the array
String bar = array[1]   // ....and so on

foo将包含String1bar将包含String2

希望这有帮助

答案 1 :(得分:0)

以下内容将从输入读入的数据拆分为单独的单词

Pattern wordPattern = Pattern.compile("\\S+");
Scanner scanner = new Scanner(input);
while(scanner.hasNext(wordPattern)) {
    System.out.println("== " + scanner.next(wordPattern));
}

答案 2 :(得分:0)

试试这个,它有效。

    BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("D:\\data.txt"));
        while ((sCurrentLine = br.readLine()) != null) {
            String[] str = sCurrentLine.split("\\t");
            String s1 = str[0];
            String s2 = str[1];
            String s3 = str[2];
            String s4 = str[3];
            String s5 = str[4];
            System.out.println("s1 = "+s1+"\ns2 = "+s2+"\ns3 = "+s3+"\ns4 = "+s4+"\ns5 = "+s5);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

输出是:

s1 = 26317273105
s2 = 77016080517
s3 = 2015-02-11 04:33:37
s4 = 2015-02-11 04:33:39
s5 = 2015-02-11 04:39:00 
s1 = 26317273123
s2 = 77715354350
s3 = 2015-02-11 04:33:37
s4 = 2015-02-11 04:33:39
s5 = 2015-02-11 04:33:00  
s1 = 26317273125
s2 = 77715354350
s3 = 2015-02-11 04:33:37
s4 = 2015-02-11 04:33:42
s5 = 2015-02-11 04:33:00  
s1 = 26317273127
s2 = 77715354350
s3 = 2015-02-11 04:33:37
s4 = 2015-02-11 04:33:43
s5 = 2015-02-11 04:33:00