Java读文件 带前缀过滤

private static List<String> loadData(File file, String prefix) {
		List<String> contentList = new ArrayList<String>();
		
		if(!file.isFile()) {
			return null;
		}
		
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if("".equals(line)) continue;
				if(line.startsWith(prefix)) {
					contentList.add(line);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return contentList;
	}

 

你可能感兴趣的:(java)