Java 从文件中读取具有特定特征的行,排除所有其他行
我正在尝试编写一种方法,从文件中读取具有某种规格的行。比如说, 我的文本文件包含以下内容:-- 制表符分隔的值,用于不同的列,这些是我只希望方法读取的行。 现在假设,如果有,如下所示,甚至输入Java 从文件中读取具有特定特征的行,排除所有其他行,java,file-io,Java,File Io,我正在尝试编写一种方法,从文件中读取具有某种规格的行。比如说, 我的文本文件包含以下内容:-- 制表符分隔的值,用于不同的列,这些是我只希望方法读取的行。 现在假设,如果有,如下所示,甚至输入 12-01-01 13:26 San Jose 12.99 DVD 12-12-30 09:40 Miami 13.50 Music 14-08-30 10:20 Arizona 16.03 Scientist 11-07-10 09:10 New York 25.00 ColdPlay 14-08-30
12-01-01 13:26 San Jose 12.99 DVD
12-12-30 09:40 Miami 13.50 Music
14-08-30 10:20 Arizona 16.03 Scientist
11-07-10 09:10 New York 25.00 ColdPlay
14-08-30 10:20 Arizona 18.04 MeetYou
[new lines]
14-08-30 10:20 Arizona 50.03 Scientist
11-07-10 09:30 New York 25.00 ColdPlay
//This line should not be read
even this should not be read #$%^&
11-07-10 09:20 New York 25.00 ColdPlay
该行应该转义。到目前为止,当文件格式合适时,我已经做了如下工作:--
publicstaticvoidmain(字符串[]args){
BufferedReader br=null;
字符串temp=null;
List arrayRead=新建ArrayList();
试一试{
br=新的BufferedReader(新的文件读取器(“D:\\testing\\SalesData.txt”);
而((temp=br.readLine())!=null){
阵列读取添加(温度);
}
int n=arrayRead.size();
System.out.println(“文件中的记录数”+n);
//将arrayList数据添加到字符串数组
String[]linesToRead=arrayRead.toArray(新字符串[arrayRead.size()]);
字符串[]lineX=null;
Hashtable dataReq=新的Hashtable();
for(int i=0;i为什么不?可能有用
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
public class MyLineReader {
public static void main(String[] args) {
File inputFile = new File("myfile.txt");
// Create pattern object.
String pattern = "^(\\d{2}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2})\\s([a-zA-Z\\s]*)\\s(\\d*\\.?\\d*)\\s(\\w*)$";
ArrayList<String[]> collectedLines = new ArrayList<String[]>();
Pattern r = Pattern.compile(pattern);
// Match those.
Matcher m;
FileInputStream fis = null;
try{
fis = new FileInputStream(inputFile);
Scanner fileScanner = new Scanner(fis);
String line;
String[] row;
while (fileScanner.hasNextLine()){
line = fileScanner.nextLine();
m = r.matcher(line);
if (m.find()) {
// Regex have groups.
row = new String[] { m.group(1), m.group(2), m.group(3), m.group(4) };
collectedLines.add ( row );
System.out.println ( String.format("Date: %s, Name: %s, Decimal: %s, Last: %s", row[0], row[1], row[2], row[3]));
}
}
}catch(IOException ex){
System.err.println(ex.getMessage());
}finally {
if (fis != null){
try{
fis.close();
}catch(Exception ex){
}
}
}
}
}
我做对了吗:你想从你的文件中提取文本(如“亚利桑那州”)和价格(或下一栏中的任何内容),忽略任何非该格式的内容?
public static void main(String[] args) {
BufferedReader br = null;
String temp = null;
List<String> arrayRead = new ArrayList<String>();
try{
br = new BufferedReader(new FileReader("D:\\testing\\SalesData.txt"));
while((temp=br.readLine())!= null){
arrayRead.add(temp);
}
int n = arrayRead.size();
System.out.println("No. of Records in file "+n);
//Add arrayList data to String Array
String[] linesToRead = arrayRead.toArray(new String[arrayRead.size()]);
String[] lineX = null;
Hashtable<String, String> dataReq = new Hashtable<String, String>();
for(int i=0; i<arrayRead.size(); i++){
lineX = linesToRead[i].split("\\t");
dataReq.put(lineX[2], lineX[3]);
}
}
catch(FileNotFoundException f){
f.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
if(br!= null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
public class MyLineReader {
public static void main(String[] args) {
File inputFile = new File("myfile.txt");
// Create pattern object.
String pattern = "^(\\d{2}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2})\\s([a-zA-Z\\s]*)\\s(\\d*\\.?\\d*)\\s(\\w*)$";
ArrayList<String[]> collectedLines = new ArrayList<String[]>();
Pattern r = Pattern.compile(pattern);
// Match those.
Matcher m;
FileInputStream fis = null;
try{
fis = new FileInputStream(inputFile);
Scanner fileScanner = new Scanner(fis);
String line;
String[] row;
while (fileScanner.hasNextLine()){
line = fileScanner.nextLine();
m = r.matcher(line);
if (m.find()) {
// Regex have groups.
row = new String[] { m.group(1), m.group(2), m.group(3), m.group(4) };
collectedLines.add ( row );
System.out.println ( String.format("Date: %s, Name: %s, Decimal: %s, Last: %s", row[0], row[1], row[2], row[3]));
}
}
}catch(IOException ex){
System.err.println(ex.getMessage());
}finally {
if (fis != null){
try{
fis.close();
}catch(Exception ex){
}
}
}
}
}
String pattern = "^(\\d{2}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2})\\s([a-zA-Z\\s]*)\\s(\\d*\\.?\\d*)\\s(\\w*)$";