2014-12-06 40 views
1

我遇到的問題是將簡單文本文件的每一行插入到我想要的特定變量中。例如我的文本文件是有序像這樣(在字符串之間的文本文件中沒有空間只有這個編輯器就會把它們放在一起)如何將文本讀取字符串插入到特定變量

達拉斯

78˚F

北,15英里每小時

dallasimage

丹佛

29女性

南,10英里每小時

denverimage

,我想達拉斯市的變量,78F的溫度變化,並依此類推,直到文本結束,此刻是所有進入城市變量,它所有打印從那裏!我知道掃描儀有一個下一行,但從來沒有使用過,所以我被困在這裏。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

public class Textreader { 

    public static void main(String[] args) { 
     try { 
         String city ="a"; 
         String temp ="b"; 
         String wind ="c"; 
         String image = "d"; 
      File file = new File("load.txt"); 
      FileReader fileReader = new FileReader(file); 
      BufferedReader bufferedReader = new BufferedReader(fileReader); 
      StringBuffer stringBuffer = new StringBuffer(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
           city = stringBuffer.append(line).toString(); 
           stringBuffer.append("\n"); 
           //city = line.toString(); 
           temp = stringBuffer.append(line).toString(); 
           wind = line; 
           image = line; 
       //stringBuffer.append(line); 
       //stringBuffer.append("\n"); 
      } 
      fileReader.close(); 
      System.out.println("Contents of file:"); 
         System.out.println(city); 
         System.out.println(temp); 

      //System.out.println(stringBuffer.toString()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

您的問題與事實,你的變量「行」不改變他的內容,並重復所有變量。將所有行讀入集合或數組,然後進行處理。類似這樣的:

 

    int count = 0; 
    String[] lines = new String[4]; 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
     lines[counter++] = line; 
    } 

    city = lines[0]; 
    temp = lines[1]; 
    // .... 
+0

我這樣做,而是它確實增量,但它保持的一切讀取前一行幷包括它。例如,對於[1]行,它讀達拉斯,78 f,行[2]它讀達拉斯,78F北,15英里 – user3034262 2014-12-06 06:29:57

+0

嗨,我的壞。這件事太快了。我更正了代碼。這是一個簡單的例子,並不是解決問題的最佳方法。不過,它會爲你做這項工作。希望這可以幫助。 – Apokai 2014-12-06 23:27:06

0

您必須致電bufferedReader.readLine()來檢索下一行。

while ((line = bufferedReader.readLine()) != null) { 
    city = line; 
    temp = bufferedReader.readLine(); 
    wind = bufferedReader.readLine(); 
    image = bufferedReader.readLine(); 
} 
+0

當我嘗試它時,它返回null。 – user3034262 2014-12-06 06:28:42

+0

我是否必須將它們轉換爲字符串,我也試過,但所有返回null。 – user3034262 2014-12-06 07:34:54

0

查看java.util.Scanner類。你可以用它做這樣的事情:

Scanner s = new Scanner(file); 
Pattern pattern = Pattern.compile("(.+?)\n(.+?)\n(.+?)\n(.+?)\n"); 
while(s.findWithinHorizon(pattern, 0) != null) { 
    MatchResult result = s.match(); 
    city = result.group(1); 
    temp = result.group(2); 
    wind = result.group(3); 
    img = result.group(4); 
    // etc. (Note that the next pass through the loop will override these variables 
    // so, unless you save the values somewhere, you will end up with them holding last 
    // entry from your file once the loop is done. 
} 
+0

我不得不在程序中進一步使用緩衝讀取器,所有上述建議都無濟於事。 – user3034262 2014-12-06 07:16:19

+0

那麼,你不能在「稍後在程序中」使用同一個閱讀器,因爲無論如何你都已經閱讀了所有的東西。如果「沒有」幫助,可能意味着你誤解或誤用了它。如果你想得到更多的幫助,就必須提供比「沒有幫助」更多的信息.​​.....並展示你的工作。 – Dima 2014-12-06 18:46:32

相關問題