Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取未格式化的文本文件并将其值存储在hashmap中_Java - Fatal编程技术网

Java 读取未格式化的文本文件并将其值存储在hashmap中

Java 读取未格式化的文本文件并将其值存储在hashmap中,java,Java,我有一个文本文件,包含以下格式的数据 Vehicle:Bike MOdel:FZ Make: Yamaha Description abcdefgh ijklmn problems gear problem, fork bend. this is auto data ***********************************end*********************** Vehicle:Bike MOdel:R15 Ma

我有一个文本文件,包含以下格式的数据

  Vehicle:Bike
  MOdel:FZ
  Make:
  Yamaha
  Description
  abcdefgh
  ijklmn
  problems
  gear problem, fork bend.

  this is auto data
  ***********************************end***********************
  Vehicle:Bike
  MOdel:R15
  Make:
  Yamaha
  Description
  1234,
  567.
  890
  problems
  gear problem, fork bend.
  oil leakage

  this is auto data
  ***********************************end***********************
我已经给出了2个数据,但是在一个文本文件中还有更多这样的数据,我想读取它并将其存储在一个hashmap中,这样

  Bike:FZ:Yamaha:abcdefghijklmn:gear problem,fork bend.
  Bike:R15:Yamaha:1234,567.890:gear problem,fork bend.oil leakage
我的示例代码:

public static void main(String[] args) {

        try {
            BufferedReader br = new BufferedReader(new FileReader("data.txt"));

            String sCurrentLine;
            int i = 0;
            int j = 0;
            hmap = new HashMap<String, Integer>();
            while ((sCurrentLine = br.readLine()) != null) {

                System.out.println(sCurrentLine);
                sCurrentLine = sCurrentLine.trim();
                if (!sCurrentLine.equals("")) // don't write out blank lines
                {
                    if (sCurrentLine.startsWith("***********")) {
                        i++;
                    } else {

                        if (sCurrentLine.startsWith("Vehicle:")) {
                            String[] veh = sCurrentLine.split(":");
                            String vehicle = tType[1];
                        }
                        if (sCurrentLine.startsWith("Model:")) {
                            String[] mod = sCurrentLine.split(":");
                            String model = iShield[1];
                        }  
                        hmap.put(0,i+":"+vehicle+":"+model);                      
                    }
                }
j++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
publicstaticvoidmain(字符串[]args){
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(“data.txt”);
弦电流线;
int i=0;
int j=0;
hmap=新的HashMap();
而((sCurrentLine=br.readLine())!=null){
System.out.println(sCurrentLine);
sCurrentLine=sCurrentLine.trim();
如果(!sCurrentLine.equals(“”)//不要写出空行
{
if(sCurrentLine.startsWith(“*************”){
i++;
}否则{
如果(sCurrentLine.startsWith(“车辆:)){
字符串[]veh=sCurrentLine.split(“:”);
字符串vehicle=t类型[1];
}
if(sCurrentLine.startsWith(“Model:)){
字符串[]mod=sCurrentLine.split(“:”);
字符串模型=iShield[1];
}  
hmap.put(0,i+“:“+车辆+”:“+模型);
}
}
j++;
}
}捕获(IOE异常){
e、 printStackTrace();
}
}

不确定如何阅读-->make、description和problems属性。

您需要一个
ObjectInputStream

例如:

/* Create an ObjectInputStream for your text file
 * and a hash map to store the values in. */
ObjectInputStream obj = new ObjectInputStream(new FileInputStream(textFile));
hmap = (HashMap<String, String>) obj.readObject(); // I assume you want strings.
hmap.put("value", var); // Var can be whatever other strings you created.
// It is always a good idea to close streams.
obj.close();
/*为文本文件创建ObjectInputStream
*以及用于存储值的哈希映射*/
ObjectInputStream obj=新ObjectInputStream(新文件InputStream(textFile));
hmap=(HashMap)obj.readObject();//我想你想要字符串。
hmap.put(“值”,var);//Var可以是您创建的任何其他字符串。
//关闭溪流总是一个好主意。
obj.close();
请记住,如果需要将另一个变量类型放入哈希映射中,可以使用类似于
HashMap
的方法创建它

显然,您需要实现已经创建的方法来确定每个变量

如果我不够具体,或者遗漏了一些重要的内容,请告诉我。

您的问题遗漏了“问题部分”。我不明白map键应该是什么,值应该是什么。“解析”传入字符串有问题吗?!请澄清。