2

我有一个文件:

12345678;ABC 123456A12345678;45678945

这就是我所做的:

Scanner s = new Scanner(new File(testCase.getFileName()));
while (s.hasNext()) {
    String[] lineItems = s.next().split(";");
}

输出:

12345678;ABC
123456A12345678;
45678945

期望的输出:

12345678
ABC 123456A12345678
45678945

我希望它被"ABC 123456A12345678"视为一个单一的标记,并且在遇到空格时不会中断。

我该怎么办?

4

3 回答 3

6

这里

“扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格”。

因此,您的程序首先根据空格拆分文件,然后.split(";");将它们拆分为;.

您需要将分隔符设置;如下:

Scanner s = new Scanner(new File(testCase.getFileName())));
s.useDelimiter(";");
while (s.hasNext()) {
   System.out.println(s.next());
}
于 2013-02-21T15:29:06.290 回答
0

你可以使用这样的东西:String[] items = line.split(";");

请考虑以下示例以更好地理解:假设您的硬盘驱动器中有一个名为“data.txt”的文件(例如“C:/Users/sarath_sivan/Desktop”),其中包含您的记录,如下所示:

12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945

并且您希望在使用 java.util.Scanner 类进行扫描时忽略空格。

首先,我们可以创建一个模型类来保存您的数据。你可以使用这样的东西:

package com.stack.overflow.works.model;

/**
 * @author sarath_sivan
 */
public class Data {

    private String column1;
    private String column2;
    private String column3;

    public Data() {}

    public String getColumn1() {
        return column1;
    }

    public void setColumn1(String column1) {
        this.column1 = column1;
    }

    public String getColumn2() {
        return column2;
    }

    public void setColumn2(String column2) {
        this.column2 = column2;
    }

    public String getColumn3() {
        return column3;
    }

    public void setColumn3(String column3) {
        this.column3 = column3;
    }

}

接下来,我们可以创建一个用于扫描目的的界面。包 com.stack.overflow.works.service;

/**
 * @author sarath_sivan
 */

public interface Scannable {

    abstract public void scan();

}

接下来,我们可以通过创建一个实现接口的新类来实现业务逻辑

package com.stack.overflow.works.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.stack.overflow.works.model.Data;

/**
 * @author sarath_sivan
 */
public class FileScanner implements Scannable {

    private static final String SEMICOLON = ";";
    private static final String TAB_SPACE = "\t";
    private static final String FILE_NAME = "C:/Users/sarath_sivan/Desktop/data.txt";

    @Override
    public void scan() {
        File file = new File(FILE_NAME);
        Scanner scanner = null;
        List<Data> recordList = new ArrayList<Data>();
        Data data = null;
        try {
            scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] items = line.split(SEMICOLON);
                data = new Data();
                data.setColumn1(items[0]);
                data.setColumn2(items[1]);
                data.setColumn3(items[2]);
                recordList.add(data);
            }
            displayRecords(recordList); /*Displaying your records*/
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void displayRecords(List<Data> recordList) {
        System.out.println("*DISPLAYING YOUR RECORDS:*");
        System.out.println("COLUMN1" + TAB_SPACE + "COLUMN2" + TAB_SPACE + TAB_SPACE + "COLUMN3");
        for (Data data: recordList) {
            System.out.println(data.getColumn1() + TAB_SPACE + data.getColumn2() + TAB_SPACE + data.getColumn3());
        }
    }

}

最后,我们可以创建一个服务或测试类来验证逻辑。包 com.stack.overflow.works.main;

import com.stack.overflow.works.service.FileScanner;
import com.stack.overflow.works.service.Scannable;

/**
 * @author sarath_sivan
 */
public class ScannerService {

    public static void main(String[] args) {
        Scannable fileScanner =  new FileScanner();
        fileScanner.scan();
    }

}

现在,您可以运行生成如下输出的 ScannerService 类:

显示您的记录:

COLUMN1     COLUMN2             COLUMN3
------------------------------------------
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945
12345678    ABC 123456A12345678 45678945

您可以在此处查看包结构在此处输入图像描述

希望这可以帮助。谢谢!

于 2013-02-21T17:02:10.597 回答
0
    LinkedList<Double> a = new LinkedList<Double>();
    File f = new File("C:/Users/etc.txt");
    BufferedReader in = new BufferedReader(new FileReader(f));
    String [] tmp=null;
    String line;
    while ((line = in.readLine()) != null) {
        if(line.trim().contains("   "))
             tmp = line.split(" ");
        a.add(Double.parseDouble(tmp[0].trim()));
    }
于 2014-03-13T13:41:34.490 回答