在java中从文件中剪切列

在java中从文件中剪切列,java,Java,我搜索过了,找不到我的问题。 我已使用linux输出ls-l保存了文件,其内容为: drwxr-xr-x 2 usr usr 4096 Jan 20 17:49 file1 drwxrwxr-x 4 usr usr 4096 Jan 20 18:00 file2 drwx------ 2 usr usr 4096 Feb 3 08:48 catalog1 例如,我只想留下第八列的hour,然后删掉剩下的部分。我该怎么办?我是java和编程的初学者。您可以使用正则表达式匹配时间戳(因为它

我搜索过了,找不到我的问题。 我已使用linux输出ls-l保存了文件,其内容为:

drwxr-xr-x  2 usr usr 4096 Jan 20 17:49 file1
drwxrwxr-x  4 usr usr 4096 Jan 20 18:00 file2
drwx------  2 usr usr 4096 Feb  3 08:48 catalog1

例如,我只想留下第八列的hour,然后删掉剩下的部分。我该怎么办?我是java和编程的初学者。

您可以使用正则表达式匹配时间戳(因为它保证类似时间的值不会出现在任何其他字段中)。比如:

// Populate this with the output of the ls -l command
String input;

// Create a regular expression pattern to match.
Pattern pattern = Pattern.compile("\\d{2}:\\d{2}");

// Create a matcher for this pattern on the input string.
Matcher matcher = pattern.matcher(input);

// Try to find instances of the given regular expression in the input string.    
while (matcher.find()){
  System.out.println(matcher.group());
}

要检索任意列,您可以选择为要检索的任何列编写正则表达式,或者只需拆分空格字符上的每一行,然后选择“按索引”。例如,要获取所有文件大小:

String input;

String[] inputLines = input.split("\n");
for (String inputLine : inputLines) {
  String[] columns = inputLine.split(" ");
  System.out.println(columns[4]); // Where 4 indicates the filesize column
}

您可以使用正则表达式来匹配时间戳(因为它保证类似时间的值不会出现在任何其他字段中)。比如:

// Populate this with the output of the ls -l command
String input;

// Create a regular expression pattern to match.
Pattern pattern = Pattern.compile("\\d{2}:\\d{2}");

// Create a matcher for this pattern on the input string.
Matcher matcher = pattern.matcher(input);

// Try to find instances of the given regular expression in the input string.    
while (matcher.find()){
  System.out.println(matcher.group());
}

要检索任意列,您可以选择为要检索的任何列编写正则表达式,或者只需拆分空格字符上的每一行,然后选择“按索引”。例如,要获取所有文件大小:

String input;

String[] inputLines = input.split("\n");
for (String inputLine : inputLines) {
  String[] columns = inputLine.split(" ");
  System.out.println(columns[4]); // Where 4 indicates the filesize column
}
您需要使用来提取您要查找的确切信息。请尝试以下代码:

String value =  "drwxr-xr-x  2 usr usr 4096 Jan 20 17:49 file1\n"+
                "drwxrwxr-x  4 usr usr 4096 Jan 20 18:00 file2\n"+
                "drwx------  2 usr usr 4096 Feb  3 08:48 catalog1";
StringBuffer sBuffer = new StringBuffer(10);
StringTokenizer sTokenizer = new StringTokenizer(value,"\n");
while (sTokenizer.hasMoreTokens())
{
    String sValue = sTokenizer.nextToken();
    StringTokenizer sToken = new StringTokenizer(sValue," ");
    int counter = 0;
    while (sToken.hasMoreTokens())
    {
        String token = sToken.nextToken();
        counter++;
        if (counter == 8)//8 is the column that you want to leave.
        {
            sBuffer.append(token+"\n");
            break;
        }
    }           
}
System.out.println(sBuffer.toString());
您需要使用来提取您要查找的确切信息。请尝试以下代码:

String value =  "drwxr-xr-x  2 usr usr 4096 Jan 20 17:49 file1\n"+
                "drwxrwxr-x  4 usr usr 4096 Jan 20 18:00 file2\n"+
                "drwx------  2 usr usr 4096 Feb  3 08:48 catalog1";
StringBuffer sBuffer = new StringBuffer(10);
StringTokenizer sTokenizer = new StringTokenizer(value,"\n");
while (sTokenizer.hasMoreTokens())
{
    String sValue = sTokenizer.nextToken();
    StringTokenizer sToken = new StringTokenizer(sValue," ");
    int counter = 0;
    while (sToken.hasMoreTokens())
    {
        String token = sToken.nextToken();
        counter++;
        if (counter == 8)//8 is the column that you want to leave.
        {
            sBuffer.append(token+"\n");
            break;
        }
    }           
}
System.out.println(sBuffer.toString());

例如,我有写时间,我的意思是我想留下一列(我会选择),例如,时间是给定的。有关检索任意列的信息,请参阅我的更新答案。请记住,文件名可能包含几乎任何内容,包括空格,所以第9列一直延伸到行尾,不应拆分。例如,我有写时间,我的意思是我想留下一列(我将选择),例如,时间是给定的。有关检索任意列的信息,请参阅我的更新答案。请记住,文件名可能包含几乎任何内容,包括空格,所以第9列一直延伸到行尾,不应该被拆分。谢谢你们,这正是我需要的!它工作得很好,现在我只能逐行学习它是如何工作的;)谢谢,这正是我需要的!它工作得很好,现在我只能逐行学习它是如何工作的;)