大家好  我现在需要读取这样一个TXT文档,提取出日期(2012/09/39/28),时间(06:55:37),进程号(比如:0x1c0c762) ,进程名(比如:Terminal,/usr/lib/opera/opera,在进程号后的第一个字符串),然后是剩下的操作。2012/09/39/28 06:55:37   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:55:41   8   S   0x1600313   /usr/lib/xfce4/notifyd/xfce4-notifyd   "xfce4-notifyd", "Xfce4-notifyd"   "xfce4-notifyd"
#2012/09/39/28 06:55:41,2012/09/39/28 06:55:41,2012/09/39/28 06:55:41
2012/09/39/28 06:55:45   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
#2012/09/39/28 06:55:49
2012/09/39/28 06:55:50   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
2012/09/39/28 06:55:50   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:55:52   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
2012/09/39/28 06:55:53   2   S   0x381f730   emacs --daemon   "emacs", "Emacs"   "EMACS: todo.org (/home/termier/org/todo.org)"
#2012/09/39/28 06:55:53
2012/09/39/28 06:55:54   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:55:58   0   S   0x1e00fa1   /usr/bin/python2 -O /usr/share/wicd/gtk/wicd-client.py --tray   "wicd-client.py", "Wicd-client.py"   "Gestionnaire réseau Wicd"
2012/09/39/28 06:56:05   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:56:07   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:56:07   0   S   0x3000012   emacs --font Monospace-10   "emacs", "Emacs"   
#2012/09/39/28 06:56:07
2012/09/39/28 06:56:08   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:56:08   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
2012/09/39/28 06:56:34   0   S   0x3a00003   nautilus --no-desktop /home/termier/Dropbox   "nautilus", "Nautilus"   "Dropbox"
我该如何读取啊。 如果可以的话写一点代码。十分感谢。

解决方案 »

  1.   

    读txt的网上很多 这里给你一个http://blog.csdn.net/zqfddqr/article/details/7311646分割的自己写代码判断一下
      

  2.   


    嗯嗯,请问 我用java tokenizer 可不可以像用一个数组一个list 一样把每个值一个个存起来呢?
      

  3.   


    嗯嗯,谢谢你啊。如果java tokenizer 可不可以像用一个数组一个list 一样把每个值一个个存起来呢?如果这样,我觉得可能比较简单点
      

  4.   

    不是的 直接用 String.split 这个方法 这是最快的了啊
      

  5.   

    package test;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;public class ReadTxtFile {
    public static void main(String[] args) {
    readTxt(new File("C:/Documents and Settings/Administrator/桌面/test.txt"));
    /*
     * ==============test.txt内容===================
     * 
     *  2012/09/39/28 06:55:37   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:41   8   S   0x1600313   /usr/lib/xfce4/notifyd/xfce4-notifyd   "xfce4-notifyd", "Xfce4-notifyd"   "xfce4-notifyd"
    #2012/09/39/28 06:55:41,2012/09/39/28 06:55:41,2012/09/39/28 06:55:41
    2012/09/39/28 06:55:45   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    #2012/09/39/28 06:55:49
    2012/09/39/28 06:55:50   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
    2012/09/39/28 06:55:50   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:52   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
    2012/09/39/28 06:55:53   2   S   0x381f730   emacs --daemon   "emacs", "Emacs"   "EMACS: todo.org (/home/termier/org/todo.org)"
    #2012/09/39/28 06:55:53
    2012/09/39/28 06:55:54   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:58   0   S   0x1e00fa1   /usr/bin/python2 -O /usr/share/wicd/gtk/wicd-client.py --tray   "wicd-client.py", "Wicd-client.py"   "Gestionnaire réseau Wicd"
    2012/09/39/28 06:56:05   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:07   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:07   0   S   0x3000012   emacs --font Monospace-10   "emacs", "Emacs"   
    #2012/09/39/28 06:56:07
    2012/09/39/28 06:56:08   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:08   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:34   0   S   0x3a00003   nautilus --no-desktop /home/termier/Dropbox   "nautilus", "Nautilus"   "Dropbox"
     */
    }

    public static void readTxt(File file) {
    try {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    if(reader == null) {
    throw new Exception("读取文件内容为空!");
    }
    String tempValue = reader.readLine();
    while ((tempValue = reader.readLine()) != null) {
    String[] lineValues = tempValue.split(" ");
    if(lineValues.length > 4) {
    System.out.println(lineValues[0]);// 日期
    System.out.println(lineValues[1]);// 时间
    System.out.println(lineValues[4]);// 进程
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  6.   

    执行程序代码:
    package test;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;public class ReadTxtFile {
    public static void main(String[] args) {
    readTxt(new File("C:/Documents and Settings/Administrator/桌面/test.txt"));
    /*
     * ==============test.txt内容===================
     * 
     *  2012/09/39/28 06:55:37   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:41   8   S   0x1600313   /usr/lib/xfce4/notifyd/xfce4-notifyd   "xfce4-notifyd", "Xfce4-notifyd"   "xfce4-notifyd"
    #2012/09/39/28 06:55:41,2012/09/39/28 06:55:41,2012/09/39/28 06:55:41
    2012/09/39/28 06:55:45   8   S   0x1c0c762   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    #2012/09/39/28 06:55:49
    2012/09/39/28 06:55:50   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
    2012/09/39/28 06:55:50   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:52   1   S   0x320000e   /usr/lib/opera/opera   "opera", "Opera"   "7th International Workshop on Business Process Intelligence (BPI '11) - Opera"
    2012/09/39/28 06:55:53   2   S   0x381f730   emacs --daemon   "emacs", "Emacs"   "EMACS: todo.org (/home/termier/org/todo.org)"
    #2012/09/39/28 06:55:53
    2012/09/39/28 06:55:54   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:55:58   0   S   0x1e00fa1   /usr/bin/python2 -O /usr/share/wicd/gtk/wicd-client.py --tray   "wicd-client.py", "Wicd-client.py"   "Gestionnaire réseau Wicd"
    2012/09/39/28 06:56:05   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:07   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:07   0   S   0x3000012   emacs --font Monospace-10   "emacs", "Emacs"   
    #2012/09/39/28 06:56:07
    2012/09/39/28 06:56:08   0   S   0x1c000ad   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:08   0   S   0x1c00004   Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier   "Terminal", "Terminal"   "Terminal - termier@akagi: ~"
    2012/09/39/28 06:56:34   0   S   0x3a00003   nautilus --no-desktop /home/termier/Dropbox   "nautilus", "Nautilus"   "Dropbox"
     */
    }

    public static void readTxt(File file) {
    try {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    if (reader == null) {
    throw new Exception("读取文件内容为空!");
    }
    String tempValue = reader.readLine();
    while ((tempValue = reader.readLine()) != null) {
    tempValue = tempValue.replace("   ", " ");// 把3个空格换成一个空格
    String[] lineValues = tempValue.split(" ");// 用" "空格分割文本
    if (lineValues.length > 5) {// 防止OutOfBoundException
    System.out.println("日期:" + lineValues[0] + "\t时间:" + lineValues[1] + "\t进程:" + lineValues[4]);
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    打印结果:
    日期:2012/09/39/28 时间:06:55:41 进程:0x1600313
    日期:2012/09/39/28 时间:06:55:45 进程:0x1c0c762
    日期:2012/09/39/28 时间:06:55:50 进程:0x320000e
    日期:2012/09/39/28 时间:06:55:50 进程:0x1c000ad
    日期:2012/09/39/28 时间:06:55:52 进程:0x320000e
    日期:2012/09/39/28 时间:06:55:53 进程:0x381f730
    日期:2012/09/39/28 时间:06:55:54 进程:0x1c000ad
    日期:2012/09/39/28 时间:06:55:58 进程:0x1e00fa1
    日期:2012/09/39/28 时间:06:56:05 进程:0x1c000ad
    日期:2012/09/39/28 时间:06:56:07 进程:0x1c00004
    日期:2012/09/39/28 时间:06:56:07 进程:0x3000012
    日期:2012/09/39/28 时间:06:56:08 进程:0x1c000ad
    日期:2012/09/39/28 时间:06:56:08 进程:0x1c00004
    日期:2012/09/39/28 时间:06:56:34 进程:0x3a00003
      

  7.   


    package model;import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;import files.logs.Focus;public class FocusScanner {
    public FocusScanner() throws IOException {
    File file = new File("/home/john/stage/log/20120928_focus_akagi.txt");
    BufferedInputStream bufferInput = new BufferedInputStream(
    new FileInputStream(file), 10 * 1024 * 1024);
    BufferedReader input = new BufferedReader(new InputStreamReader(
    bufferInput));
    String str = null;
    String[] array = null;
    String suboperation;
    int indexop;
    boolean indexls;
    boolean flag = false;
    ArrayList<Focus> list = new ArrayList<Focus>();
    while ((str = input.readLine()) != null) {
    Pattern p = Pattern.compile("\t");
    Matcher m = p.matcher(str);
    str = m.replaceAll("");
    array = str.split(" ");
    if (array.length > 10) {
    if (!array[0].contains("#")) {
    list.add(new Focus(array[0], array[1], array[2], array[3],
    array[4], array[5], array[6]));
    }
    }
    /*
     * indexop = array[5].indexOf(" "); if (indexop == -1) suboperation
     * = ""; else { suboperation = array[2].substring(indexop + 1);
     * array[2] = array[2].substring(0, indexop); } 
     * if (!array[0].contains("#")) { if (array[2].indexOf(" ") == -1) {
     * flag = true; } if (array[3].contains("NO_WINDOW") || flag) {
     * suboperation = "NO_WINDOW"; list.add(new Focus(array[0],
     * array[1], array[2], array[3], array[4], array[5], array[6])); }
     * else { list.add(new Focus(array[0], array[1], array[2], array[3],
     * array[4], array[5], array[6])); } }
     */
    }
    input.close();
    System.out.println(array[0].length());
    // List<DistDate> listDate=new ArrayList<DistDate>();
    for (Focus focus : list) {
    String dateTest = focus.getDate();
    if (dateTest.contains("2012/09/39/28")) {
    System.out.println(focus.toString());
    }
    }
    }
    }你好啊,谢谢你的代码,判断的那句话很有用啊,避免错误。
    我现在出现现在的问题:读出来的内容为
    Focus [Date: 2012/09/39/28 Time: 06:55:37 session:  ls: 8 id:    command: S operations: ]
    Focus [Date: 2012/09/39/28 Time: 06:55:41 session:  ls: 8 id:    command: S operations: ]
    Focus [Date: 2012/09/39/28 Time: 06:55:45 session:  ls: 8 id:    command: S operations: ]
    Focus [Date: 2012/09/39/28 Time: 06:55:50 session:  ls: 1 id:    command: S operations: ]
    Focus [Date: 2012/09/39/28 Time: 06:55:50 session:  ls: 0 id:    command: S operations: ]
    Focus [Date: 2012/09/39/28 Time: 06:55:52 session:  ls: 1 id:    command: S operations: ]我用debug发现   实际的id (进程)在 array[10] 其他没显示出来的也是,在数组的其他位置。现在切割还是有问题。你能帮我看看吗?
      

  8.   

    package model;import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;import files.logs.Focus;public class FocusScanner {
    public FocusScanner() throws IOException {
    File file = new File("/home/john/stage/log/20120928_focus_akagi.txt");
    BufferedInputStream bufferInput = new BufferedInputStream(
    new FileInputStream(file), 10 * 1024 * 1024);
    BufferedReader input = new BufferedReader(new InputStreamReader(
    bufferInput));
    String str = null;
    String[] array = null;
    String suboperation;
    int indexop;
    boolean indexls;
    boolean flag = false;
    ArrayList<Focus> list = new ArrayList<Focus>();
    while ((str = input.readLine()) != null) {
    Pattern p = Pattern.compile("\t");
    Matcher m = p.matcher(str);
    str = m.replaceAll("");
    array = str.split(" ");
    if (array.length > 10) {
    if (!array[0].contains("#")) {
    list.add(new Focus(array[0], array[1], array[2], array[3],
    array[4], array[5], array[6]));
    }
    }
    /*
     * indexop = array[5].indexOf(" "); if (indexop == -1) suboperation
     * = ""; else { suboperation = array[2].substring(indexop + 1);
     * array[2] = array[2].substring(0, indexop); } 
     * if (!array[0].contains("#")) { if (array[2].indexOf(" ") == -1) {
     * flag = true; } if (array[3].contains("NO_WINDOW") || flag) {
     * suboperation = "NO_WINDOW"; list.add(new Focus(array[0],
     * array[1], array[2], array[3], array[4], array[5], array[6])); }
     * else { list.add(new Focus(array[0], array[1], array[2], array[3],
     * array[4], array[5], array[6])); } }
     */
    }
    input.close();
    System.out.println(array[0].length());
    // List<DistDate> listDate=new ArrayList<DistDate>();
    for (Focus focus : list) {
    String dateTest = focus.getDate();
    if (dateTest.contains("2012/09/39/28")) {
    System.out.println(focus.toString());
    }
    }
    }
    }