我有一个txt文件得到一个特定的值:如何从一个文本文件
80,90,100,110,120,130,140,150,160
100,20,22,24,26,28,26,28,29,27
110,30,32,34,36,37,39,37,39,40
120,40,41,42,44,45,46,48,47,49
代表与价格百叶窗表格,第一行是盲目的,没有第一列的宽度80是身高。其余的数字是价格。
我已经做了这个使用C#,但在Java中我不知道该怎么做,在C#我的代码看起来像这样,一切都很好。有人可以在java中看到同样的东西吗? theWidth和theHeight是我必须键入尺寸的文本字段。
string[] lines = File.ReadAllLines(@"tabel.txt");
string[] aux = lines[0].Split(',');
for (int i = 0; i < aux.Length-1; i++)
{
if (aux[i] == theWidth.ToString())
{
Console.WriteLine(aux[i]);
indiceLatime = i;
}
}
for (int i = 1; i < lines.Length; i++)
{
aux = lines[i].Split(',');
if (aux[0] == theHeight.ToString())
{
showPrice.Text = aux[indiceLatime + 1];
}
}
在java中我想是这样的:
try {
BufferedReader inputStream = new BufferedReader(new FileReader("tabel.txt"));
int theWidth = 90;
int theHeight = 100;
int indiceLatime = 0;
String line;
try {
while ((line = inputStream.readLine()) != null) {
String[] aux = line.split(",");
for (int i = 0; i < aux.length; i++) {
if (aux[i].equals(Integer.toString(theWidth))) {
indiceLatime = i;
}
}
for (int i = 1; i < aux.length; i++) {
if (aux[0].equals(Integer.toString(theHeight))) {
System.out.println("price: " + aux[indiceLatime]);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
所以价格在theWidth的指数,我试图得到某种方式的theHeight的行。有没有人能告诉我如何从行中得到正确的数字(价格)?
我不是Java专家,但是这看起来像C#。 OP正在问如何用Java来做到这一点。 – paqogomez
没有它的java代码来读取文件 – Kick
它可能看起来像C#,但它不是。 Java的。 –