2014-02-26 43 views
-4

我有一个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的行。有没有人能告诉我如何从行中得到正确的数字(价格)?

回答

5

可以使用的FileReader是这样的:

try { 

     br = new BufferedReader(new FileReader(filePath)); 
     while ((line = br.readLine()) != null) { 

      // code here to handle the current readed line 

     } 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

[编辑]

关于你的更新,我有编辑你的代码,请检查一下。

int widthIndex = 90; 
    int hightIndex = 100; 
    int indiceLatime = 0; 
    boolean lookForWidth = true; 

    try { 
     BufferedReader br = new BufferedReader(new FileReader("table.txt")); 
     String line = ""; 
     while ((line = br.readLine()) != null) { 

      String[] aux = line.split(","); 

      if(lookForWidth) {// this flag to look for width only at the first time. 
       for (int i = 0; i < aux.length; i++) { 
        if(widthIndex == Integer.parseInt(aux[i].trim())) { 
         indiceLatime = i; 
         lookForWidth = false; 
         continue; 
        } 
       } 
      } 

      for (int i = 0; i < aux.length; i++) { 
       if(hightIndex == Integer.parseInt(aux[0])) { 
        System.out.println(aux[indiceLatime]); 
        break; 
       } 
      } 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

我不是Java专家,但是这看起来像C#。 OP正在问如何用Java来做到这一点。 – paqogomez

+0

没有它的java代码来读取文件 – Kick

+1

它可能看起来像C#,但它不是。 Java的。 –

1

尝试这样的:

public static void main(String[] args) throws FileNotFoundException { 
String filePath = <Your file Path>; 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(filePath)); 
     String line = br.readLine(); 
     System.out.println(line); 
     while (line != null || !line.equals(null)) { 
      System.out.println(line); 
      line=br.readLine(); 
     } 
    } catch (Exception e) { 
    } 
}