【问题标题】:Get tab-delimited variables from each line in txt从 txt 中的每一行获取制表符分隔的变量
【发布时间】:2015-02-12 10:57:03
【问题描述】:

你能帮我从文本文件的每一行中拆分制表符分隔的单词吗?

例如文件包含以下数据:

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 等

请帮忙。

编辑:这是我尝试的:

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

【问题讨论】:

  • 请编辑您的帖子并添加一些您目前尝试过的源代码。
  • @SebastianStigler 添加了我尝试过的内容。但我只有第一行的输出值

标签: java string file


【解决方案1】:

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

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

希望对你有帮助

【讨论】:

  • 谢谢。我试过这个。但我只有第一行的输出值。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试这个,它有效。

        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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2013-01-08
      相关资源
      最近更新 更多