你可以使用这样的东西: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
您可以在此处查看包结构
希望这可以帮助。谢谢!