簡體   English   中英

從txt中的每一行獲取制表符分隔的變量

[英]Get tab-delimited variables from each line in txt

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

例如,文件包含以下數據:

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  

我需要將這些值變為不同的變量。 例如,數字= 26317273105,電話= 77016080517,日期1 = 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

假設您有一個用制表符分隔的字符串,如下所示:

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

希望這可以幫助

以下內容將從輸入讀入的數據拆分為單獨的單詞

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

試試這個,它有效。

    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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM