如何读取文件并使用它来创建一个hashmap? [英] How do I read a file and use it to create a hashmap with it?

查看:75
本文介绍了如何读取文件并使用它来创建一个hashmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我会解释这里的问题。我正在尝试阅读包含一些信息的文件,稍后我将使用这些信息来创建产品。我该怎么做?因为我已经尝试了几个星期来解决它:(并且截止日期已经结束。请帮助:(



我尝试了什么:



这是MAIN课程



import java.util。* ;

import java.io. *;



公共类VendingMachine {

public static void main(String [] args)抛出IOException {





产品;

显示屏显示;

ProductStorage product_storage;

CoinManager coin_manager;



字符串部分[] =新字符串[5];

int标记;

int i = 0;

int lt = 0;

char x;

String aaa;



//要打开的文件的名称。

String fileName =products.txt;



//这将一次引用一行

String line = null;



尝试{

// FileReader以默认编码读取文本文件。

FileReader fileReader = new FileReader(fileName);



//始终将文件读取器包装在BufferedReader中。

BufferedReader bufferedReader = new BufferedReader(fileReader);



// * StringTokenizer str = new StringTokenizer(); *



tokens = 0;

while((line = bufferedReader.readLine())!= null) {



System.out.println(line);

while(tokens< 4){

x = line.charAt(i);

if(x ==','){

parts [tokens] = line.substring(lt,i);

parts [tokens] .trim();

System.out.println(parts [tokens]);

令牌++;

System.out.println(i);

lt = i + 2;

}

i ++;

}



System.out.println(i);

if(令牌== 4){



int a = line.length();

System.out.println(i +。+ a);

/*parts [5] = line.substring(i,a-1);

parts [5] .trim();

System.out.println(parts [5]); * /

i = 0;

tokens = 0;

lt = 0;



}



System.out.println(...); < br $>


//parts [tokens] = line.substring(lt,i);

//System.out.println(parts[tokens ]);

}



//总是关闭文件。

bufferedReader.clos e(); {

}

catch(FileNotFoundException ex){

System.out.println(无法打开文件'+ fileName +');

}

catch(IOException ex){

System.out.println(读取文件错误 + fileName +');

}

}

}



这是产品文件:



import java.io. *;



公共类产品{

双倍价格;

字符串名称;

int id;

static int id_all = 0;



公共产品(双倍价格,字符串名称){

this.price = price;

this.name = name;

this.id = id_all;

id_all ++;

}



公共产品(双倍价格,字符串名称,int id){

this.price = price;

this.name = name;

this.id = id;

}



public double getprice(){

返回价格;

}



public void setprice(双倍价格){

this.price = price;

}



public String getname( ){

返回姓名;

}



公共布尔值等于(产品其他产品){

if(this.name.equals(otherproduct.name)){

if(this.price == otherproduct.price){

if(this。 id == otherproduct.id){

返回true;

}

}

}

返回false;

}

}



这是文本文件我必须阅读:



1,软饮料,可口可乐,330,1.00

2,水,水,500 ,0.50

3,水,水,1000,0.50

4,果汁,keanita,250,1.00

5,巧克力,lacta, 100,2.00



(只是数字,饮料类型,名称,st ock,和价格)

Ok guys, i will explain what is the problem here. I'm trying to read a file that contains some informations that i will use later on to create a "product". How can i do it? Because i have trying for few weeks to solve it :( and the deadline is close. Pls help :(

What I have tried:

This is the MAIN class

import java.util.*;
import java.io.*;

public class VendingMachine{
public static void main(String[] args)throws IOException{


Product product;
Display display;
ProductStorage product_storage;
CoinManager coin_manager;

String parts[]=new String[5];
int tokens;
int i=0;
int lt=0;
char x;
String aaa;

// The name of the file to open.
String fileName = "products.txt";

// This will reference one line at a time
String line = null;

try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);

//*StringTokenizer str = new StringTokenizer ();*

tokens=0;
while((line = bufferedReader.readLine()) != null) {

System.out.println(line);
while (tokens < 4){
x=line.charAt(i);
if (x==','){
parts[tokens]=line.substring(lt,i);
parts[tokens].trim();
System.out.println(parts[tokens]);
tokens++;
System.out.println(i);
lt=i+2;
}
i++;
}

System.out.println(i);
if (tokens==4){

int a=line.length();
System.out.println(i+"."+a);
/*parts[5]=line.substring(i,a-1);
parts[5].trim();
System.out.println(parts[5]);*/
i=0;
tokens=0;
lt=0;

}

System.out.println("...");

//parts[tokens]=line.substring(lt,i);
//System.out.println(parts[tokens]);
}

// Always close files.
bufferedReader.close();{
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
}
}
}

this is the "product" file:

import java.io.*;

public class Product{
double price;
String name;
int id;
static int id_all = 0;

public Product(double price, String name){
this.price=price;
this.name=name;
this.id=id_all;
id_all++;
}

public Product(double price, String name, int id){
this.price=price;
this.name=name;
this.id=id;
}

public double getprice(){
return price;
}

public void setprice (double price){
this.price = price;
}

public String getname(){
return name;
}

public boolean equals(Product otherproduct){
if (this.name.equals(otherproduct.name)) {
if (this.price == otherproduct.price) {
if (this.id == otherproduct.id) {
return true;
}
}
}
return false;
}
}

This is the text file that i have to read:

1, soft drink, cocacola, 330, 1.00
2, water, water, 500, 0.50
3, water, water, 1000, 0.50
4, juice, keanita, 250, 1.00
5, chocolate, lacta, 100, 2.00

(just number, the type of drink, the name, the stock, and the price)

推荐答案

您读取文件但不对内容做任何事情。从Collection对象开始( Trail:Collections(The Java™Tutorials) [ ^ ])将保存产品列表。然后在读取每一行时,将其拆分为相关的标记,并创建一个包含数据的新Product对象。将此对象添加到集合中并重复到文件末尾。你做什么取决于你,但你应该有一个很好的产品集合,你可以操纵。
You read the file but then do not do anything with the content. Start with a Collection object(Trail: Collections (The Java™ Tutorials)[^]) that will hold a list of products. Then as you read each line, split it into the relevant tokens and create a new Product object that will contain the data. Add this object to your collection and repeat to the end of the file. What you do then is up to you, but you should have a nice collection of products that you can manipulate.


这篇关于如何读取文件并使用它来创建一个hashmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆