用java读取文件,直到找到一行
我有一个文件如下所示:用java读取文件,直到找到一行,java,arraylist,Java,Arraylist,我有一个文件如下所示: color1 red color2 blue color3 white end color1 blue color2 green end color1 black color2 white color3 red end 我想读取此文件,并将数据放在end之间的ArrayList 我的代码: public ArrayList<ArrayList<HashMap<String,String>>> getColors (String pat
color1 red
color2 blue
color3 white
end
color1 blue
color2 green
end
color1 black
color2 white
color3 red
end
我想读取此文件,并将数据放在end
之间的ArrayList
我的代码:
public ArrayList<ArrayList<HashMap<String,String>>> getColors (String path) {
ArrayList<ArrayList<HashMap<String,String>>> allColors = new ArrayList<ArrayList<HashMap<String,String>>>();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = null;
ArrayList<HashMap<String,String>> color = new ArrayList<HashMap<String,String>>();
while((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
String color = tokens[0];
String colorValue = tokens[1];
HashMap<String,String> colorWithValue = new HashMap<String,String>();
colorWithValue.put(color, colorValue);
color.add(colorWithValue);
}
allColors.add(color);
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return allColors;
}
public ArrayList getColors(字符串路径){
ArrayList allColors=新的ArrayList();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(路径));
字符串行=null;
ArrayList color=新的ArrayList();
而((line=br.readLine())!=null){
String[]tokens=line.split(“”);
字符串颜色=标记[0];
字符串colorValue=标记[1];
HashMap colorWithValue=新HashMap();
colorWithValue.put(颜色,colorValue);
添加(colorWithValue);
}
所有颜色。添加(颜色);
br.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回所有颜色;
}
问题是,当我找到一个
结束
并重新开始时,如何停止,构建一个新的颜色数组列表?您可以使用类似的方法
while((line = br.readLine()) != null) {
if ("end".equals(line.trim())) {
allColors.add(color);
color = new ArrayList<HashMap<String,String>>();
}
else {
String[] tokens = line.split(" ");
String color = tokens[0];
String colorValue = tokens[1];
HashMap<String,String> colorWithValue = new HashMap<String,String>();
colorWithValue.put(color, colorValue);
color.add(colorWithValue);
}
}
br.close();
while((line=br.readLine())!=null){
如果(“end”.equals(line.trim())){
所有颜色。添加(颜色);
颜色=新的ArrayList();
}
否则{
String[]tokens=line.split(“”);
字符串颜色=标记[0];
字符串colorValue=标记[1];
HashMap colorWithValue=新HashMap();
colorWithValue.put(颜色,colorValue);
添加(colorWithValue);
}
}
br.close();
附言:你的allColors
是如此之深,以至于我在其中看到了彩虹…如果你坚持使用这种方法,我建议使用这种算法
public static ArrayList<ArrayList<HashMap<String, String>>> getColors(String path)
{
ArrayList<ArrayList<HashMap<String, String>>> allColors = new ArrayList<>();
try
{
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
do
{
line = br.readLine();
ArrayList<HashMap<String, String>> colors = new ArrayList<>();
while (line != null && !"end".equals(line)) {
String[] tokens = line.split(" ");
String color = tokens[0];
String colorValue = tokens[1];
HashMap<String, String> colorWithValue = new HashMap<>();
colorWithValue.put(color, colorValue);
colors.add(colorWithValue);
line = br.readLine();
}
allColors.add(colors);
} while (line != null);
br.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return allColors;
}
publicstaticarraylistgetcolors(字符串路径)
{
ArrayList allColors=新的ArrayList();
尝试
{
BufferedReader br=新的BufferedReader(新文件读取器(路径));
弦线;
做
{
line=br.readLine();
ArrayList colors=新的ArrayList();
而(行!=null&!“.end.”等于(行)){
String[]tokens=line.split(“”);
字符串颜色=标记[0];
字符串colorValue=标记[1];
HashMap colorWithValue=新HashMap();
colorWithValue.put(颜色,colorValue);
添加(colorWithValue);
line=br.readLine();
}
所有颜色。添加(颜色);
}while(line!=null);
br.close();
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
返回所有颜色;
}
但您可以使用其他算法来实现它。您可以通过使用其他结构来降低算法的复杂性。你知道,在我的生活中,从来没有使用过像这样的深层嵌套结构:ArrayListlook在拆分时只给出一个标记的行,该标记是end:tokens[0]。equals(“end”)
为什么要创建一个单项哈希映射列表?这似乎不是一个可以轻松获得颜色的结构。您听说过“使用资源进行尝试”吗?在这种情况下很有用。ey@fabian,我喜欢你的想法。。。您能否“升级”我的代码,以展示如何利用Java的现代特性来完成它?(尽管为那些刚刚开始学习的人保留我的榜样是很好的)。@Victor:当然。。。如果你让它成为社区维基。。。但是你自己也可以很容易地做到。只需将br
的声明/初始化移动到()
括号中的try
之后,然后删除br.close()代码>。以下是一个教程: